Results 1 to 12 of 12
  1. #1
    shaggyoo7 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Default Need Urgent help parseInt and parseFloat problem in code need help plz

    I am having two classes Temperature and Converter these classes are infact two different files.
    The classes are as follows:-
    --------------------------------------------------------------------------
    public class Temperature
    {
    private double Fahrenheit,Celsius;
    public double Fahren2Cels(double Fahren)
    {
    Celsius = ((Fahren * (9 / 5)) + 32);
    return Celsius;
    }
    public double Cels2Fahren(double Cels)
    {
    Fahrenheit = ((Cels - 32) * (5 / 9));
    return Fahrenheit;
    }
    }

    --------------------------------------------------------------------------
    import java.io.*;
    import java.lang.*;
    public class Converter
    {
    static Temperature t1 = new Temperature();
    public static void main(String args[])
    {
    System.out.println("Menu:");
    System.out.println("1. Convert Fahrenheit to Celsius.");
    System.out.println("2. Convert Celsius to Fahrenheit.");
    BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
    int ch = Integer.parseInt(br2);
    switch (ch)
    {
    case 1:
    System.out.println("Enter the temperature in Fahrenheit.");
    BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
    double value = Double.parseDouble(br1);
    double ans = t1.Fahren2Cels(value);
    System.out.println("The ans is "+ans);
    break;
    /*Rest code yet to be developed*/
    default:
    break;
    }
    }
    }
    --------------------------------------------------------------------------
    Temperature class compiles well, but while compiling Converter the compile time error are as follows:-
    --------------------------------------------------------------------------
    D:\Program Files\Java\jdk1.7.0\bin>javac Converter.java
    Converter.java:14: cannot find symbol
    symbol : method parseInt(java.io.BufferedReader)
    location: class java.lang.Integer
    int ch = Integer.parseInt(br2);
    ^
    Converter.java:20: method parseDouble in class java.lang.Double cannot be applied to given types
    required: java.lang.String
    found: java.io.BufferedReader
    double value = Double.parseDouble(br1);
    ^
    2 errors
    --------------------------------------------------------------------------
    Someone plz help me its urgent

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

    Default Why double to int?

    • Integer.parseInt(BufferedReader) doesn't exist.
    • Double.parseDouble(BufferedReader) doesn't exist.
    • What does exist is:

    Integer parseInt(java.lang.String)

    Double parseDouble(java.lang.String))

    Question: why are you trying to turn the double's result into into int?

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

  3. #3
    shaggyoo7 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Default Need more help

    I did the necessary changes both the classes compiled successfully but now there is a runtime exception pls go through:-
    --------------------------------------------------------------------------
    public class Temperature
    {
    private double Fahrenheit,Celsius;
    public double Fahren2Cels(double Fahren)
    {
    Celsius = ((Fahren * (9 / 5)) + 32);
    return Celsius;
    }
    public double Cels2Fahren(double Cels)
    {
    Fahrenheit = ((Cels - 32) * (5 / 9));
    return Fahrenheit;
    }
    }
    --------------------------------------------------------------------------
    import java.io.*;
    import java.lang.*;
    public class Converter
    {
    static Temperature t1 = new Temperature();
    public static void main(String args[])
    {
    System.out.println("Menu:");
    System.out.println("1. Convert Fahrenheit to Celsius.");
    System.out.println("2. Convert Celsius to Fahrenheit.");
    BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
    int ch = Integer.parseInt(br2.toString());
    switch (ch)
    {
    case 1:
    System.out.println("Enter the temperature in Fahrenheit.");
    BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
    double value = Double.parseDouble(br1.toString());
    double ans = t1.Fahren2Cels(value);
    System.out.println("The ans is "+ans);
    break;
    default:
    break;
    }
    }
    }
    --------------------------------------------------------------------------
    D:\Program Files\Java\jdk1.7.0\bin>java Converter
    Menu:
    1. Convert Fahrenheit to Celsius.
    2. Convert Celsius to Fahrenheit.
    Exception in thread "main" java.lang.NumberFormatException: For input string: "j
    ava.io.BufferedReader@173a10f"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.
    java:65)
    at java.lang.Integer.parseInt(Integer.java:484)
    at java.lang.Integer.parseInt(Integer.java:519)
    at Converter.main(Converter.java:12)
    --------------------------------------------------------------------------
    Pls provide me more help..........

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

  5. #5
    shaggyoo7 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Default

    Then what should be done 2 accept a comand line input and apply it in a switch case for the perticular case to be executed.

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

    Default

    use method like readLine in bufferReader to read a line of string
    and
    catch IOException .. NumberFormatException

  7. #7
    shaggyoo7 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Default

    Thanks a lot it worked.

  8. #8
    shaggyoo7 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Default

    No answer it produces 0.0 as the answer where is the problen pls check the code:-
    --------------------------------------------------------------------------
    public class Temperature
    {
    private double Fahrenheit,Celsius;
    public double Fahren2Cels(double Fahren)
    {
    Celsius = ((Fahren -32) * 5 / 9);
    return Celsius;
    }
    public double Cels2Fahren(double Cels)
    {
    Fahrenheit = ((Cels * (9 / 5)) + 32);
    return Fahrenheit;
    }
    }
    --------------------------------------------------------------------------
    import java.io.*;
    import java.lang.*;
    public class Converter
    {
    static Temperature t1 = new Temperature();
    public static void main(String args[])
    {
    System.out.println("Menu:");
    System.out.println("1. Convert Fahrenheit to Celsius.");
    System.out.println("2. Convert Celsius to Fahrenheit.");
    try
    {
    BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
    int ch = Integer.parseInt(br2.readLine());
    switch (ch)
    {
    case 1:
    System.out.println("Enter the temperature in Fahrenheit.");
    BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
    double value = Double.parseDouble(br1.readLine());
    double ans = t1.Fahren2Cels(value);
    System.out.println("The ans is "+ans);
    break;
    default:
    break;
    }
    }catch(Exception e)
    {
    System.out.println("The Exception is"+e);
    }
    }
    }
    --------------------------------------------------------------------------
    D:\Program Files\Java\jdk1.7.0\bin>java Converter
    Menu:
    1. Convert Fahrenheit to Celsius.
    2. Convert Celsius to Fahrenheit.
    1
    Enter the temperature in Fahrenheit.
    103
    The ans is 0.0
    --------------------------------------------------------------------------
    What is the problem i am not getting anyone with a sollution pls.

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

    Default

    The Fahren to Cels works fine for me? Sure you compiled the latest version of your code?
    Menu:
    1. Convert Fahrenheit to Celsius.
    2. Convert Celsius to Fahrenheit.
    1
    Enter the temperature in Fahrenheit.
    103
    The ans is 39.44444444444444
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  10. #10
    shaggyoo7 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Default

    Thans i was compiling the wrong file.

  11. #11
    shaggyoo7 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Default

    Hey does any one know when we write an interface in a file what the files extension is suppose to be.

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

Similar Threads

  1. Problem with Integer.parseInt()
    By Hevonen in forum New To Java
    Replies: 2
    Last Post: 12-14-2008, 03:41 AM
  2. [SOLVED] Command Line Arguments and ParseInt
    By Sophiie in forum New To Java
    Replies: 4
    Last Post: 11-16-2008, 09:45 PM
  3. code HELP URGENT
    By NickkicN in forum New To Java
    Replies: 1
    Last Post: 08-14-2008, 05:43 PM
  4. Urgent small code
    By karingulanagaraj in forum New To Java
    Replies: 7
    Last Post: 08-11-2008, 04:11 AM
  5. <URGENT> problem after linking .jnlp
    By bongia in forum New To Java
    Replies: 14
    Last Post: 11-18-2007, 05:57 PM

Posting Permissions

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