Results 1 to 9 of 9
  1. #1
    Joshsmith is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default Need help: output is not what i want

    // I have a few errors: honor is not initialized

    import javax.swing.JOptionPane;

    public class Honors{

    public static void main(String[] args)
    {

    String major, name, honor;
    double gpa;

    major = JOptionPane.showInputDialog("what is your major?");

    gpa = Double.parseDouble(JOptionPane.showInputDialog("wh at is your high school gpa?"));

    name = JOptionPane.showInputDialog("what is your name?");

    if(gpa < 3.2)
    honor = "Not accepted into University of Java";
    else
    if(gpa <3.5)
    honor = "Not awarded scholarship";
    else if (3.5 >= gpa <= 3.6) // i have an error here: How do i show gpa is between 3.5 - 3.6?
    honor = "1000";
    else
    if (gpa > 3.6)
    honor = "placed in honors and receive 5000";

    JOptionPane.showMessageDialog(null, "student: " + name + " Scholarship award " + honor);

    }
    }
    Last edited by Joshsmith; 09-26-2009 at 07:53 PM.

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

    Default

    Do what the error message is telling you to do: initialize honor; give it a default value before the if blocks. "" will do.

  3. #3
    Joshsmith is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    How do I initialize honor? Didn't I already initialize it with string? Also, I get an error here:
    else if (3.5 >= gpa <= 3.6) // i have an error here: How do i show gpa is between 3.5 - 3.6?

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

    Default

    How do I initialize honor? Didn't I already initialize it with string?
    You declare honor but don't initialize it before your if statements.

    In other words, you do this:
    Java Code:
    String honor;
    but never do this:
    Java Code:
    honor = "";
    (or in one step)
    Java Code:
    String honor = "";
    You initialize honor just as you would initialize any variable, by assigning it a value such as "" or null.

    How do i show gpa is between 3.5 - 3.6?
    Java Code:
    if (gpa >= 3.5 && gpa <= 3.6)

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Declare and Initialize do not mean the same thing -- you declared it. Do as Fubarable says:
    Java Code:
    String honor = "";
    "if (3.5 >= gpa <= 3.6) "
    is not valid, nor does it make sense. Maybe you meant:
    Java Code:
    if (gpa >= 3.5 && gpa <= 3.6)

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Ah! you beat me to the punch Fubarable :D

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

    Default

    Quote Originally Posted by quad64bit View Post
    Ah! you beat me to the punch Fubarable :D
    By exactly 1 minute at that. ;) Though it's good to see that our recommendations are the same.

  8. #8
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    epic code battle
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    Or, give honor a starting value the same as one of the criteria (the "> 3.6" one probably) and compeletely skip that check. That way you're not initialising it to a value that's never used.

Similar Threads

  1. cannot see the output...
    By Mihail Kravsun in forum New To Java
    Replies: 3
    Last Post: 04-15-2009, 06:57 AM
  2. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM
  3. how to c the output
    By pro85 in forum Java Applets
    Replies: 1
    Last Post: 02-11-2009, 09:09 AM
  4. different output
    By pro85 in forum Java Applets
    Replies: 6
    Last Post: 08-05-2008, 01:42 PM
  5. output
    By Camden in forum New To Java
    Replies: 3
    Last Post: 12-01-2007, 10:34 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
  •