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

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

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.

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

How should I change the switch to make it work?

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
}

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.