Results 1 to 16 of 16
  1. #1
    li_yun88 is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

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


    }
    }

    }

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    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[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....
    Last edited by mtyoung; 02-25-2009 at 06:55 AM.

  3. #3
    li_yun88 is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default pls help me!!

    can u help me to correct this programming? i really dont how to correct this? thanks!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Why not first try his suggestions, at least attempt to try, and come back with your code?

  5. #5
    li_yun88 is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default

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


    }
    }

    }

  6. #6
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    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[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

  7. #7
    li_yun88 is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default

    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 01:18 PM.

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    I think mtyoung had a typo...
    means String variable can't be divided
    The following can't work:
    Java 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.
    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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    li_yun88 is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default

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


    }
    }

    }

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    OK... here's how it should look:
    Java Code:
    String [] a = s.split(" ");
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    li_yun88 is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default

    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?

  12. #12
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

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

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  13. #13
    li_yun88 is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default

    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 . . .

  14. #14
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  15. #15
    li_yun88 is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default

    i thougthfor loop is needed because array is using...i cut them off alreday...it can be run now...thank you....

  16. #16
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    So your program is running? Great !!! You welcome

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Posting Permissions

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