954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Question about the Scanner class

Can someone look at my program. I am having a problem with my Scanner line. Does anyone know how to fix this problem??

I keep getting an error at this part:
letter = scan.nextLine();

import java.util.Scanner;

public class ATMdriver
{
	public static void main(String[] args)
		{
	ATM atm=new ATM();
	char letter;
	int timePass;
	
	
	do
	{
	System.out.println();
	System.out.println("What would you like to do ?");
	System.out.println("W - Withdraw money from the ATM");
	System.out.println("T - Add twenties to the ATM");
	System.out.println("F - Add fives to the ATM");
	System.out.println("Q - Quit");

	System.out.println("");
	Scanner scan = new Scanner (System.in);
	letter = scan.nextLine();
	
	
	System.out.println("");
	switch(letter)
	{
	case 'W':
		case 'w':
		atm.withdraw();
		System.out.println(atm);
		break;
	case 'T':
		case 't':
		atm.addTwenties();
		System.out.println(atm);
		break;
	case 'F':
		case 'f':
		atm.addFives();
		System.out.println(atm);
		break;

	case 'Q':
		case 'q':
		System.out.println(atm);
		break;
	default:
		System.out.println("Not a valid command. Please reenter.");
		break;
	}
	}while((letter!='q')&&(letter!='Q'));

}
}

Code tags added. -Narue

jengels
Newbie Poster
22 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

I've never heard of the Scanner class... So I went to the api... and I couldn't find it. But if your goal is to just allow the user to input a letter from the console, then you can use the

import java.io.*;

InputStreamReader isReader = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(isReader);


and then when you want the user to input something, just use:

String input = reader.readLine();


-Nick Nisi

stupidenator
Junior Poster
192 posts since Mar 2005
Reputation Points: 18
Solved Threads: 4
 

Scanner is a new class in 1.5, maybe you were looking at the 1.4 API docs?

I've not used it myself so I can't really help you at this stage.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Scanner.nextLine() is a String, NOT a char.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

How should I change the switch to make it work?

jengels
Newbie Poster
22 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

I would use if else statements since you only have a couple of tests to perform.
Using if else, you could also cut down on the code for testing uCase and lowercase characters:

if (letter.equals("T") || letter.equals("t"))
{
   //calculations and stuff
}
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

change this

letter = scan.nextLine();


to this:

letter = scan.nextLine().charAt(0);


this reads in a string, and plucks off the first character

or better yet, replace it with this:

letter = scan.nextLine().toLowerCase().charAt(0);


toLowerCase and charAt(0) are both methods from java.lang.String

this will automatically format the char to be lower case, so in your switch statment you don't have to check for case 'W' and case 'w' just take for one lower case

paradox814
Posting Whiz
351 posts since Oct 2004
Reputation Points: 13
Solved Threads: 4
 

one last thing, after you do-while loop you should close your scanner

scan.close();


it will be closed by the jvm even if you don't, but it's still good practice to close any connections you open

paradox814
Posting Whiz
351 posts since Oct 2004
Reputation Points: 13
Solved Threads: 4
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You