Results 1 to 3 of 3
  1. #1
    grifan526 is offline Member
    Join Date
    Jul 2009
    Location
    I live in Jacksonville, Fl
    Posts
    4
    Rep Power
    0

    Question Problem with scanner in loop

    I am writing a program that will be used sort of as a note card study tool, but I am currently having trouble creating "note cards". The program runs a loop scanning in the information for the "front side" and the "back side" of the card, but when the loop is repeated it skips that scanner for the "front side". I would greatly appreciate it if anyone can tell me why.

    Java Code:
    PrintWriter newNotes= new PrintWriter(output);
    String frontSide=new String();
    String backSide=new String();
    Scanner input=new Scanner(System.in);
    int decision=1;
    
    do{
    System.out.println("Front side of card is:");
    frontside=input.nextLine();//reads in front side of card
    System.out.println("The Back Side of the card is:");
    backside=input.nextLine();//reads in backside of card
    System.out.println("Are these correct: 1 for yes, 2 for no\n"+frontSide+"\n"+backSide);
    decision=input.nextInt();//makes sure information is correct
    if(decision==1){
    System.out.println("Enter 1 for make another card, or 2 for I'm done");
    decision=input.nextInt();
    }
    }while(decision!=2);

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You might need to have the Scanner object swallow the end of line characters after getting one or both ints. This is done by calling nextLine() on the scanner object and discarding the returned result.

    For example:
    Java Code:
    import java.util.Scanner;
    
    public class Fu2 {
      public static void main(String[] args) {
        String frontSide = new String();
        String backSide = new String();
        Scanner input = new Scanner(System.in);
        int decision = 1;
    
        do {
          System.out.print("Front side of card is: ");
          frontSide = input.nextLine();// reads in front side of card
          System.out.print("The Back Side of the card is: ");
          backSide = input.nextLine();// reads in backside of card
          System.out.println("Are these correct: 1 for yes, 2 for no\n" + frontSide
              + "\n" + backSide);
          decision = input.nextInt();// makes sure information is correct
          input.nextLine();  // get and discard end of line here
          if (decision == 1) {
            System.out.println("Enter 1 for make another card, or 2 for I'm done");
            decision = input.nextInt();   // get and discard end of line here
            input.nextLine();
          }
        } while (decision != 2);
      }
    }
    Now you have to figure out what to do if the user presses 2 -- the data entered was incorrect.

    Good luck.
    Last edited by Fubarable; 07-20-2009 at 11:20 PM.

  3. #3
    grifan526 is offline Member
    Join Date
    Jul 2009
    Location
    I live in Jacksonville, Fl
    Posts
    4
    Rep Power
    0

Similar Threads

  1. while loop bypasses scanner input on 2nd pass
    By xf021209 in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 08:10 AM
  2. Scanner-While Loop
    By hyunski in forum New To Java
    Replies: 2
    Last Post: 03-12-2009, 02:15 AM
  3. Loop Problem
    By jralexander in forum New To Java
    Replies: 4
    Last Post: 12-02-2008, 07:08 AM
  4. Scanner input problem
    By slayer_azure in forum New To Java
    Replies: 3
    Last Post: 05-26-2008, 10:49 PM
  5. problem with scanner class:incompatible types
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 07:02 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •