0

The problem requires to input different values for each attribute.Ex:
Color Black White
Water Cool Hot Medium
Wind Strong Weak
I made ArrayList of ArrayList of String to store such thing as no. of values of each attribute is not fixed.The user inputs Black White and on hitting new line the program has to start taking values of NEXT attribute( Cool Hot Medium).The no. of attributes has been already specified.I followed some (almost related) answers here and wrote the following code:

ArrayList<ArrayList<String>> attributes = new ArrayList<ArrayList<String>>();
    String input;
    for(i=0; i<num_of_Attributes ;i++)
    { System.out.print(" Enter attribute no." + i+1 + " : ");
     ArrayList<String> list = new ArrayList<String>();
     while(! input.equals("\n"))
    {
        list.add(input);
        input = sc.nextLine(); 
    }
     attributes.add(list);
    }


The program prints "Enter Attribute 1 : " but even after new line it doesn't print "Enter attribute 2 : ".It goes into infinite loop. How can I achieve what the program requires to do? sc is my Scanner object.

3
  • So you're not getting all the input at the same time, the program has to wait to receive more input after the first line, is that correct? Commented Sep 3, 2015 at 9:12
  • First my program outputs "Enter attribute 1: " I enter "Black White", but after pressing enter "Enter attribute 2: never comes.(no matter how many words/newlines i give.) Commented Sep 3, 2015 at 9:27
  • I am able to do this using StringTokeniser.That serves my purpose for now. I can't figure out how to do this by comparing input or using isEmpty() etc. Commented Sep 3, 2015 at 10:32

2 Answers 2

0

You should read:

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine%28%29

specifically the part that states:

This method returns the rest of the current line, excluding any line separator at the end

So, if the user inputs an empty line with only the line separator \n, you will read an empty line without such line separator.

Check while (!input.isEmpty()) or, even better, while (!input.trim().isEmpty())

As a more general rule, you can debug your program (or even just print input) to try to find out yourself what is the actual value you are checking.

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

Comments

0

As a quick-Hack you can do sth. like

    for (i = 0; i < num_of_Attributes; i++) {
        input = " ";
        System.out.print(" Enter attribute no." + (i + 1) + " : ");
        ArrayList<String> list = new ArrayList<String>();
        while (!input.isEmpty()) {
            list.add(input);
            input = sc.readLine();
        }
        attributes.add(list);
    }

not nice but it works. Please also watch out for calculating in String concaternation. In you code it will print 01, 11, 21 and so on. With brackets it will work.

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.