Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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.

share|improve this question
    
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? – Andrew Aitken Sep 3 '15 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.) – shane Sep 3 '15 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. – shane Sep 3 '15 at 10:32

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.