# Thread: what is the problem?

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 == inches) {
if( charAt == 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 == meters) {
if(charAt == 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 == liters) {
if( charAt == 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 == quarts) {
if( charAt == 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 == grams) {
if( charAT == 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 == pounds) {
if( charAt == grams)

pounds = grams / 0.0254;
System.out.println(" They are " + pounds + " pounds in " + num + " grams ");

}

else
System.out.println(" This question is invalid conversion.");

}
}

}  Reply With Quote

2. Senior Member Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
12

## 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
Java Code:
`if( charAt == 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....
Last edited by mtyoung; 02-25-2009 at 05:55 AM.  Reply With Quote

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!  Reply With Quote

4. ## Why not first try his suggestions, at least attempt to try, and come back with your code?  Reply With Quote

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.");

}
}

}  Reply With Quote

6. Senior Member Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
12

## operator / cannot be applied to java.lang.String,double
means String variable can be divided

and
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 ");

}```
what do you want to do?

i suggest you to use xxx = string.split(" ") method... as we can access single word like xxx directly...
assume user input "How many inches are in 2 meters?”
xxx will be "How", xxx 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  Reply With Quote

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=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=Double.parseDouble(s);

if( a.equals("inches")) {
if( a.equals("meters"))

inches = a/ 0.0254;
System.out.println(" They are " + inches + " inches in " +a+ " meters ");

}

else
System.out.println(" This question is invalid conversion.");

if( a.equals("meters")) {
if(a .equals("inches"))

meters = a * 0.0254;
System.out.println(" They are " + meters + " meters in " +a+ " inches ");

}

else
System.out.println(" This question is invalid conversion.");

if( a .equals("liters")) {
if( a.equals("quarts"))

liters = a / 0.0254;
System.out.println(" They are " + liters + " liters in " +a+ " quarts ");

}

else
System.out.println(" This question is invalid conversion.");

if( a .equals("quarts")) {
if( a.equals("liters"))

quarts = a * 0.0254;
System.out.println(" They are " + quarts + " quarts in " +a+" liters ");

}

else
System.out.println(" This question is invalid conversion.");

if( a.equals("grams")) {
if( a.equals("pounds"))

grams = a / 0.0254;
System.out.println(" They are " + grams + " grams in " +a+ " pounds ");

}

else
System.out.println(" This question is invalid conversion.");

if( a .equals("pounds")) {
if( a .equals("grams"))

pounds = a / 0.0254;
System.out.println(" They are " + pounds + " pounds in " +a+ " grams ");

}

else
System.out.println(" This question is invalid conversion.");

}

}
Last edited by li_yun88; 02-25-2009 at 12:18 PM.  Reply With Quote

8. ## I think mtyoung had a typo...
means String variable can't be divided
The following can't work:
Java Code:
`inches = [COLOR="red"]a[/COLOR]/ 0.0254;`
You can't use a string in a math operation. You fist have to convert it to a double.
Java 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:
Java 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  Reply With Quote

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);

if( a.equals("inches")) {
if( a.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.equals("meters")) {
if(a .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 .equals("liters")) {
if( a.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 .equals("quarts")) {
if( a.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.equals("grams")) {
if( a.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 .equals("pounds")) {
if( a .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.");

}
}

}  Reply With Quote

10. ## OK... here's how it should look:
Java Code:
`String [] a = s.split(" ");`
Luck,
CJSL  Reply With Quote

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?  Reply With Quote

12. ## Please post the complete error msg... it tells on which line the error happened

Luck,
CJSL  Reply With Quote

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 java.lang.Double.parseDouble(Double.java:482)
at Question2.main(Question2.java:25)
Press any key to continue . . .  Reply With Quote

14. ## You must have changed the code, because I don't get that error. I did find the following:
Java Code:
`if( a[[B][COLOR="Red"]3[/COLOR][/B]].equals("meters"))`
The index is wrong... it sould be:
Java 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  Reply With Quote

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....  Reply With Quote

16. ## So your program is running? Great !!! You welcome

Luck,
CJSL  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•