1

I am making a project for characteristics of a vehicle. I have the code to make it run once without any problem. However, after i type Y to do it again it puts the first two questions together in one prompt. How do i fix this?

public static void main(String[] args) {

    //break continue
    char choice;

    Scanner in = new Scanner(System.in);

    do{       
        //vehicle make
        System.out.print("Please enter vehicle make ");
        String make = in.nextLine();

        //vehicle model
        System.out.print("Please enter vehicle model ");
        String model = in.nextLine();

        //vehicle color
        System.out.print("Please enter color of vehicle ");
        String color = in.nextLine();

        //number of doors
        System.out.print("Please enter number of doors ");
        int door = in.nextInt();

        //Print out information
        System.out.println("Your vehicle is a " +make + " " +model + ", which is " +color + " with " +door + " doors.");

        //Continue prompt
        System.out.print("Do you want to continue (Y/N): ");
        choice=in.next().charAt(0);

        }while((choice!='n')&&(choice!='N'));      

    } //end main

} //end class
Derlin
  • 8,518
  • 2
  • 22
  • 42
Lazirous
  • 21
  • 1
  • Are you entering just Y or Y then pressing enter? – Freiheit Oct 20 '17 at 14:29
  • Not familiar with the classes involved in Java, but I expect the newline accompanying the `Y` isn't consumed by `in.next()` and so remains in the buffer for `in.nextLine()` after the first `print`. I would just use `in.nextLine` instead of `in.next()`. – ryachza Oct 20 '17 at 14:32
  • I tried to do both in.next and in.nextLine when I put the in.nextLine() I get an error for the char variable and it won’t let me input (Y/N) – Lazirous Oct 20 '17 at 15:21
  • I have figured it out. Adding a in.nextLine(); below my integer input while also switching my Scanner in = in.nextLine(); location to inside my "do" method. Thank you for helping me. – Lazirous Oct 20 '17 at 15:42

0 Answers0