Computer Science Canada Inputting Strings |
Author: | ProgrammingFun [ Tue Sep 21, 2010 5:46 pm ] | ||
Post subject: | Inputting Strings | ||
Hello, I have just started Java this year and am facing problems with inputting strings. How can I declare a variable and store a string inside it (like in Turing)? I have tried the following...
but am given the following error: Quote: Budget.java:18: cannot find symbol symbol : variable input location: class Budget name = input.next(); ---------^ Is there any other way to store strings? Please note that I will need multiple variables (as in the example), some to store strings, and others for doubles. An answer is needed A.S.A.P! Thanks |
Author: | TerranceN [ Tue Sep 21, 2010 6:11 pm ] |
Post subject: | RE:Inputting Strings |
You named the scanner "money" not "input", so you would have to write money.next() instead. Also, in order to convert a double to a string you can use Double.parseDouble(String s). Hope that helps. |
Author: | ProgrammingFun [ Tue Sep 21, 2010 6:21 pm ] | ||||
Post subject: | Re: RE:Inputting Strings | ||||
TerranceN @ Tue Sep 21, 2010 6:11 pm wrote: You named the scanner "money" not "input", so you would have to write money.next() instead. Also, in order to convert a double to a string you can use Double.parseDouble(String s).
Hope that helps. Thank you so much!!! I have one other question (might as well include it in this thread). I am confused about casting, I was trying to answer the following question but continuosly run into an error. Question: Write a program that prompts the user for five grades and then displays the average of the grades. The grades are integers and they must be stored in variables of type int. Real division should be performed when calculating the average. Attempted answer:
|
Author: | TerranceN [ Tue Sep 21, 2010 7:32 pm ] |
Post subject: | RE:Inputting Strings |
Since sum is type int it cannot have decimal places, same with avg. Double on the other hand, can. What you need to know about math operations in java to answer this question, is that when there is an operation on two values of different precision, the higher precision type is the result. For example 7 / 2 (int / int) would give you 3 but 7 / 2.0 (int / double) would give you 3.5. Because double is the higher precision of the two, it is what is used in the operation |
Author: | ProgrammingFun [ Tue Sep 21, 2010 9:02 pm ] |
Post subject: | Re: RE:Inputting Strings |
TerranceN @ Tue Sep 21, 2010 7:32 pm wrote: Since sum is type int it cannot have decimal places, same with avg. Double on the other hand, can.
What you need to know about math operations in java to answer this question, is that when there is an operation on two values of different precision, the higher precision type is the result. So then how would I go about using real division using casting (while storing the variables as int). Casting is a must in this question. |
Author: | TerranceN [ Tue Sep 21, 2010 9:54 pm ] |
Post subject: | RE:Inputting Strings |
First of all, the average variable can't be an int or you will get the same thing as if you did integer division (in both cases everything past the decimal is cut off). Anyway, in what I said before one of the 2 values must be double in order to get a double, so you can just cast the variable to a double. int x = 7; double y = (double)x / 2; // results in 3.5 |
Author: | ProgrammingFun [ Wed Sep 22, 2010 7:07 pm ] |
Post subject: | RE:Inputting Strings |
Thanks for the help! |