Results 1 to 16 of 16
Thread: what is the problem?
- 02-25-2009, 04:44 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
what is the problem?
this is the question:
(Metric Conversion Application ) Write an application that will assist the user with metric conversions. Your application should allow the user to specify the names of the units as strings(i.e., centimeters, liters, grams, and so on, for the metric system and inches, quarts, pounds, and so on, for the English system) and should respond to simple questions, such as
“How many inches are in 2 meters?”
“How many liters are in 10 quarts?”
Your application should recognize invalid conversions. For example, the question
“How many feet are in 5 kilograms?”
is not meaningful because “feet” is a unit of length, whereas “kilograms” is a unit of mass.
But i not really understand the question...is it true i do like this?there are too many errors....can i use charAt[i] to detect the third word of a line?or hava another method?
import java.util.Scanner;
import java.util.StringTokenizer;
public class MetricConversion {
public static void main(String[] args) {
double inches;
double meters;
double liters;
double quarts;
double grams;
double pounds;
int num;
Scanner scanner = new Scanner(System.in);
System.out.println(" Enter A Metric Conversion Question.");
String s = scanner.nextLine();
StringTokenizer tokenizer = new StringTokenizer(s);
while (tokenizer.hasMoreTokens()){
if( charAt[3] == inches) {
if( charAt[7] == meters)
inches = meters / 0.0254;
System.out.println(" They are " + inches + " inches in " + num + " meters ");
}
else
System.out.println(" This question is invalid conversion.");
if( charAt[3] == meters) {
if(charAt[7] == inches)
meters = inches * 0.0254;
System.out.println(" They are " + meters + " meters in " + num + " inches ");
}
else
System.out.println(" This question is invalid conversion.");
if( charAt[3] == liters) {
if( charAt[7] == quarts)
liters = quarts / 0.0254;
System.out.println(" They are " + liters + " liters in " + num + " quarts ");
}
else
System.out.println(" This question is invalid conversion.");
if( charAt[3] == quarts) {
if( charAt[7] == liters)
quarts = liters * 0.0254;
System.out.println(" They are " + quarts + " quarts in " + num + " liters ");
}
else
System.out.println(" This question is invalid conversion.");
if( charAt[3] == grams) {
if( charAT[7] == pounds)
grams = pounds / 0.0254;
System.out.println(" They are " + grams + " grams in " + num + " pounds ");
}
else
System.out.println(" This question is invalid conversion.");
if( charAt[3] == pounds) {
if( charAt[7] == grams)
pounds = grams / 0.0254;
System.out.println(" They are " + pounds + " pounds in " + num + " grams ");
}
else
System.out.println(" This question is invalid conversion.");
}
}
}
- 02-25-2009, 05:24 AM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
what charAt is?
i think you should use String.split(" ") instead of StringTokenizer
so that you can use Sting[] chartAt = xxx.split(" ")
and for codes like following
inches should be a String, not a variable name, and please use xxx.equals("yyy") for checkingJava Code:if( charAt[3] == inches)
also, array index start from 0
you do not initialize, assign value to variables like meters....Last edited by mtyoung; 02-25-2009 at 05:55 AM.
- 02-25-2009, 05:58 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
pls help me!!
can u help me to correct this programming? i really dont how to correct this? thanks!
-
Why not first try his suggestions, at least attempt to try, and come back with your code?
- 02-25-2009, 07:04 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
i ard try but still got errors. the operator / cannot be applied to java.lang.String,double . what is mean?
actually i dont understand this question want i do what? want respond the conversion whether valid o invalid? or also want me to answer how many inches are in 6 meters?
import java.util.Scanner;
import java.util.StringTokenizer;
public class MetricConversion {
public static void main(String[] args) {
String inches;
String meters;
String liters;
String quarts;
String grams;
String pounds;
int num;
Scanner scanner = new Scanner(System.in);
System.out.println(" Enter A Metric Conversion Question.");
String s = scanner.nextLine();
StringTokenizer tokenizer = new StringTokenizer(s);
while (tokenizer.hasMoreTokens()){
if( inches == meters) {
inches = meters / 0.0254;
System.out.println(" They are " + inches + " inches in " + num + " meters ");
}
else
System.out.println(" This question is invalid conversion.");
if( meters == inches) {
meters = inches * 0.0254;
System.out.println(" They are " + meters + " meters in " + num + " inches ");
}
else
System.out.println(" This question is invalid conversion.");
if(liters == quarts) {
liters = quarts / 0.0254;
System.out.println(" They are " + liters + " liters in " + num + " quarts ");
}
else
System.out.println(" This question is invalid conversion.");
if(quarts == liters ) {
quarts = liters * 0.0254;
System.out.println(" They are " + quarts + " quarts in " + num + " liters ");
}
else
System.out.println(" This question is invalid conversion.");
if(grams == pounds ) {
grams = pounds / 0.0254;
System.out.println(" They are " + grams + " grams in " + num + " pounds ");
}
else
System.out.println(" This question is invalid conversion.");
if(pounds == grams) {
pounds = grams / 0.0254;
System.out.println(" They are " + pounds + " pounds in " + num + " grams ");
}
else
System.out.println(" This question is invalid conversion.");
}
}
}
- 02-25-2009, 07:25 AM #6
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
operator / cannot be applied to java.lang.String,double
means String variable can be divided
and
what do you want to do?Java Code:while (tokenizer.hasMoreTokens()){ if( inches == meters) { inches = meters / 0.0254; System.out.println(" They are " + inches + " inches in " + num + " meters "); } else System.out.println(" This question is invalid conversion."); if( meters == inches) { meters = inches * 0.0254; System.out.println(" They are " + meters + " meters in " + num + " inches "); }
assume user input "How many inches are in 2 meters?”i suggest you to use xxx = string.split(" ") method... as we can access single word like xxx[2] directly...
xxx[0] will be "How", xxx[1] will be "many", so on,
then you only need to check xxx[?] are String in "inches", "meters" etc
and xxx[??] is "2", "3", "4.2"
and xxx[???] is String in "inches", "meters" etc
check them are correct input or not
for xxx[??], you can use Double.parseDouble method to covert string to double data type and do the calculation
** remeber to handle NumberFormatException
- 02-25-2009, 11:33 AM #7
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
can i use the split method like this?
got this error
C:\Documents and Settings\user\My Documents\Question2.java:22: ']' expected
String a[i]= s.split(" ");
^
C:\Documents and Settings\user\My Documents\Question2.java:26: ']' expected
double a[5]=Double.parseDouble(s);
^
2 errors
Tool completed with exit code 1
import java.util.Scanner;
public class Question2 {
public static void main(String[] args) {
double inches;
double meters;
double liters;
double quarts;
double grams;
double pounds;
int num;
Scanner scanner = new Scanner(System.in);
System.out.println(" Enter A Metric Conversion Question.");
String s = scanner.nextLine();
for(int i=0;i<s.length();i++){
String a[i]= s.split(" ");
}
double a[5]=Double.parseDouble(s);
if( a[3].equals("inches")) {
if( a[7].equals("meters"))
inches = a[5]/ 0.0254;
System.out.println(" They are " + inches + " inches in " +a[5]+ " meters ");
}
else
System.out.println(" This question is invalid conversion.");
if( a[3].equals("meters")) {
if(a[7] .equals("inches"))
meters = a[5] * 0.0254;
System.out.println(" They are " + meters + " meters in " +a[5]+ " inches ");
}
else
System.out.println(" This question is invalid conversion.");
if( a[3] .equals("liters")) {
if( a[7].equals("quarts"))
liters = a[5] / 0.0254;
System.out.println(" They are " + liters + " liters in " +a[5]+ " quarts ");
}
else
System.out.println(" This question is invalid conversion.");
if( a[3] .equals("quarts")) {
if( a[7].equals("liters"))
quarts = a[5] * 0.0254;
System.out.println(" They are " + quarts + " quarts in " +a[5]+" liters ");
}
else
System.out.println(" This question is invalid conversion.");
if( a[3].equals("grams")) {
if( a[7].equals("pounds"))
grams = a[5] / 0.0254;
System.out.println(" They are " + grams + " grams in " +a[5]+ " pounds ");
}
else
System.out.println(" This question is invalid conversion.");
if( a[3] .equals("pounds")) {
if( a[7] .equals("grams"))
pounds = a[5] / 0.0254;
System.out.println(" They are " + pounds + " pounds in " +a[5]+ " grams ");
}
else
System.out.println(" This question is invalid conversion.");
}
}Last edited by li_yun88; 02-25-2009 at 12:18 PM.
- 02-25-2009, 02:23 PM #8
I think mtyoung had a typo...
The following can't work:means String variable can't be divided
You can't use a string in a math operation. You fist have to convert it to a double.Java Code:inches = [COLOR="red"]a[5][/COLOR]/ 0.0254;
Take the array assignmente out of the "for" loop:Java Code:for(int i=0;i<s.length();i++){ [COLOR="Red"]String a[i]= s.split(" ");[/COLOR]
There are more errors, but that all the time I have...Java Code:[COLOR="Blue"]String a[i]= s.split(" ");[/COLOR] for(int i=0;i<s.length();i++){
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-25-2009, 04:37 PM #9
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
still can not run...it still same after i take out the String a[i]= s.split(" ") from for loop,is that need to put a for loop?why this error keep occuring?
C:\Documents and Settings\user\My Documents\Question2.java:20: ']' expected
String a[i]= s.split(" ");
^
1 error
import java.util.Scanner;
public class Question2 {
public static void main(String[] args) {
double inches=0;
double meters=0;
double liters=0;
double quarts=0;
double grams=0;
double pounds=0;
Scanner scanner = new Scanner(System.in);
System.out.println(" Enter A Metric Conversion Question.");
String s = scanner.nextLine();
String a[i]= s.split(" ");
for(int i=0;i<7;i++){
double k =Double.parseDouble(a[5]);
if( a[3].equals("inches")) {
if( a[6].equals("meters"))
inches = k/ 0.0254;
System.out.println(" They are " + inches + " inches in " +k+ " meters ");
}
else
System.out.println(" This question is invalid conversion.");
if( a[3].equals("meters")) {
if(a[6] .equals("inches"))
meters = k * 0.0254;
System.out.println(" They are " + meters + " meters in " +k+ " inches ");
}
else
System.out.println(" This question is invalid conversion.");
if( a[3] .equals("liters")) {
if( a[6].equals("quarts"))
liters = k / 0.0254;
System.out.println(" They are " + liters + " liters in " +k+ " quarts ");
}
else
System.out.println(" This question is invalid conversion.");
if( a[3] .equals("quarts")) {
if( a[6].equals("liters"))
quarts = k * 0.0254;
System.out.println(" They are " + quarts + " quarts in " +k+" liters ");
}
else
System.out.println(" This question is invalid conversion.");
if( a[3].equals("grams")) {
if( a[6].equals("pounds"))
grams = k / 0.0254;
System.out.println(" They are " + grams + " grams in " +k+ " pounds ");
}
else
System.out.println(" This question is invalid conversion.");
if( a[3] .equals("pounds")) {
if( a[6] .equals("grams"))
pounds = k / 0.0254;
System.out.println(" They are " + pounds + " pounds in " +k+ " grams ");
}
else
System.out.println(" This question is invalid conversion.");
}
}
}
- 02-25-2009, 07:29 PM #10
OK... here's how it should look:
Luck,Java Code:String [] a = s.split(" ");
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-26-2009, 12:58 AM #11
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
i correct the error u told,it can be compiled,but it cant run,got exception in thread "main" java.lang.NumberForamatException...Is it the for loop problem?i dont know what number of looping should put,can i just put seven means seven words?
- 02-26-2009, 01:27 AM #12
Please post the complete error msg... it tells on which line the error happened
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-26-2009, 01:35 AM #13
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
Enter A Metric Conversion Question.
how many of inches are in 4 meters
Exception in thread "main" java.lang.NumberFormatException: For input string: "in"
at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1224)
at java.lang.Double.parseDouble(Double.java:482)
at Question2.main(Question2.java:25)
Press any key to continue . . .
- 02-26-2009, 02:11 AM #14
You must have changed the code, because I don't get that error. I did find the following:
The index is wrong... it sould be:Java Code:if( a[[B][COLOR="Red"]3[/COLOR][/B]].equals("meters"))
This should be changed through all the "if"s.Java Code:if( a[[B][COLOR="Blue"]2[/COLOR][/B]].equals("meters"))
The for loop (as coded) has no function. It just loops over the same part of the code. What is it for?
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-26-2009, 02:21 AM #15
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
i thougthfor loop is needed because array is using...i cut them off alreday...it can be run now...thank you....
- 02-26-2009, 02:27 AM #16


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks