Results 1 to 6 of 6
Thread: Printing out to dialog box
- 04-24-2012, 05:17 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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
- 04-24-2012, 05:49 AM #2
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
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); }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?But my output is only one window at a time
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.
- 04-24-2012, 05:22 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
- 04-24-2012, 06:26 PM #4
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
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.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.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); }]
- 04-25-2012, 02:50 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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; } }
- 04-25-2012, 02:58 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
Printing multiple arrays in 1 dialog box
By turbopenguin in forum New To JavaReplies: 9Last Post: 04-19-2012, 08:18 AM -
Implement Custom Printing Logic & Enhanced Word Documents Printing
By sherazam in forum Java SoftwareReplies: 0Last Post: 12-26-2011, 03:52 PM -
Printing multiple array elements using only one dialog box using a for loop
By soccer_kid_6 in forum New To JavaReplies: 3Last Post: 03-22-2010, 01:50 AM -
Printing multiple array elements using only one dialog box using a for loop
By soccer_kid_6 in forum New To JavaReplies: 0Last Post: 03-10-2010, 02:13 AM -
Printing (no dialog)
By Java Tip in forum Java TipReplies: 0Last Post: 02-04-2008, 09:36 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks