aspose file tools*
The moose likes Beginning Java and the fly likes Read data? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Read data?" Watch "Read data?" New topic
Author

Read data?

Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
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
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
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?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18714
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18714
    
  40



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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Read data?