Welcome to Dream.In.Code
Become a Java Expert!

Join 150,064 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,770 people online right now. Registration is fast and FREE... Join Now!




terminating this program

 
Reply to this topicStart new topic

terminating this program

mds913
8 Jun, 2008 - 12:22 PM
Post #1

New D.I.C Head
*

Joined: 5 Jun, 2008
Posts: 18

i have this coding working just fine. i have only one problem. i can't get the program to terminate at the point were the user is asked via JOptionPane "Would you like to try again?" if the answer is no then it should term. instead it jumps back up and asks for the number of students again.

CODE

import javax.swing.JOptionPane;
import java.util.*;

public class Array2


{
    public static void main(String[]args)
    {
       int students;
       String again;  
       int [ ] scores = new int[10];
       String numOfStudents;
       String posNumber;
       String inputOrRandom=null;
       String answer;
      
       do
       {
        numOfStudents = JOptionPane.showInputDialog("How many students in the class?");
        students = Integer.parseInt(numOfStudents);
        
        while(students < 0)
        {
            posNumber = JOptionPane.showInputDialog("Please enter a positive number.");
            students = Integer.parseInt(posNumber);
        }
            if(students > 0)
            inputOrRandom = JOptionPane.showInputDialog("Would you like to input the numbers or have them random generate? (I/R) ");
            
            if(inputOrRandom.equals("I") || inputOrRandom.equals("i"))
             scores = generateUser(students);
                        
            else if
            (inputOrRandom.equals("R") || inputOrRandom.equals("r"))
            scores = generateRandom(students);
            
            else
            return;
            
            outputHistogram(scores);
            
            again = JOptionPane.showInputDialog("Would you like to try again? ");
        }
            while(again.equals("N") || again.equals("n"));                                              
            System.out.println("");//if(again =="N" || again =="n")
            System.exit(0);
            
        }

    public static int [] generateUser(int numOfStudents)
    {
        int i=0;
        int [ ] scores = new int[10];
        int grade;
        
       for(int j = 0; j < 10;++j)
            scores[j] = 0;
          
        for(i = 0; i < numOfStudents; i++)
        {
                do
            {
             grade = Integer.parseInt(JOptionPane.showInputDialog(
                    "enter score from 1 to 100 inclusive"));
            }while(grade < 1 || grade >100);    //if the user enters a score <1 or > 100 it loops back up and
                                                //increment the counter.
                    
            scores[(grade-1)/10]= scores[(grade-1)/10]+1;
            System.out.print(grade + " ");
        }
            System.out.println();
            return scores;
    }
    
    public static int[] generateRandom(int numOfStudents)
    {
            int[] scores = new int[10];
            Random generator = new Random();
        
            for(int i = 0; i < numOfStudents; i++) {
            int grade = 0;
            while(grade < 1 || grade > 100) {
               double x = generator.nextGaussian();
               x += 3.6;
               x *= 20;
               grade = (int) x;
               grade = Math.max(1,grade);
               grade = Math.min(100,grade);
            }
                      scores[(grade-1)/10]++;
                      System.out.print(grade + " ");
        }
            System.out.println();    
            return scores;
    }

    public static void outputHistogram(int [] scores)
    {
            
             for(int i = 0; i < 10;++i)
             {
               if(i == 0)
              
               System.out.print((i*10 + 1)+" -"+(i*10+10)+ "  |"); //this prints the labels 1-10
               if(i >= 1 && i <= 8)
               System.out.print((i*10 + 1)+"-"+(i*10+10)+ "  |");//this prints the labels 11-21 thru 81-90.
               if(i >= 9 && i <= 10)  // else
               System.out.print((i*10 + 1)+"-"+(i*10+10)+ " |");//this prints the labels 91-100.
                                  
               for(int x =0; x < scores[i];++x)
               System.out.print("*");               //this prints the astriks
                
               System.out.println();
             }
          
    }
}

User is offlineProfile CardPM
+Quote Post

herefishyfishy
RE: Terminating This Program
8 Jun, 2008 - 12:26 PM
Post #2

D.I.C Head
Group Icon

Joined: 1 May, 2008
Posts: 60


Dream Kudos: 100
My Contributions
Change the while to an if and wrap the System.exit(0); in its code block.

This post has been edited by herefishyfishy: 8 Jun, 2008 - 12:28 PM
User is offlineProfile CardPM
+Quote Post

mds913
RE: Terminating This Program
8 Jun, 2008 - 12:28 PM
Post #3

New D.I.C Head
*

Joined: 5 Jun, 2008
Posts: 18

you're kidding right?
User is offlineProfile CardPM
+Quote Post

herefishyfishy
RE: Terminating This Program
8 Jun, 2008 - 12:29 PM
Post #4

D.I.C Head
Group Icon

Joined: 1 May, 2008
Posts: 60


Dream Kudos: 100
My Contributions
Sorry, I didn't read your post carefully enough.
User is offlineProfile CardPM
+Quote Post

nick2price
RE: Terminating This Program
8 Jun, 2008 - 01:11 PM
Post #5

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
Why dont you just use a YES NO dialog box instead of an input dialog. Looks a lot better and easy. Also, looks like your brackets are in the wrong place. look at this
CODE
private void AddEntryJButtonActionPerformed( ActionEvent event )
{
    int answer = JOptionPane.YES_OPTION;
    int count = 0;
  
   try{
  
    
        while (answer == JOptionPane.YES_OPTION )
            {

            String ar = JOptionPane.showInputDialog("Please, enter artist name");
            String cd = JOptionPane.showInputDialog("Enter CD name:");
            int n = getNumberOfTracks();
            if(n < 0)
            {
                    // user cancelled
                continue;
            }

              if  (ar!=null &&  cd!=null && !ar.trim().equals("") && !cd.trim().equals(""))
                 //making sure that data has been inputted
                {
                Album album = new Album(ar, cd, n);    //creating new Album object and adding my three variables to it
                list.add(album);  //adding this Album object to my ArrayList
                }
                
            else
                {
                   JOptionPane.showMessageDialog(null, "please enter all required data");
                }
    
        answer = JOptionPane.showConfirmDialog(null, "Enter another record?",  //if yes, will loop again, if no it will exit
                      "???", JOptionPane.YES_NO_OPTION);
    
        count++; //increment
    
            }
       }
      
   catch (NumberFormatException  e)
        {
            System.err.println("Caught NumberFormatException : "+ e.getMessage());
            e.printStackTrace();

        }
        

}


This post has been edited by nick2price: 8 Jun, 2008 - 01:14 PM
User is online!Profile CardPM
+Quote Post

pbl
RE: Terminating This Program
8 Jun, 2008 - 05:50 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions

while(again.equals("Y") || again.equals("y"));

you will ask this question in how many post ? mad.gif
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:44PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month