Results 1 to 3 of 3
  1. #1
    Googol is offline Banned
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Check pre conditions for scanner system.in

    Java Code:
    public String userInput() {
    
        Scanner in = new Scanner(System.in);
        boolean cond = true;
        String result;
    
        // do while loop ensures the characters entered are in the correct format
        do {
    
          // set cond to false, if input fails any checks then it will be reset to true
          cond = false;
          // accessing user input
          System.out.println("\nType in the characters: ");
          String str = in.next();
    
          if (some check fails) 
            System.out.println("Try Again");
            cond = true;
          }
    
          result = str
          in.reset();
        } while (cond);
    
        in.close();
        return result;
    }
    My problem is a null pointer exception because of the in.next() statement, I am unsure how to run a check on input and then if failed, attempt to access the keyboard again without using the same scanner. I have put the scanner initiation/closing within the do while but that changed nothing. The scanner reset wasn't what I was looking for either. I am open to a more appropriate design style if anyone has a recommendation, or just a fix for this one.

    Thank You for your help.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Check pre conditions for scanner system.in

    Don't close the scanner as it's also closing the underlying streams, i.e. System.in.
    Java Code:
    Scanner in = new Scanner(System.in);
        boolean cond = true;
        String result;
     
        // do while loop ensures the characters entered are in the correct format
        do {
     
          // set cond to false, if input fails any checks then it will be reset to true
          cond = false;
          // accessing user input
          System.out.println("\nType in the characters: ");
          String str = in.nextLine();
     
          if (some check fails)
            System.out.println("Try Again");
            cond = true;
          }
     
          result = str
        } while (cond);
        return result;
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    ajb5876 is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Check pre conditions for scanner system.in

    PROBLEM SOLVED! Thanks All!

    -Austin

Similar Threads

  1. Problem in while conditions
    By davie89 in forum New To Java
    Replies: 2
    Last Post: 03-20-2012, 01:37 AM
  2. Replies: 4
    Last Post: 10-22-2011, 04:52 AM
  3. Replies: 6
    Last Post: 06-07-2010, 04:47 PM
  4. Asking for a username with conditions
    By ScentOfAWookie in forum New To Java
    Replies: 2
    Last Post: 03-20-2009, 06:53 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
  •