The user must get out of while loop when he/she press enter in a new line. but it does not work and it keeps going to new lines

Habiballah Hezarehee :

I need to do the following exercise: a) Make a new text file b) Put the user's input into that text file c) we must save all user's input while user keeps typing but as soon as user pressing Enter in a new line (When an empty string is sent) the user must get out of the program. For coding this issue I have write the following codes, but when I try it by myself so I am stuck at while loop, cant get out when I sending empty string. So could anyone help with a solution for this issue?

Thanks

I have tried some of the solutions I have found on youtube like making if statement inside the while loop or adding the code that takes the input of the user inside the loop's condition. So I do not know what to do at the next stage. I tried to see the console window via the Eclipse output.


import java.io.*;
import java.util.Scanner;
public class lesson {
    public static void main(String[] args) throws IOException {

        File file = new File("mytext.txt");

        if (file.exists() == false) {
            file.createNewFile();
        }
        PrintWriter pw = new PrintWriter(file);
        System.out.println("Enter a text here: ");
        String str;
         while (true) {
             Scanner input = new Scanner(System.in);
            str = input.next();
            pw.println();
            if (str.equals(null)) {
                break;
            }
        } 
        pw.close();
        System.out.println("Done");
    } 
}

The user must get out of the loop when he/she sends an empty string. and the writing to the file must be finished.

Abra :

First the code, then the explanation...

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Lesson {
    public static void main(String[] args) {
        File file = new File("mytext.txt");
        try (Scanner input = new Scanner(System.in);
             PrintWriter pw = new PrintWriter(file)) {
            System.out.println("Enter a text here: ");
            String str = input.nextLine();
            while (str.length() > 0) {
                pw.println(str);
                pw.flush();
                str = input.nextLine();
            }
        }
        catch (IOException xIo) {
            xIo.printStackTrace();
        }
        System.out.println("Done");
    }
}

The above code requires at least Java 7 since it uses try-with-resources. Scanner should be closed, just like PrintWriter should be closed. The try-with-resources ensures that they are closed. Note that if file mytext.txt doesn't exist, creating a new PrintWriter will also create the file and if the file already exists, its contents will be removed and replaced with the text that you enter.

After that the prompt is displayed, i.e. Enter a text here, and the user enters a line of text. Method nextLine() will get all the text entered until the user presses Enter. I read your question again and the program should exit when the user presses Enter without typing any text. When the user does this, str is an empty string, i.e. it has zero length. That means I need to assign a value to str before the while loop, hence the first call to method nextLine() before the while loop.

Inside the while loop I write the value entered by the user to the file mytext.txt and then wait for the user to enter another line of text. If the user presses Enter without typing any text, str will have zero length and the while loop will exit.

Written and tested using JDK 12 on Windows 10 using Eclipse for Java Developers, version 2019-03.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=100708&siteId=1