| Author |
Message |
wtd
|
Posted: Mon May 01, 2006 4:53 pm Post subject: (No subject) |
|
|
The question you should be asking is, how can I convert a string into an integer. If you can read in a string, and you can convert a string to an integer, then logic dictates that you can read in an integer, right?  |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
cool dude

|
Posted: Mon May 01, 2006 5:53 pm Post subject: (No subject) |
|
|
true. i just thought like in turing, Visual basic u can get input which can be both an integer or a string depending on your variable type. so if i declared a variable type as int it should allow me to get input as integer. i guess u can't do that in java, i might be wrong i dunno? if i am wrong which i presume than how do u convert a string to an integer?
p.s. does someone know a good link that gives java syntax because i know how to program (hopefully) i just don't know the syntax |
|
|
|
|
 |
Tony

|
Posted: Mon May 01, 2006 6:00 pm Post subject: (No subject) |
|
|
| Sun's resourses for Java are pretty [url=http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html#Integer(java.lang.String)]awesome[/url] |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
[Gandalf]

|
Posted: Mon May 01, 2006 11:24 pm Post subject: (No subject) |
|
|
| Exactly what you are asking for is perfectly possible using the Scanner class's [url=http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html#nextInt()]nextInt()[/url] method. |
|
|
|
|
 |
cool dude

|
Posted: Tue May 02, 2006 4:37 pm Post subject: (No subject) |
|
|
[Gandalf] wrote: Exactly what you are asking for is perfectly possible using the Scanner class's [url=http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html#nextInt()]nextInt()[/url] method.
it seems easier but i keep getting the error "cannot resolve symbol - class scanner" and the line that is highlighted is
| code: |
Scanner in = new Scanner(System.in); |
can u please tell me wats wrong with that, because thats the code u wrote. also does it matter which one i use i.e. the scanner or the buffered one? |
|
|
|
|
 |
[Gandalf]

|
Posted: Tue May 02, 2006 4:49 pm Post subject: (No subject) |
|
|
Are you using Java 5.0? Have you imported java.util.*?
If you are using a previous version of the SDK you will have to use the other mentioned method, yes. Otherwise, I personally recommend you use the Scanner class for both keyboard and file input. |
|
|
|
|
 |
cool dude

|
Posted: Tue May 02, 2006 4:58 pm Post subject: (No subject) |
|
|
| i just started java so i might be doing something wrong although i pretty much copied your code. i'm using Blue J and yes i imported java.util.* did u test your code yourself? because i think u mentioned that u didn't test it so it might not actually work? i do wanna use your way though because it is easier to remember so can u test it out and tell me if its wrong? |
|
|
|
|
 |
HellblazerX

|
Posted: Tue May 02, 2006 5:01 pm Post subject: (No subject) |
|
|
| If that's the case, then it probably is because you don't have Java 1.5.0. Otherwise, it wouldn't give an error like the one you got. I suggest you use the other method with BufferedReader. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
[Gandalf]

|
Posted: Tue May 02, 2006 5:05 pm Post subject: (No subject) |
|
|
| Indeed, are you sure you have Java 5.0 (1.5.0)? I have tested the code, it works. If you're not sure which version of Java you have (maybe it was installed packaged with BlueJ), check the Java folder (probably) in Program Files/Java. |
|
|
|
|
 |
cool dude

|
Posted: Tue May 02, 2006 6:16 pm Post subject: (No subject) |
|
|
[Gandalf] wrote: Indeed, are you sure you have Java 5.0 (1.5.0)? I have tested the code, it works. If you're not sure which version of Java you have (maybe it was installed packaged with BlueJ), check the Java folder (probably) in Program Files/Java.
k i checked my version and it is 1.5.0 so it should work right? also just to be certain i went to reinstall it here https://sdlc3a.sun.com/ECom/EComActionServlet;jsessionid=9617D14BBA459EED110ED9CE11C0AA6D |
|
|
|
|
 |
cool dude

|
Posted: Tue May 02, 2006 6:24 pm Post subject: (No subject) |
|
|
| do u have to declare something like in the BufferedReader code? |
|
|
|
|
 |
Krabjuice
|
Posted: Tue May 02, 2006 7:50 pm Post subject: (No subject) |
|
|
Scanner in 1.5 only requires that you import the Scanner library: java.util.Scanner
Then, you simply need to create a Scanner object. You already did that, its:
| code: | Scanner <name> = new Scanner (System.in); |
You can use various methods, ie:
| code: | <name>.next() //Get single word, string
<name>.nextLine() //Get a Line of string
<name>.nextInt() //Get a Integer
<name>.Double() //Get a Double
//and so on.
|
|
|
|
|
|
 |
cool dude

|
Posted: Tue May 02, 2006 8:31 pm Post subject: (No subject) |
|
|
| wow it seems really simple i really wanna be able to use this method. can someone please tell me wat can possibly be wrong when it says "cannot resolve symbol - class scanner". when i click on the question mark it says that "you are using a method here that has not been declared in any visible scope. check the spelling of the name did u mistype it? or did you forget to declare it? or maybe you did declare it but it is not visible from here" it seems like i'm using the correct version of java so wat can i do? |
|
|
|
|
 |
wtd
|
Posted: Tue May 02, 2006 8:40 pm Post subject: (No subject) |
|
|
| If we could see your code, plain and simple, that would help us diagnose problems. |
|
|
|
|
 |
Tony

|
Posted: Tue May 02, 2006 8:42 pm Post subject: (No subject) |
|
|
cool dude wrote: "cannot resolve symbol - class scanner"
Scanner is capitilized, right? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
|