Print integer Array values into a single !, showMessagedialog
Hello, dear programmers
"I began this year with java, and i have a question about how to print the values of an integer Array into a Single showMessageDialog. The issue is that the program is printing the values in different shoMessageDialogs. "
The program is meant to find the random value in the Random int variable
This is my code:
Code:
package testa;
import javax.swing.*;
import java.util.*;
import java.lang.*;
public class Testa {
public static void main(String[] args){
int Random = (int)(Math.random()*10);
int[] Array = new int[10];
Scanner read = new Scanner(System.in);
// start programm read 10 ints into array
for(int index = 0; index<Array.length;index++){
Array[index] = read.nextInt();
if(Array[index]>Random)
{
System.out.println("You are higher");
}
else if(Array[index]<Random)
{
System.out.println("You are lower");
}
if(Array[index]==Random)
{
System.out.println("Good job! you found the searching number " + Random);
for(int i =0; i < Array.length;i++)
{
if(Array[i]!=0)
{
JOptionPane.showMessageDialog(null, "You Have Entered the values:"+Array[i]);
}
}
break;
}
if(index == 9)
{
System.out.println("You are failed bitch!");
break;
}
}
};
:(handshake):
Iam very glad if you can help me
Re: Print integer Array values into a single !, showMessagedialog
Ah and... where is your problem in printing it in one single dialog?
Re: Print integer Array values into a single !, showMessagedialog
Quote:
Originally Posted by
Sierra
Ah and... where is your problem in printing it in one single dialog?
if(Array[i]!=0)
{
JOptionPane.showMessageDialog(null, "You Have Entered the values:\n"+Array[i]);
break;
}
here is the problem, it prints for each int valueau a other messagedialog
Re: Print integer Array values into a single !, showMessagedialog
Make a String variable, put all that is output in multiple dialogs currently, into one large string... should not be a difficult task as you have all there:
- Join strings by the plus sign ( "You Have Entered the values:\n" + Array[i]
- Show the dialog ( JOptionPane.showMessageDialog(...) )
- A loop ( for (...) )
Sorry, where is your problem? You go through the loop, join all strings together in a variable of type String. Move the message dialog out of the loop as you do not want multiple... and there you go.
Show a message dialog showing the string... separate maybe the lines by the "\n" character: for ( ... ) myOutput += myLine + "\n";
Re: Print integer Array values into a single !, showMessagedialog
Quote:
Originally Posted by
Sierra
Make a String variable, put all that is output in multiple dialogs currently, into one large string... should not be a difficult task as you have all there:
- Join strings by the plus sign ( "You Have Entered the values:\n" + Array[i]
- Show the dialog ( JOptionPane.showMessageDialog(...) )
- A loop ( for (...) )
Sorry, where is your problem? You go through the loop, join all strings together in a variable of type String. Move the message dialog out of the loop as you do not want multiple... and there you go.
Show a message dialog showing the string... separate maybe the lines by the "\n" character: for ( ... ) myOutput += myLine + "\n";
I did this one:
for(int i =0; i < Array.length;i++)
{
if(Array[i]!=0)
{
JOptionPane.showMessageDialog(null, "You Have Entered the values:\n"+Arrays.toString(Array));
break;
}
This absolute works, it displays 1 single popup, but i dont want to see the 0 values. How can I remove the 0 values, i dont want to display them
1 Attachment(s)
Re: Print integer Array values into a single !, showMessagedialog