Results 1 to 8 of 8

Thread: i need help..

  1. #1
    amalina is offline Member
    Join Date
    Jul 2009
    Posts
    17
    Rep Power
    0

    Question i need help..

    i get a task that is we get 3 numbers and get the average...
    this is my code...
    but there is an error occur..
    it does not display the average of 3 numbers.


    package average;
    import javax.swing.JOptionPane;



    public class ave {



    public static void main(String[] args)
    {
    String sumStr = "";
    int Value;
    int sum;

    JOptionPane.showInputDialog(null,
    "Rainfall for April:\t",
    "Input Dialog",
    JOptionPane.QUESTION_MESSAGE);


    JOptionPane.showInputDialog(null,
    "Rainfall for May:\t",
    "Input Dialog",
    JOptionPane.QUESTION_MESSAGE);

    JOptionPane.showInputDialog(null,
    "Rainfall for June:\t",
    "Input Dialog",
    JOptionPane.QUESTION_MESSAGE);


    sumStr += "Average rainfall:\t";

    int intValue = Integer.parseInt(sumStr);



    sumStr = "int 1" + "int 2" + "int 3";

    JOptionPane.showMessageDialog(null,sumStr,"Sum",JO ptionPane.INFORMATION_MESSAGE);


    System.exit(0);
    }


    }


    this is the error:
    Exception in thread "main" java.lang.NumberFormatException: For input string: "Average rainfall: "
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:447)
    at java.lang.Integer.parseInt(Integer.java:497)
    at average.ave.main(ave.java:43)

    i need a fast reply..
    tq for helping me...
    please..
    Last edited by amalina; 08-24-2009 at 03:22 AM. Reason: wrong copy..

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    This is totally wrong. First of all you had to have three integers to calculate the summation and then calculate the average,

    Java Code:
    int intValue = Integer.parseInt(sumStr);
    This is wrong, because you cannot convert a string to integer. It's wrong.

    And also the way you've handle the user inputs in messagebox it wrong, you haven't collect the user response.

  3. #3
    amalina is offline Member
    Join Date
    Jul 2009
    Posts
    17
    Rep Power
    0

    Default

    i did not get what are you trying to say..
    can you explain more..
    tq

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    1. You want to collect information(a number) from the user through a message box, JOptionpane. But where you store them?

    2. Did you read Java doc about the JOptionpane, it's return the user input as a string. Even a user enter a number, it return them in the application as a String. You cannot string together, not make sense at all.
    eg: 1 + 2 = 3, this is valid, but
    "One" + "Two" = ??????,


  5. #5
    amalina is offline Member
    Join Date
    Jul 2009
    Posts
    17
    Rep Power
    0

    Question

    okay eranga..
    this is my new coding..
    there is no error..
    but it does not display the average of rainfall..
    could you help me out..


    package average1;

    import javax.swing.JOptionPane;


    public class ave {


    public static void main(String[] args) {
    String aveDouble = "";
    int month1,month2,month3,average;
    String month1Double, month2Double, month3Double, averageDouble;




    month1Double = JOptionPane.showInputDialog("Rainfall in April:\t");
    month1 = Integer.parseInt(month1Double);

    month2Double = JOptionPane.showInputDialog("Rainfall in May:\t");
    month2 = Integer.parseInt(month2Double);

    month3Double = JOptionPane.showInputDialog("Rainfall in June:\t");
    month3 = Integer.parseInt(month3Double);


    JOptionPane.showMessageDialog(null, aveDouble, "average rainfall",
    JOptionPane.INFORMATION_MESSAGE);

    System.exit(0);
    }
    public static void calculateAverage(double month1, double month2, double month3, double a)

    {
    a = (month1 + month2 + month3) / 5.0;


    }

    }

  6. #6
    amalina is offline Member
    Join Date
    Jul 2009
    Posts
    17
    Rep Power
    0

    Smile Solved

    package average1;

    import javax.swing.JOptionPane;


    public class ave {


    public static void main(String[] args) {
    double a;
    int month1,month2,month3,average;
    String month1Double, month2Double, month3Double, averageDouble;




    month1Double = JOptionPane.showInputDialog("Rainfall in April:\t");
    month1 = Integer.parseInt(month1Double);

    month2Double = JOptionPane.showInputDialog("Rainfall in May:\t");
    month2 = Integer.parseInt(month2Double);

    month3Double = JOptionPane.showInputDialog("Rainfall in June:\t");
    month3 = Integer.parseInt(month3Double);



    a = (month1 + month2 + month3) / 3.0;



    JOptionPane.showMessageDialog(null, a, "average rainfall" + a,
    JOptionPane.INFORMATION_MESSAGE);

    System.exit(0);


    }

    }

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Nice so you got the point right, seems to me you read the Java doc carefully. And also I believe that you've a better idea about the variable types like Strings as well, you cannot add string in Java, only thing you can do is concatenate them. Good luck!

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Java Code:
    System.exit(0);
    This is not necessary in main method at all. JVM exit the main method safely all the time, even your code throws any exceptions.

    And also at the same time, it's better if you can study about Object Oriented concepts. Simply at this moment what I want to tell you is, don't workaround with a single method all the time. User different methods for different process. Main method is just the entry point into your code, by the OS

Posting Permissions

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