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

0

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

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;
        }
    }
}

Comments

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;
}

Comments

0

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

if(question[i].equals(""))

Comments

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.

Comments

0

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

if(question[i]==null)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.