2

I was trying to make a program that prints out user-inputted values in an ArrayList, and for the most part, it works. Except it does not print the first element. Here is the code:

import java.util.Scanner;
import java.util.ArrayList;
public class Family {
    public static void main(String[] args){
        ArrayList<String> names=new ArrayList<String>();
        Scanner in=new Scanner(System.in);
        System.out.println("Enter the names of your immediate family members and enter \"done\" when you are finished.");
        String x=in.nextLine();
        while(!(x.equalsIgnoreCase("done"))){
            x = in.nextLine();
            names.add(x);

        }
        int location = names.indexOf("done");
        names.remove(location);
        System.out.println(names);
    }
}

For example if, I enter jack, bob, sally, it'll print [bob, sally]

2
  • 2
    FYI, you don't need the 2-step indexOf() and remove(). names.remove("done") will do the trick. Commented Mar 2, 2017 at 20:21
  • user did any of the answers help you? Commented Mar 2, 2017 at 20:30

3 Answers 3

4

You're calling nextLine() immediately when you enter the loop, losing the previously inputted line in the process. You should use it before reading an additional value:

while (!(x.equalsIgnoreCase("done"))) {
    names.add(x);
    x = in.nextLine();            
}

EDIT:
This, of course, means that "done" won't be added to names, so the following lines, and they should be removed:

int location = names.indexOf("done");
names.remove(location);
Sign up to request clarification or add additional context in comments.

7 Comments

@EduardoDennis Sure it does.
@shmosel it gives arrayIndexOutofBounds I just tried it. look at the line where he is doing names.remove the index will be higher than the size of the array list.
@EduardoDennis those lines should, of course, be removed - I've edited it into my answer.
of course ¯_(ツ)_/¯ you and @shmosel should upvote my question because I was right :D
@shmosel it wasnt working, there I removed it ahah lets become allies since we are always in the Java questions :) I even gave upvote
|
1
String x=in.nextLine();

this line outside the while loop consumes first input because as you enter the while loop, you again call x=in.nextLine(); without saving the first input, so it gets lost. Hence it doesn't gets printed because its not in the ArrayList.

Just remove String x=in.nextLine(); that is included before the while loop and your code will work fine.

String x="";

System.out.println("Enter the names of your immediate family members and enter \"done\" " +
"when you are finished.");

while(!(x.equalsIgnoreCase("done"))){
    x = in.nextLine();
    names.add(x);
}

Comments

0

because the first element is consumed by the first x= in.nextLine(); and you never added it to the list.

Try this:

 System.out.println("Enter the names of your immediate family members and enter \"done\" when you are finished.");
        String x="";
        while(!(x.equalsIgnoreCase("done"))){
            x = in.nextLine();
            names.add(x);

        }

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.