Skipped User Input


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Skipped User Input

  1. #1
    Join Date
    Mar 2006
    Location
    San Luis Obispo
    Posts
    6

    Skipped User Input

    this may seem like a trivial matter but i am executing a while loop with a System.out.println(...); command followed by a call to a scanner to recieve input from the user. this executes perfectly the first time but after that the loop will execute once with out calling the scanner's .readLine() method and then going through again and calling it.

    very frustrating and i have debugged and watched this happen and am very stumped as to why it only does it every other time...

    any help or questions are appreciated

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Not sure I understand your question. Can you post your code?

  3. #3
    Join Date
    Mar 2006
    Location
    San Luis Obispo
    Posts
    6
    Quote Originally Posted by destin
    Not sure I understand your question. Can you post your code?
    sorry i wasnt clear enough... when i get back to my room i will post a shortened version of the method this error occurs in. thanks for the followup though!

  4. #4
    Join Date
    Mar 2006
    Location
    San Luis Obispo
    Posts
    6
    so here is the code in a shortened version

    Code:
    private License editLicense(License licenseToEdit, Scanner r) {
            while(true) {
                System.out.print("\n           Please enter a command: (E)dit name, (A)dd a ticket, (D)elete a ticket, (R)eturn to previous level: ");
    
                String tempCommand = r.nextLine();    
    
                if (tempCommand.equalsIgnoreCase("r")) {
                    ... do something else
                }
                else if (tempCommand.equalsIgnoreCase("e")) {
                    ... do something else
                }
                else if (tempCommand.equalsIgnoreCase("a")) {
                    ... so something else
                }
                else if (tempCommand.equalsIgnoreCase("d")) {
                    ... do something else
                }
            }
        }
    the problem is is that the first print (the one with "Enter a command...") prints out twice for each input scanned by the scanner. meaning the scanner only gets input from the user once every two times... i have no idea why

  5. #5
    Join Date
    Mar 2006
    Location
    San Luis Obispo
    Posts
    6
    i have actually found a fix for this. it appears that it when a scanners .nextDouble() method is invoke it for some reason skips the next call to the .nextLine() method. dont ask me why... cause i dont know. maybe some of you know? but i countered this by adding a call to the .nextLine() method right after the .nextDouble() and it skips this one and gives me correct output!

    this is still not perfect because it is not logical and does not make sense and i do not like having random method calls that do not do anything. so for now it is a fix but i am still looking into the root cause of this problem.

    oh and this works fine with a BufferedReader but my teach wants a single scanner so he can use some input textfiles on my program. so the BufferedReaders are out of the question unless someone knows an easy way to parse ints and doubles from a BufferedReader (or any other type of reader besides a Scanner)

    thanks

  6. #6
    Join Date
    Mar 2006
    Location
    San Luis Obispo
    Posts
    6
    UPDATE (for those that care):

    i have recently trashed the idea of using a Scanner and have gone back to using a BufferedReader. now to parse the double input from the string made with the .readLine() method i am now using the method .parseDouble(String); in the Double class. this works well and am happy with the results.

  7. #7
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote Originally Posted by tbrigham
    Code:
    private License editLicense(License licenseToEdit, Scanner r) {
            while(true) {
                System.out.print("\n           Please enter a command: (E)dit name, (A)dd a ticket, (D)elete a ticket, (R)eturn to previous level: ");
    
                String tempCommand = r.nextLine();    
    
                if (tempCommand.equalsIgnoreCase("r")) {
                    ... do something else
                }
                else if (tempCommand.equalsIgnoreCase("e")) {
                    ... do something else
                }
                else if (tempCommand.equalsIgnoreCase("a")) {
                    ... so something else
                }
                else if (tempCommand.equalsIgnoreCase("d")) {
                    ... do something else
                }
            }
        }
    If you're just checking for one char, just take the first character of the String.
    Code:
    private License editLicense(License licenseToEdit, Scanner scanner) {
        while (true) {
            System.out.print("\n\tPlease enter a command: (E)dit name, (A)dd a ticket, (D)elete a ticket, (R)eturn to previous level: ");
            
            char c = scanner.nextLine().charAt(0);
            
            if (Character.toLowerCase(c) == 'e') {
                // edit name
            } else if (Character.toLowerCase(c) == 'a') {
                // add a ticket
            } else if (Character.toLowerCase(c) == 'd') {
                // delete a ticket
            } else if (Character.toLowerCase(c) == 'r') {
                // return to previous level
            }
        }
    }

Similar Threads

  1. Replies: 5
    Last Post: 02-17-2006, 09:12 AM
  2. Excel Macro / modify formulas via user input
    By fatboy in forum VB Classic
    Replies: 2
    Last Post: 01-19-2006, 08:54 AM
  3. Replies: 0
    Last Post: 02-05-2002, 03:11 PM
  4. Script for scrolling
    By Mark in forum Web
    Replies: 3
    Last Post: 08-30-2001, 11:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center