Results 1 to 6 of 6
  1. #1
    Xystus777 is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Exclamation Scanner Not working

    Hello, I'm not going to post my entire code, because it's quite lengthy, but for some reason, this section of code is not working. I have a Scanner called input, and it works in many other sections of this code. But it's not working here...let me show you my code first.

    Java Code:
    //Make a transaction
    				String transMenu;
    				atmMenu = 1;
    				transactionMenu = 1;
    				boolean secure = false;
    				
    				System.out.print("\nAs a security measure, please provide either your Account Number (N) or your Name along with your Zip Code (Z)." +
    						"\nWhich would you like to provide? ");
    				transMenu = input.next();
    				String zipCode = "";
    				String myName = "";
    				
    				if (transMenu.equals("Z") || transMenu.equals("z"))
    				{
    					System.out.println("\nPlease enter your full name: ");
    					myName = input.nextLine();
    					for (int i = 0; i <= 50; i++)
    					{
    						if (myName.equals(account[i].getName()))
    						{
    							System.out.print("\nPlease enter your Zip Code: ");
    							zipCode = input.next();
    							if (zipCode.equals(account[i].getZipCode()))
    							{
    								secure = true;
    								break;
    							}
    							else
    							{
    								System.out.print("\nThe Zip Code you entered does not match the name.");
    								secure = false;
    								break;
    							}				
    						}
    						else
    						{
    							System.out.print("\nThe name you entered is invalid.");
    							secure = false;
    							break;
    						}
    					}
    				}
    				else if (transMenu.equals("N") || transMenu.equals("n"))
    				{
    					System.out.print("\nPlease enter your Account Number: ");
    					int accMenu = input.nextInt();
    					for (int i = 0; i <= accountsOpen; i++)
    					{
    						if (accMenu == account[i].getAccountNumber())
    						{
    							myAccount = i;
    							secure = true;
    							break;
    						}
    						else
    						{
    							if (i == accountsOpen)
    							{
    								System.out.print("The Account Number you entered was not found in our system.");
    								break;
    							}
    						}
    					}
    				}
    				else
    				{
    					System.out.print("\nPlease enter 'N' for Account Number or 'Z' for Zip Code: ");
    				}

    Okay, so what this code snippet SHOULD DO is this:

    As a security measure, please provide either your Account Number (N) or your Name along with your Zip Code (Z). Which would you like to provide? (INPUT HERE, this one works, let's just say you pressed Z)

    Please enter your full name: (INPUT HERE, NOT WORKING, IT TOTALLY SKIPS THIS PART AND MOVES ON!!!!!)


    What it does is it skips that input (myName = input.nextLine() ) What's wrong with that? It looks fine. Then it checks my if statement, notices that my if is false because myName has not been changed at all, it still equals "". Then it goes to the else statement, and prints "The name you entered is invalid". I'm soooo clueless! If you want...I can post my entire code, but that might be a little much. Please help!

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Instead of:
    Java Code:
    myName = input.nextLine();
    try:
    Java Code:
    myName = input.next();
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Xystus777 is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    I tried that once, but the problem with that is, with just

    myName = input.next();

    I don't believe it would see both the FIRST and LAST name that needs to be typed in. It would only see the FIRST name. Right? I did it before, but I could try again I guess.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    ah, yes, you're right. I think this is a quirk of the nextLine() method. Here's an explanation (half way down about the nextLine() method):
    Scanner class

    Luck,
    CJSL
    Last edited by CJSLMAN; 03-17-2009 at 11:59 PM. Reason: fix some fat fingered words
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

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

    Default

    How about...
    Java Code:
        transMenu = input.nextLine();
        String zipCode = "";
        String myName = "";
        if (!transMenu.isEmpty() && transMenu.toUpperCase().charAt(0) == 'Z')
        {

  6. #6
    Xystus777 is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    Thanks Fubarable, that might work. As of right now, I just did:

    String myName = input.nextLine();
    myName = input.nextLine();

    I don't know why, but for some reason, if there's two of them, it works just fine. But your way would probably work and I'll probably implement it later. By the way Fubarable, I don't know if you read my project from yesterday or not, but it appears we got just about everything to work great now :).

Similar Threads

  1. Scanner
    By choko in forum New To Java
    Replies: 10
    Last Post: 01-24-2009, 03:37 PM
  2. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  3. need help with scanner
    By whiterex in forum New To Java
    Replies: 1
    Last Post: 04-22-2008, 01:41 PM
  4. Scanner class
    By ajaymenon.k in forum Advanced Java
    Replies: 1
    Last Post: 11-26-2007, 07:01 AM
  5. help with IP scanner
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:00 PM

Posting Permissions

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