Results 1 to 2 of 2
  1. #1
    atac57 is offline Member
    Join Date
    Jan 2012
    Posts
    24
    Rep Power
    0

    Default Not able to enter input with String variable set to Scanner

    I just wrote this to practice and for some reason when I run it it won't let me enter input on "answer = scan.nextLine();" after the second print statement. Appreciate the help.

    Java Code:
    import java.util.Scanner;
    
    public class FirstProgram {
    	
    	public static void main(String args[]){
    		
    		Scanner scan = new Scanner(System.in);
    		
    		double balance, withdraw, deposit;
    		String answer;
    		
    		System.out.println("What is your bank balance?");
    		balance = scan.nextDouble();
    		
    		System.out.println("Would you like you make a withdraw, or a deposit?");
    		answer = scan.nextLine();
    		
    		if(answer.equalsIgnoreCase("withdraw"))
    		{
    			System.out.println("How much would you like to withdraw?");
    			withdraw = scan.nextDouble();
    			
    			balance = balance - withdraw;
    			System.out.println("Your current balance is: " + balance);
    				
    		}
    		else if (answer.equalsIgnoreCase("deposit"))
    		{
    			System.out.println("How much would you like to deposit?");
    			deposit = scan.nextDouble();
    			
    			balance = balance + deposit;
    			System.out.println("Your current balance is: " + balance);
    			
    		}
    			
    	}
    }

  2. #2
    atac57 is offline Member
    Join Date
    Jan 2012
    Posts
    24
    Rep Power
    0

    Default Re: Not able to enter input with String variable set to Scanner

    Ok, it looks like .next() without the "Line" is what works, but I've never had that problem before, nextLine(), would always work. Can anyone explain that?

Similar Threads

  1. enter a string sentence
    By amorosa19 in forum New To Java
    Replies: 13
    Last Post: 09-06-2013, 11:34 AM
  2. Replies: 2
    Last Post: 01-10-2012, 02:10 PM
  3. That old problem with scanner and end of line (enter)
    By Java Riser in forum New To Java
    Replies: 2
    Last Post: 09-29-2011, 02:54 AM
  4. End Scanner int input with text value
    By BillyB in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 06:13 PM
  5. how to enter input method name for getMethod(------)
    By Dayanand in forum New To Java
    Replies: 3
    Last Post: 02-23-2011, 07:07 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
  •