File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read data?

 
Johannes Thorén
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Johannes Thorén
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20820
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20820
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic