8eardcules
8eardcules

Reputation: 21

End input of a string array

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

Upvotes: 2

Views: 739

Answers (7)

Joffrey
Joffrey

Reputation: 37700

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.

Upvotes: 4

parag.rane
parag.rane

Reputation: 137

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

if(question[i]==null)

Upvotes: 0

vikas patil
vikas patil

Reputation: 1

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.

Upvotes: 0

Daksh Shah
Daksh Shah

Reputation: 3113

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

Upvotes: 3

Fizer Khan
Fizer Khan

Reputation: 92745

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

Upvotes: 1

SteveL
SteveL

Reputation: 3389

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

Upvotes: 1

Evan Knowles
Evan Knowles

Reputation: 7501

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

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

Upvotes: 0

Related Questions