Inputting Strings
Author |
Message |
ProgrammingFun

|
Posted: 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...
Java: |
import java.awt.*;
import java.util.*;
public class Budget
{
public static void main (String[] args )
{
Scanner money = new Scanner (System. in);
String name = "";
String s1 = "";
String s2 = "";
String s3 = "";
String s4 = "";
double birth_year, g1, g2, g3, g4;
System. out. print ("Enter your first name: ");
name = input. next();
System. out. print ("Enter your age: ");
birth_year = input. next();
}
}
|
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 |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
TerranceN
|
Posted: 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. |
|
|
|
|
 |
ProgrammingFun

|
Posted: 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:
Java: |
import java.awt.*;
import java.util.*;
public class Gradeaverage
{
public static void main (String[] args )
{
Scanner grades = new Scanner (System. in);
int g1, g2, g3, g4, g5, sum, avg;
System. out. print ("Please enter the first grade: ");
g1 = grades. nextInt();
System. out. print ("Please enter the second grade: ");
g2 = grades. nextInt();
System. out. print ("Please enter the third grade: ");
g3 = grades. nextInt();
System. out. print ("Please enter the fourth grade: ");
g4 = grades. nextInt();
System. out. print ("Please enter the fifth grade: ");
g5 = grades. nextInt();
sum = (double) (g1 + g2 + g3 + g4 + g5 );
avg = avg / 5;
System. out. println ("Your average is: " + avg );
}
}
|
Error: |
Gradeaverage.java:26: possible loss of precision
found : double
required: int
sum = (double) (g1 + g2 + g3 + g4 + g5);
^
1 error
|
|
|
|
|
|
 |
TerranceN
|
Posted: 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 |
|
|
|
|
 |
ProgrammingFun

|
Posted: 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. |
|
|
|
|
 |
TerranceN
|
Posted: 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 |
|
|
|
|
 |
ProgrammingFun

|
Posted: Wed Sep 22, 2010 7:07 pm Post subject: RE:Inputting Strings |
|
|
Thanks for the help! |
|
|
|
|
 |
|
|