# what is the problem?

• 02-25-2009, 05:44 AM
li_yun88
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, 06:24 AM
mtyoung
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
Code:

`if( charAt[3] == inches)`
inches should be a String, not a variable name, and please use xxx.equals("yyy") for checking

also, array index start from 0

you do not initialize, assign value to variables like meters....
• 02-25-2009, 06:58 AM
li_yun88
pls help me!!
can u help me to correct this programming? i really dont how to correct this? thanks!
• 02-25-2009, 07:02 AM
Fubarable
Why not first try his suggestions, at least attempt to try, and come back with your code?
• 02-25-2009, 08:04 AM
li_yun88
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, 08:25 AM
mtyoung
operator / cannot be applied to java.lang.String,double
means String variable can be divided

and
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 "); }```
what do you want to do?

Quote:

i suggest you to use xxx = string.split(" ") method... as we can access single word like xxx[2] directly...
assume user input "How many inches are in 2 meters?”
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, 12:33 PM
li_yun88
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.");

}

}
• 02-25-2009, 03:23 PM
CJSLMAN
I think mtyoung had a typo...
Quote:

means String variable can't be divided
The following can't work:
Code:

`inches = [COLOR="red"]a[5][/COLOR]/ 0.0254;`
You can't use a string in a math operation. You fist have to convert it to a double.
Code:

```for(int i=0;i<s.length();i++){ [COLOR="Red"]String a[i]= s.split(" ");[/COLOR]```
Take the array assignmente out of the "for" loop:
Code:

```[COLOR="Blue"]String a[i]= s.split(" ");[/COLOR] for(int i=0;i<s.length();i++){```
There are more errors, but that all the time I have...

Luck,
CJSL
• 02-25-2009, 05:37 PM
li_yun88
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, 08:29 PM
CJSLMAN
OK... here's how it should look:
Code:

`String [] a = s.split(" ");`
Luck,
CJSL
• 02-26-2009, 01:58 AM
li_yun88
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, 02:27 AM
CJSLMAN
Please post the complete error msg... it tells on which line the error happened

Luck,
CJSL
• 02-26-2009, 02:35 AM
li_yun88
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 java.lang.Double.parseDouble(Double.java:482)
at Question2.main(Question2.java:25)
Press any key to continue . . .
• 02-26-2009, 03:11 AM
CJSLMAN
You must have changed the code, because I don't get that error. I did find the following:
Code:

`if( a[[B][COLOR="Red"]3[/COLOR][/B]].equals("meters"))`
The index is wrong... it sould be:
Code:

`if( a[[B][COLOR="Blue"]2[/COLOR][/B]].equals("meters"))`
This should be changed through all the "if"s.

The for loop (as coded) has no function. It just loops over the same part of the code. What is it for?

Luck,
CJSL
• 02-26-2009, 03:21 AM
li_yun88
i thougthfor loop is needed because array is using...i cut them off alreday...it can be run now...thank you....
• 02-26-2009, 03:27 AM
CJSLMAN
So your program is running? Great !!! You welcome

Luck,
CJSL