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!
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
Re: Printing out to dialog box
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);
}
Quote:
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.
Re: Printing out to dialog box
Quote:
Originally Posted by
mwr1976
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......
Re: Printing out to dialog box
There is no way to make it more clear.
Quote:
Look at your code you are telling it to show a new dialog with one message in it each time it iterates.
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.
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
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;
}
}
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.