Computer Science Canada input |
Author: | cool dude [ Sun Apr 30, 2006 1:46 pm ] |
Post subject: | input |
i'm just starting to learn Java alone. i checked out wtd tutorial which was very helpful ![]() |
Author: | Tony [ Sun Apr 30, 2006 2:22 pm ] |
Post subject: | |
rizzix wrote a tutorials on reading / writing in Java |
Author: | cool dude [ Sun Apr 30, 2006 3:36 pm ] |
Post subject: | |
i don't think u understood my question correctly. i want to know how to get input from the user. NOT from a file. just like in turing u say get so how do u do that in java. i'm just starting out i have no clue |
Author: | Tony [ Sun Apr 30, 2006 4:26 pm ] |
Post subject: | |
I'm pretty sure that the code provided in the tutorial is for reading the keyboard input.. ![]() |
Author: | cool dude [ Sun Apr 30, 2006 5:48 pm ] |
Post subject: | |
k u might be right because i'm just starting to learn java but i really can't understand it. what is the import java.io mean? and is it something i just need to memorize? also how come they are talking about files if u keep looking at the rest of the posts below if u said thats input from user. wat i mean is like what is your name? and then the user can answer and then it can say hello user. for some reason even using that code i can't do such a simple program. if i copy and paste that code it says identifier expected, and i have no clue wat that means since i just started learning java on my own. |
Author: | Tony [ Sun Apr 30, 2006 6:21 pm ] | ||
Post subject: | |||
java.io is the I/O class - Input/Output, you import it into the program to be able to use its functionality the basic program would look something like
I think... ![]() |
Author: | wtd [ Sun Apr 30, 2006 6:46 pm ] |
Post subject: | |
java.io is a package which includes classes used in I/O operations. |
Author: | cool dude [ Sun Apr 30, 2006 7:27 pm ] | ||
Post subject: | |||
wow thanks that helped! ![]() the only things i don't get is firstly what is try and catch for and what does this line mean?
|
Author: | Krabjuice [ Sun Apr 30, 2006 7:49 pm ] |
Post subject: | |
Are you using Java 1.5? How about using the Scanner construct? Its pretty quick and easy. |
Author: | Tony [ Sun Apr 30, 2006 8:31 pm ] |
Post subject: | |
wtd wrote: java.io is a package
you're absolutely right, thx cool dude wrote: what is try and catch for
if there's any error inside the try block, catch will be executed instead. Since reading input is prone to screwups (wrong input? eof? etc) it is usually a good idea to place it within try/catch the code declears the buffer you're reading from and outputting to. Actually out is just an alias for System.out so out.println is same as System.out.println |
Author: | [Gandalf] [ Sun Apr 30, 2006 9:01 pm ] | ||
Post subject: | |||
Much simpler using the Scanner class:
Hope I didn't make any stupid/obvious mistakes, don't have Java SDK installed here. |
Author: | Tony [ Sun Apr 30, 2006 9:05 pm ] |
Post subject: | |
the only difference is the declaration of the input buffer to read from really.. I think it should still be inside try/catch setup for the sake of good programming practice. the bottom line is that when attempting to execute you're going to recieve an error message from the catch block and not have an exception escalate and crash the entire application. |
Author: | wtd [ Sun Apr 30, 2006 9:51 pm ] |
Post subject: | |
No... good programming practice is not to dismiss errors unless you have something meaningful to do. |
Author: | MysticVegeta [ Mon May 01, 2006 1:45 pm ] | ||||
Post subject: | |||||
Tony wrote: java.io is the I/O class - Input/Output, you import it into the program to be able to use its functionality
the basic program would look something like
I think... ![]() I think you can just do this to reduce the try and catch statements:
Notice the "throws IOException" reduces your lines, but one disadvantage is that you cant output any message "you" would like when the try fails |
Author: | cool dude [ Mon May 01, 2006 4:34 pm ] |
Post subject: | |
k i got this working with any input that is a string but how can i get it to work with integers? |
Author: | wtd [ Mon May 01, 2006 4:53 pm ] |
Post 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? ![]() |
Author: | cool dude [ Mon May 01, 2006 5:53 pm ] |
Post 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 |
Author: | Tony [ Mon May 01, 2006 6:00 pm ] |
Post 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] |
Author: | [Gandalf] [ Mon May 01, 2006 11:24 pm ] |
Post 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. |
Author: | cool dude [ Tue May 02, 2006 4:37 pm ] | ||
Post 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
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? |
Author: | [Gandalf] [ Tue May 02, 2006 4:49 pm ] |
Post 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. |
Author: | cool dude [ Tue May 02, 2006 4:58 pm ] |
Post 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? |
Author: | HellblazerX [ Tue May 02, 2006 5:01 pm ] |
Post 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. |
Author: | [Gandalf] [ Tue May 02, 2006 5:05 pm ] |
Post 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. |
Author: | cool dude [ Tue May 02, 2006 6:16 pm ] |
Post 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 |
Author: | cool dude [ Tue May 02, 2006 6:24 pm ] |
Post subject: | |
do u have to declare something like in the BufferedReader code? |
Author: | Krabjuice [ Tue May 02, 2006 7:50 pm ] | ||||
Post 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:
You can use various methods, ie:
|
Author: | cool dude [ Tue May 02, 2006 8:31 pm ] |
Post 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? |
Author: | wtd [ Tue May 02, 2006 8:40 pm ] |
Post subject: | |
If we could see your code, plain and simple, that would help us diagnose problems. |
Author: | Tony [ Tue May 02, 2006 8:42 pm ] |
Post subject: | |
cool dude wrote: "cannot resolve symbol - class scanner"
Scanner is capitilized, right? |
Author: | cool dude [ Tue May 02, 2006 8:53 pm ] | ||
Post subject: | |||
yes Scanner is capitalized! here is my code
maybe i don't have the right version ![]() |
Author: | [Gandalf] [ Tue May 02, 2006 9:15 pm ] |
Post subject: | |
Yes, it works. I believe this is one of the reasons why wtd doesn't recommend IDEs, especially for the beginner. It's much simpler and more straight forward to just download the SDK from java.sun.org and user a text editor and the command line to do your programming. |
Author: | cool dude [ Tue May 02, 2006 9:22 pm ] |
Post subject: | |
unforunately i can see i'm not going to be using your way. ![]() ![]() ![]() |
Author: | cool dude [ Tue May 02, 2006 9:31 pm ] | ||
Post subject: | |||
nm finally figured how how to convert them!
right? and wat does parseInt mean? p.s. how do i error proof it so that if the user enters a letter it won't crash? |
Author: | wtd [ Tue May 02, 2006 9:34 pm ] | ||
Post subject: | |||
cool dude wrote: nm finally figured how how to convert them!
right? and wat does parseInt mean? p.s. how do i error proof it so that if the user enters a letter it won't crash? First off: variable names should not ever ever ever begin with a capital letter. Second: what does "parse" mean? Look it up in a dictionary. ![]() Lastly: What happens if a user does enter something other than a valid number? |
Author: | cool dude [ Tue May 02, 2006 10:05 pm ] | ||
Post subject: | |||
wtd wrote: cool dude wrote: nm finally figured how how to convert them!
right? and wat does parseInt mean? p.s. how do i error proof it so that if the user enters a letter it won't crash? First off: variable names should not ever ever ever begin with a capital letter. Second: what does "parse" mean? Look it up in a dictionary. ![]() Lastly: What happens if a user does enter something other than a valid number? 1) may i ask why variable names should never start with a capital letter? this was just a sample program i made i'm usually too lazy to always type in capitals but i still wanna know the reasoning? 2) is that a rhetorical question? because i asked u how can i error proof that if the user enters something thats not a valid number? |
Author: | Tony [ Tue May 02, 2006 10:11 pm ] |
Post subject: | |
1 - capitalized names are for class names: String, Integer, Scanner, etc 2 - it's not a rhetorical question. Find out what happens when the user enters something that is not a valid number. (hint: try it out with your code) |
Author: | cool dude [ Tue May 02, 2006 10:20 pm ] |
Post subject: | |
k i don't think u guys understood me. i know wat happens thats why i posted my question!!! how can i error proof that? how can i fix it from crashing when u enter a letter or anything but a number. |
Author: | wtd [ Tue May 02, 2006 10:28 pm ] |
Post subject: | |
Java programs don't generally just crash. What is actually happening when invalid input occurs? |
Author: | cool dude [ Tue May 02, 2006 10:29 pm ] | ||
Post subject: | |||
finally figured out how to error proof that ![]()
now the only thing i don't understand is after NumberFormatException there is "nfe" what does that mean? and is it something i just need to memorize? |
Author: | [Gandalf] [ Tue May 02, 2006 10:43 pm ] | ||
Post subject: | |||
nfe can be renamed to almost anything else (any value variable name), because that's what it is. It contains the NumberFormatException that is being caught. For example you could have:
Try it out! |
Author: | wtd [ Tue May 02, 2006 10:46 pm ] |
Post subject: | |
Perhaps you should read up on exception handling a bit more. ![]() |
Author: | cool dude [ Wed May 03, 2006 4:59 pm ] | ||||
Post subject: | |||||
k i don't know if i should make new posts everytime because i'm just starting out and i have lots of questions so if u guys think its better to make new posts than i will! ![]() aside from that little note this is my question 1)what is the difference between = and == 2)in my if statement i tried saying:
but this gives me an error saying incompatible types although i declared operation as string so it should be the same types right? the only way i could fix it is by saying
can someone tell me why i can't use the equal sign (=)? 3) on error handling i have it working although if it is an error it says whatever message i want and then stops the program. how can i make it just go back and keep asking the question until they type something valid? would i need a loop for that? |
Author: | wtd [ Wed May 03, 2006 5:04 pm ] |
Post subject: | |
The = operator is used for assignment. The == operator is used to check for equality in primitive types (int, float, boolean, double, long, etc.). In object types, it checks for referential equality. Do the two variables point to the same object in memory. The equals methods checks for equality in object types. Of what type is the "operation" variable? |
Author: | Tony [ Wed May 03, 2006 5:05 pm ] | ||||||
Post subject: | |||||||
Edit: wtd is obviously a faster typer re: = sign It's used for assigning a value, as you do in
if is looking for a boolean operator to evaluate in
you assign the value of "A" to variable operator. Though the action does not return a boolean value.
returns boolean true/false, something that if() understands. == is used for comparison, though careful as you'll likely make the mistake of comparing one instance of a class to another. Even if they are of the same type and hold identical values, they will not be the "same" and you'll consistently get "false" for a result |
Author: | [Gandalf] [ Wed May 03, 2006 5:09 pm ] | ||
Post subject: | |||
Questions are good, so long as you are also able to find the answers on your own.
This is some very basic and essential knowledge. Perhaps you should read wtd's introduction to java thread. |
Author: | wtd [ Wed May 03, 2006 5:17 pm ] | ||
Post subject: | |||
Tony wrote: if is looking for a boolean operator to evaluate
in
I think that should be: Quote: if is looking for a boolean expression to evaluate
The operator is irrelevant, as long as the expression returns a boolean value. |
Author: | wtd [ Wed May 03, 2006 5:18 pm ] | ||
Post subject: | |||
[Gandalf] wrote: Questions are good, so long as you are also able to find the answers on your own.
This is much too basic a comparison. The Java operators are considerably more nuanced in their behavior than those from Turing highlighted here. |