Forums Register Login

Read data?

+Pie Number of slices to send: Send
I've got a huge problem. Our teacher told us to use this line to read data:
Scanner scan = new Scanner(System.in);

And then to read numbers = scan.nextInt();
And to read Strings = scan.nextLine();

And now i got a mission to make a program to change letters to numbers. a should be 97 and so on. I wrote this script:

public class Ex33 {
public static void main(String[] args){
Scanner Keyboard = new Scanner(System.in);
char tecken = 'a';
System.out.println("Tecken = " + tecken);
System.out.print("Enter a new sign: ");
tecken = Keyboard.readChar();
System.out.println("Tecken = " + tecken);

}
}

The only problem is that the class Scanner doesint support char at all, so im wondering if anyone know what class to use to read data, since Scanner fails to support char and therefor also fails to support switch and so on.

Please help me //Confused student
+Pie Number of slices to send: Send
Hi Johannes,

Read Strings with nextLine(), and then use the methods of java.lang.String to extract the characters (or perhaps just the first character). Check out the method "charAt()" in the Javadocs for String.
+Pie Number of slices to send: Send
I get the error:
The method charAt() is undefined for the type Scanner

i seams like Scanner can just read string, int, double and not char. does anyone know any other class i can use to read char?

Also i accountered a problem with swing.
I got an assignment to make a "bank account" to give a menue with
1. Deposit money
2. Withdraw money
3. Account information
4. exit

And i thought i could use this:
String yourchoise = null;

yourchoise = JOptionPane.showInputDialog("1.Deposit money"+ \n +"2.Withdraw money"... and so on);

if(yourchoise = 1){
JOptionPane.showInputDialog("How much do you want to deposit?");
} else
if(yourchoise = 2)~ and so on

But i get the error that yourchoise need to be a boolean in the if thing, and that it needs to be a int if i should have the numbers. Whats the "JOptionPane.showInputDialog();" for int or double?
Or does anyone got an idea of what to do?
+Pie Number of slices to send: Send
 

The method charAt() is undefined for the type Scanner



Reread EFH posts again. The charAt() method is part of the String class. He was recommending that you read in a string and process the chars in the string -- not read in the chars.

Henry
+Pie Number of slices to send: Send


Well, you have two issues here (three if you count the fact that you should *not* combine topics like this).

First, just because the string holds an int doesn't mean that it is interchangeable. You still have to check and then parse the string to an integer. Second, a single equals is used for assignments. Is this what you want?

Henry
[ November 24, 2008: Message edited by: Henry Wong ]
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1347 times.
Similar Threads
Using percent Modulus to calculate total number of even numbers from user input
Some help with a problem
Java 1.5 on mac
How to validate a Scanner keyboard input?
A question about Scanner
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:12:14.