Results 1 to 8 of 8
  1. #1
    j0ta_12 is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Exclamation Need help in a program

    this programs supose to get 3 names and 3 times and then put them in order from the minior number to the mayor number plss help me solve this problem

    import javax.swing.JOptionPane;
    public class racersTime {

    /**
    * @param args
    */
    public static void main(String[] args) {

    String inputString;
    String name1, name2, name3,temp2;
    double time1, time2, time3;
    double temp1;


    inputString = JOptionPane.showInputDialog("Enter the name of the contestant:");
    name1 = inputString;
    inputString = JOptionPane.showInputDialog("Enter the time of the contestan:");
    time1= Double.parseDouble(inputString);

    inputString = JOptionPane.showInputDialog("Enter the name of the contestant:");
    name2 = inputString;
    inputString = JOptionPane.showInputDialog("Enter the time of the contestan:");
    time2= Double.parseDouble(inputString);

    inputString = JOptionPane.showInputDialog("Enter the name of the contestant:");
    name3 = inputString;
    inputString = JOptionPane.showInputDialog("Enter the time of the contestan:");
    time3= Double.parseDouble(inputString);


    if (time1 > time2)
    {temp1=time1;
    time1=time2;
    time2=temp1;}

    {temp2=name1;
    name1=name2;
    name2=temp2;}

    if(time2 > time3)
    {temp1=time2;
    time2=time3;
    time3=temp1;}

    {temp2=name2;
    name2=name3;
    name3=temp2;}
    if(time1 > time3 )
    {temp1=time1;
    time1=time3;
    time3=temp1;}

    {temp2=name1;
    name1=name3;
    name3=temp2;}

    JOptionPane.showMessageDialog(null," The contestant from the first to the last: "+
    name1+","+time1 + " " + name2 + ","+time2+ " " + name3+","+time3);


    System.exit(0);
    }




    }

    thank for your needed help :):)

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

    Default

    Two suggestions:
    1) Please edit your post so that it uses code tags (see my signature with its link below). Doing this will make your code much more readable, and will increase your chances of getting help.
    2) Also, if you provide more details as to exactly what your current problem is, you'll also increase your chances here.

    Much luck!

  3. #3
    j0ta_12 is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    Tnks im new here :)

  4. #4
    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 j0ta_12 View Post
    Tnks im new here :)
    Oh, my bad, I forgot -- a big welcome here!!

    (but again, if you follow my advice above, you'll likely get better help quicker!)

  5. #5
    j0ta_12 is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    Wen i run the program this supose to put the three names and the three times in order
    of minior to mayor and went i put this numbers 12, 4, 8 the program don't give me it corectly

    Ex.of wath i want to the program i enter:
    Jonathan 12.54 -seconds
    Joel 10.00 - seconds
    Erick - 8.0 - seconds

    the program supose to give me this output: erick,8.00 joel,10.00 jonathan, 12.54

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

    Default

    That seems correct to me, since for an auto racer, a shorter time is better.

    Edit: oh, I see. It is not outputting this result. Well you might want to post what it is in fact outputting, but even more important, I recommend that you go through on paper just what your program is doing:

    Let's get rid of the names and just use letters. Say you input
    A 12
    B 10
    C 8

    The first if block will check the first two racers and swap them if 1 is greater than 2. This will result in:
    B 10
    A 12
    C 8

    The next if block will swap the 2nd and 3rd racers if 2 is greater than 3 (which again it is), resulting in:
    B 10
    C 8
    A 12

    The last if block will swap the 3rd and first racers if first is greater than the third. Since this isn't so, no swap will be done, and your result will be as noted above.

    Given this reasoning, you should be able to figure out a solution, right?

    Also, this is dangerous:
    Java Code:
          if (time1 > time2) {
             temp1 = time1;
             time1 = time2;
             time2 = temp1;
          }
    
          {
             temp2 = name1;
             name1 = name2;
             name2 = temp2;
          }
    As you'll swap names in the second block whether or not the if is true. Better is:
    Java Code:
          if (time1 > time2) {
             temp1 = time1;
             time1 = time2;
             time2 = temp1;
    
             temp2 = name1;
             name1 = name2;
             name2 = temp2;
          }
    Last edited by Fubarable; 11-04-2009 at 11:44 PM.

  7. #7
    j0ta_12 is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    Thanks realy now i solve my error you safe me in this homework :)
    :):):)

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

Similar Threads

  1. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 05:32 PM
  2. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 04:17 PM
  3. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 08:24 AM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 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
  •