Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Scanner Requiring Two Inputs

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Scanner Requiring Two Inputs

    Hello everyone, I知 having some issue with my code. Please keep in mind that I知 very new to programming/java, so understand that simple solutions are a lot easier for me to understand and use. I知 trying to allow the user to make a choice between two options. I知 using a scanner to get the next integer they type, assigning it to an int, and then comparing that to another (pre assigned) int. If they match it should display the first (if) outcome, if not, it should display the second (else) outcome. All this works. The problem is that I have to enter two numbers before it replies; only the first number matters though (eg. I press 1 then enter. Nothing happens, so I press 2 then enter and it tells me I choose option 1.) So if anyone can help me stop it from needing two inputs, it would be greatly appreciated.
    Here is my code:
     import java.util.Scanner;
     
    class UserInputsTest {
     
    	public static void main(String[] args) {
     
    		System.out.println("Type \"1\" for option 1 and \"2\" 							
                                     for option 2.");
     
    				//This prints a line asking for user input
     
    		Scanner scan = new Scanner(System.in);
    		int input = scan.nextInt();
    		int checkinput = 1;
    		scan.nextInt();
     
    			//Creates scanner object and assigns the next integer input value 				
                            //to int input.
     
    		if (input == checkinput) {
    			System.out.println("You choose option \"1\".");
    		} else {
    			System.out.println("You choose option \"2\".");
    		}
     
    			//Checks the input obtained from the scanner against int 					
                            //checkinput, if they match it uses the if outcome, if not it uses 				
                            //the else outcome.
    	}
    }


  2. #2
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Scanner Requiring Two Inputs

    Hey, what's up Nate.

    There are several great ways to debug your code; one of them, which I particularly advise, is to read through your code line by line and understanding what exactly is being done. Trust me. There's a difference between skimming your code and truly reading it.
    Simplicity calls for Complexity. Think about it.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Scanner Requiring Two Inputs

    Wonder why it would ask for two inputs. Must be someting going on.
    Have you read about the Scanner class?
    There may be a hint to what is going on in some related paperwork, maybe...

  4. The Following User Says Thank You to jps For This Useful Post:

    Nate (July 17th, 2012)

  5. #4
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Scanner Requiring Two Inputs

    Well, thanks for taking the time to reply. Staticity, trust me, I've read through my code multiple times, translating it into what I think it should be doing as I go, and I don't understand what the problem is. jps, thanks for that link, it kinda helped but most of it seemed unrelated to what I'm trying to accomplish. I guess I still don't understand why it wont print the response until a second thing is entered. Even someone pointing out where the issue is so I can look at it would be very nice.

  6. #5
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Scanner Requiring Two Inputs

    Quote Originally Posted by Nate View Post
    Well, thanks for taking the time to reply. Staticity, trust me, I've read through my code multiple times, translating it into what I think it should be doing as I go, and I don't understand what the problem is.
    You should give it another look. Doing it as you go isn't the best idea. You may be repeating your mistakes.

    Quote Originally Posted by Nate View Post
    jps, thanks for that link, it kinda helped but most of it seemed unrelated to what I'm trying to accomplish.
    Here's something I thought was very important from that link. "A scanning operation may block waiting for input."

    Just to explain, whenever you ask the scanner to scan something, it will not proceed with any operations until it receives an input value.
    Simplicity calls for Complexity. Think about it.

  7. The Following User Says Thank You to Staticity For This Useful Post:

    Nate (July 17th, 2012)

  8. #6
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Scanner Requiring Two Inputs

    Well, I had a long reply written up describing how that really didn't seem like it could be the source of the issue, when one of my arguments made me think, "wait a minute...".
    Thanks for the hints guys, I removed the second scan.nextInt and now it works fine. I feel stupid, but at least it's working now.

  9. #7
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Scanner Requiring Two Inputs

    Haha, that's programming. The most common error is the simplest error. Glad you found it.
    Simplicity calls for Complexity. Think about it.

Similar Threads

  1. Have user enter in 2 inputs instead of using -1 to end. Help appreciated!
    By neontiger in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2012, 03:26 AM
  2. Need more help, this time requiring the Comparable interface
    By Mellowhype in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 14th, 2011, 10:58 PM
  3. Concatenating Inputs, need help please!
    By xdrechsler in forum File I/O & Other I/O Streams
    Replies: 17
    Last Post: August 19th, 2011, 01:02 PM
  4. How to store 10 inputs in an arraylist?
    By Bray92 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 20th, 2011, 10:22 PM
  5. Inputs not being applied.
    By Norflok669 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 15th, 2010, 12:25 AM

Tags for this Thread