0

I would greatly appreciate some help with my java code. I am using ArrayLists to store the users favorite type of vehicles. So they will input vehicles until they type "Exit" to get out of the loop. It will then print the array list. Right now, it is only reading every other input. Depending on how many inputs there are, the user might have to type "Exit" twice before it prints the results out. Any help would be greatly appreciated, thank you!

package bacaCCh6Sec8;
import java.util.ArrayList;
import java.util.Scanner;
public class BacaCCh6Sec8 {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    ArrayList<String> vehicles = new ArrayList<>();
    boolean done = false; 
    final String EXIT = "Exit";
    System.out.printf("Enter your favorite vehicles.\n");
    System.out.printf("Type Exit after you have input all your vehicles.\n");
    do {
        String input = in.next();
        if (!input.equals(EXIT)) {
            vehicles.add(in.next());
        } else {
            done = true;
        } // end If
    } while (!done); // End Do While
    System.out.printf("Your favorite vehicles are %s.\n" , vehicles);

    } // End Main
} // End Class
3
  • Look for wherever you call in.next(). At that spot you extract a token from the Scanner and either use it or discard it. Don't discard it! Commented Apr 15, 2017 at 0:32
  • 2
    So change vehicles.add(in.next()); to vehicles.add(input); Commented Apr 15, 2017 at 0:33
  • I knew it was something super simple... I sat there forever trying to figure it out. Thank you so much! @HovercraftFullOfEels Commented Apr 15, 2017 at 0:35

3 Answers 3

1

the user might have to type "Exit" twice before it prints the results out

the issue is that you're calling next() method twice hence the user must enter a value twice. the solution is to simply use the value you've got from the first next() method rather than discarding it.

String input = in.next();
if (!input.equals(EXIT)) {
     vehicles.add(input); // <--- we're using the input variable
} else {
      done = true;
} // end If
Sign up to request clarification or add additional context in comments.

Comments

0

Each call of .next() actually reads the input. So if you call .next() twice, it'll read two lines. To rectify change vehicles.add(in.next()); to vehicles.add(input);

Comments

0
    public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            ArrayList<String> vehicles = new ArrayList<>();
            boolean done = false; 
            final String EXIT = "Exit";
            System.out.printf("Enter your favorite vehicles.\n");
            System.out.printf("Type Exit after you have input all your vehicles.\n");
            do {
                String word = in.next();
                if(word.equalsIgnoreCase("exit")) break;

                vehicles.add(word);

            } while (!done); // End Do While
            System.out.printf("Your favorite vehicles are %s.\n" , vehicles);
     }
}

Well not my best code but it helped

Enter your favorite vehicles.
Type Exit after you have input all your vehicles.
hola
mundo
exit
Your favorite vehicles are [hola, mundo].

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.