2

I am trying to take input of an array of strings (each string is a question) and my code is as follows:

void read_quest()throws IOException
{
    System.out.println("enter the questions(enter null to end input operation)");
    for(int i = 0;; i++)
      {
        question[i]=in.readLine();
        if(question[i].equals("\0")==true)
        {
            n=i-1;
            break;
        }
    }
}

but the loop is never exited i am using bluej as it is for a school project(we are allowed to use only bluej) thanks in advance

Joffrey
  • 32,348
  • 6
  • 68
  • 100
8eardcules
  • 21
  • 1

7 Answers7

4

In Java, you don't have to deal with "\0". Also, it might be hard for the user to input NUL character anyway.

The method in.readLine() returns a String that was input by the user, excluding the end-of-line character that terminates it.

  • If you want to check if user presses Enter without entering any text, compare the input with the empty string "" like this:
    if ("".equals(question[i]))

  • If you want to check for Ctrl+D (End of transmission character), compare with \4 as said in this post:
    if ("\4".equals(question[i])).
    Note: cannot test this in Eclipse, and the user will have to press Enter anyway after Ctrl+D

Note that if the user uses Ctrl-C, your readLine() will return null, and your program will exit not long after.


Side notes:

  • you don't need ==true, because x.equals(y) already is a boolean.

  • "literal".equals(variable) is safer than variable.equals("literal"): if your variable is null, the first version just yields false without crashing, while the second version throws NullPointerException.

Community
  • 1
  • 1
Joffrey
  • 32,348
  • 6
  • 68
  • 100
3

Compare it with anything other than "\0" and you are good to go, you have to do this because null cannot be entered

Daksh Shah
  • 2,997
  • 6
  • 37
  • 71
1

in.readLine() returns null if there are no more lines

void read_quest()throws IOException
{
    System.out.println("enter the questions(enter null to end input operation)");
    for(int i = 0;; i++)
      {
        question[i]=in.readLine();
        if(question[i]==null)
        {
            n=i-1;
            break;
        }
    }
}
SteveL
  • 3,331
  • 4
  • 32
  • 57
1

If the end of input, you can ask user just press enter without a string. so you can check for empty string or string length(0)

if (question[i].length() == 0) {
    break;
}
Fizer Khan
  • 88,237
  • 28
  • 143
  • 153
0

They cannot enter null - perhaps you meant a blank string? Try

if(question[i].equals(""))
Evan Knowles
  • 7,426
  • 2
  • 37
  • 71
0

in.readLine() is the method of inputstream it returns the line of text otherwise it returns the null so you have to check null condition.

if(question[i] == null)

Should solve your problem.

J. Steen
  • 15,470
  • 15
  • 56
  • 63
0

You comparing the null in wrong way in Java, you need to change the if condition as follows:

if(question[i]==null)
parag.rane
  • 139
  • 1
  • 6