Results 1 to 6 of 6
  1. #1
    bigDaddyJohn is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Printing out to dialog box

    I need to print the following completely in one Joption.pane

    Excuse name # of excuses

    My dog ate it 15
    It got burned 12
    I lost it 18
    I ate it 23
    It fell down the sewer 16
    It got shredded 44
    I forgot it 19
    I can't remember where I put it 17
    Coffee spilled on it 0
    My daughter colored on it 1

    The Excuse Coffee spilled on it had the max numer of excuses
    The Excuse My daughter colored on it had the min numer of excuses
    Total number of excuses: 264

    But my output is only one window at a time

    Any help would be appreciated!


    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.Scanner;
    
    
    public class Array {
    
        private static int[] nums = new int[10];
        private static String[] Excuses = {"My dog ate it","It got burned","I lost it","I ate it",
    	 "It fell down the sewer", "It got shredded","I forgot it","I can't remember where I put it", "Coffee spilled on it","My daughter colored on it"};
    	 
    	 //main method begins program execution
        public static void main(String[] args) throws FileNotFoundException{
    
    
          File file = new File("name.txt");
          StringBuilder contents = new StringBuilder();
          BufferedReader reader = null;
          reader = new BufferedReader(new FileReader(file));
          String text = null;
    
    
            int i = 0;
            int ind = 0;
            int inp = 0;
            try {
    
                //use scanner to read the file
                Scanner scanner = new Scanner(file);
                while (scanner.hasNext()) {
                     ind= Integer.parseInt(scanner.next());
                     if (ind < 1 || ind > 10){
                         System.out.println();
                         scanner.next();
                     }
                     else {
                         inp = Integer.parseInt(scanner.next());
                          getNums()[ind-1] = inp;
    
                     }
    
                     i++;
                }
            } catch (FileNotFoundException e) {
            }
    
          		 
            for ( i = 0; i < getExcuses().length;i++){
                if (getNums()[i] == 99){
                    
                    javax.swing.JOptionPane.showMessageDialog(null,"Excuses name   # of excuses");
                    javax.swing.JOptionPane.showMessageDialog(null,getExcuses()[i] + "    " + 0);
                    
                    
                }
                else {
                    
                    javax.swing.JOptionPane.showMessageDialog(null,"Excuses name   # of excuses");
    
                    javax.swing.JOptionPane.showMessageDialog(null,getExcuses()[i] + "     " + getNums()[i]); 
                    
                   }
            }
    
            //print output
                    
            javax.swing.JOptionPane.showMessageDialog(null,"The Excuse " + getExcuses()[max()] + " had the max numer of excuses\n"
            + "The Excuse " + getExcuses()[min()] + " had the min numer of excuses\n"+
             "Total number of excuses: " + total() );
          
                }
    
    
        //get the max
        static int max(){
            int maxValue = getNums()[0];
            int pos = 0;
            for(int i=1;i < getNums().length;i++){
                if(getNums()[i] > maxValue){
                maxValue = getNums()[i];
    
                pos = i;
                }
            }
            return pos;
        }
    
        //get the min
         static int min(){
             for (int i =0; i < getNums().length;i++){
                 if (getNums()[i] == 0){
                     getNums()[i] = 99;
                 }
             }
            int minValue = getNums()[0];
            int pos = 0;
            for(int i=1;i < getNums().length;i++){
                if(getNums()[i] < minValue){
                minValue = getNums()[i];
                pos = i;
                }
            }
            return pos;
        }
    
    	 //get the total
        static int total(){
            int tot = 0;
            for(int i = 0; i < getNums().length;i++){
                tot += getNums()[i];
            }
            return tot;
        }
    
        /**
         * @return the Excuses
         */
        public static String[] getExcuses() {
            return Excuses;
        }
    
        /**
         * @param aExcuses the Excuses to set
         */
        public static void setExcuses(String[] aExcuses) {
            Excuses = aExcuses;
        }
    
        /**
         * @return the nums
         */
        public static int[] getNums() {
            return nums;
        }
    
        /**
         * @param aNums the nums to set
         */
        public static void setNums(int[] aNums) {
            nums = aNums;
        }
    
    
    
    }//end Array class

  2. #2
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Printing out to dialog box

    Java Code:
     for ( i = 0; i < getExcuses().length;i++){
                if (getNums()[i] == 99){
                     
                    javax.swing.JOptionPane.showMessageDialog(null,"Excuses name   # of excuses");
                    javax.swing.JOptionPane.showMessageDialog(null,getExcuses()[i] + "    " + 0);
                     
                     
                }
    But my output is only one window at a time
    Look at your code you are telling it to show a new dialog with one message in it each time it iterates. Its doing exactly what you are telling it to do. Is there a way you can think of telling it to print the full contents in one dialog?

    also your get excuses doesn't iterate through and print out each excuse, it just returns the whole array.
    Last edited by mwr1976; 04-24-2012 at 05:51 AM.

  3. #3
    bigDaddyJohn is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Printing out to dialog box

    Quote Originally Posted by mwr1976 View Post
    Java Code:
     for ( i = 0; i < getExcuses().length;i++){
                if (getNums()[i] == 99){
                     
                    javax.swing.JOptionPane.showMessageDialog(null,"Excuses name   # of excuses");
                    javax.swing.JOptionPane.showMessageDialog(null,getExcuses()[i] + "    " + 0);
                     
                     
                }


    Look at your code you are telling it to show a new dialog with one message in it each time it iterates. Its doing exactly what you are telling it to do. Is there a way you can think of telling it to print the full contents in one dialog?

    also your get excuses doesn't iterate through and print out each excuse, it just returns the whole array.
    I still don't understand, call me slow...I orginally set this up as a println and it compile or prointed out correctly, I though it would do the same with JOPtion, I'm a bit slow......

  4. #4
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Printing out to dialog box

    There is no way to make it more clear.

    Look at your code you are telling it to show a new dialog with one message in it each time it iterates.
    Java Code:
    for ( i = 0; i < getExcuses().length;i++){
               if (getNums()[i] == 99){
                     
                   javax.swing.JOptionPane.showMessageDialog(null,"Excuses name   # of excuses");
                   javax.swing.JOptionPane.showMessageDialog(null,getExcuses()[i] + "    " + 0);
                     
                     
               }]
    This code says: i is your counter every time it loops if the value of your Array is equal to 99 show these two JOption Panes. getExcuses() does not iterate through the Array it just returns it. Its the same as saying Excuses[i]. Its only going to print the value at that index. Your getExcuses() either has to iterate through each index, or you could pass a String that stores the results of iterating through your String Array.

  5. #5
    bigDaddyJohn is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Printing out Correctly

    Ok, I've revised my code, after reading and getting my head out of my arse, but I missed something!

    My popup dialog box comes up and displays the information, rather displays in a one line than like:

    Excuse name # of excuses

    My dog ate it 15
    It got burned 12
    I lost it 18
    I ate it 23
    It fell down the sewer 16
    It got shredded 44
    I forgot it 19
    I can't remember where I put it 17
    Coffee spilled on it 0
    My daughter colored on it 1

    The Excuse Coffee spilled on it had the max numer of excuses
    The Excuse My daughter colored on it had the min numer of excuses
    Total number of excuses: 264

    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.Scanner;
    import javax.swing.JDialog;
    import javax.swing.JTextField;
    
    
    public class Array{
        
        static int[] nums = new int[10];
        
        static String[] Excuses = {"My dog ate it","It burned","I lost it","I ate it","It fell down the sewer", 
            "It shredded","I forgot it","I can't remember","Water spilled on it","A tornado got it"};
        
        public static void main(String[] args) throws FileNotFoundException{
            
            
          File file = new File("name.txt");
          StringBuilder contents = new StringBuilder();
          BufferedReader reader = null;
          reader = new BufferedReader(new FileReader(file));
          String text = null;
    
          
           
            int i = 0;
            int a = 0;
            int b = 0;
            try {
              //use scanner to read the file
                Scanner scanner = new Scanner(file);
                while (scanner.hasNext()) {
                     a= Integer.parseInt(scanner.next());
                     if (a < 1 || a > 10){
                         System.out.println("Invalid excuse number");                     
                         scanner.nextInt();
                     }
                     else {
                         b = Integer.parseInt(scanner.next());
                          nums[a-1] = b;
                         
                     }                 
                   
                     i++;
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
     
            JDialog c = new JDialog();
            JTextField d = new JTextField();
            d.setEditable(false);
            c.add(d); 
            //print output  
            String str = " ";
           
            str += ("Total number of excuses:  " + total());
            
            str +=("The Excuse " + Excuses[max()] + " had the max numer of excuses");
            str +=("The Excuse " + Excuses[min()] + " had the min numer of excuses");
            str +=("");
            
            
            javax.swing.JOptionPane.showMessageDialog(null,"Excuses name   # of excuses ");
    
            System.out.println("Excuse name  # of excuses");
            for ( i = 0; i < Excuses.length; i++){
                if (nums[i] == 99){
                    
                   
    
                    str +=("  " + Excuses[i] + "   " + 0);
                }
                else {
                    
                    
                    str +=("  " + Excuses[i] + "   " + nums[i]);
                }
            }
            d.setText(str);
            c.setVisible(true);
        }
        
        //get the total
        static int total(){
            int tot = 0;
            for(int i = 0; i < nums.length;i++){
                tot += nums[i];
            }
            return tot;
        }
        
        //get the max
        static int max(){
            int maxValue = nums[0];
            int pos = 0;
            for(int i=1;i < nums.length;i++){  
                if(nums[i] > maxValue){  
                maxValue = nums[i];  
                
                pos = i;
                }  
            }  
            return pos; 
        }
        
        //get the min
         static int min(){
             for (int i =0; i < nums.length;i++){
                 if (nums[i] == 0){
                     nums[i] = 99;
                 }
             }
            int minValue = nums[0];
            int pos = 0;
            for(int i=1;i < nums.length;i++){  
                if(nums[i] < minValue){  
                minValue = nums[i];           
                pos = i;
                }  
            }  
            return pos; 
        }
        
    }

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

    Default Re: Printing out to dialog box

    If you were to print out the String 'str' you woudl see that there are no line breaks in there.
    Not sure if a JTextField can have line breaks, though, so you may want to use a JTextArea.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Printing multiple arrays in 1 dialog box
    By turbopenguin in forum New To Java
    Replies: 9
    Last Post: 04-19-2012, 08:18 AM
  2. Replies: 0
    Last Post: 12-26-2011, 03:52 PM
  3. Replies: 3
    Last Post: 03-22-2010, 01:50 AM
  4. Replies: 0
    Last Post: 03-10-2010, 02:13 AM
  5. Printing (no dialog)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-04-2008, 09:36 AM

Posting Permissions

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