Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Sierra

Thread: Print integer Array values into a single !, showMessagedialog

  1. #1
    Join Date
    Dec 2012
    Location
    Netherlands
    Posts
    4
    Rep Power
    0

    Default 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:
    Java 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;
            
        }
        
        }
            };


    Iam very glad if you can help me

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Print integer Array values into a single !, showMessagedialog

    Ah and... where is your problem in printing it in one single dialog?
    Kadir Karapinar likes this.
    I like likes!

  3. #3
    Join Date
    Dec 2012
    Location
    Netherlands
    Posts
    4
    Rep Power
    0

    Default Re: Print integer Array values into a single !, showMessagedialog

    Quote Originally Posted by Sierra View Post
    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

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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";
    I like likes!

  5. #5
    Join Date
    Dec 2012
    Location
    Netherlands
    Posts
    4
    Rep Power
    0

    Default Re: Print integer Array values into a single !, showMessagedialog

    Quote Originally Posted by Sierra View Post
    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

  6. #6
    Join Date
    Dec 2012
    Location
    Netherlands
    Posts
    4
    Rep Power
    0

    Default Re: Print integer Array values into a single !, showMessagedialog

    Print integer Array values into a single !, showMessagedialog-issue.jpgClick image for larger version. 

Name:	issue.jpg 
Views:	6 
Size:	27.6 KB 
ID:	4302Click image for larger version. 

Name:	issue.jpg 
Views:	6 
Size:	27.6 KB 
ID:	4302Click image for larger version. 

Name:	issue.jpg 
Views:	6 
Size:	27.6 KB 
ID:	4302Click image for larger version. 

Name:	issue.jpg 
Views:	6 
Size:	27.6 KB 
ID:	4302

Similar Threads

  1. How to print the integer value of char : '
    By fatabass in forum New To Java
    Replies: 2
    Last Post: 04-03-2012, 11:08 PM
  2. How to Print every tenth integer in my output
    By jim01 in forum New To Java
    Replies: 13
    Last Post: 04-16-2011, 03:00 AM
  3. cant print 2d array(giving null values)
    By fneeks in forum New To Java
    Replies: 1
    Last Post: 04-07-2010, 08:48 PM
  4. An Array of different integer values
    By lithium002 in forum New To Java
    Replies: 7
    Last Post: 12-04-2009, 05:25 AM
  5. How to make print function for single label?
    By makpandian in forum AWT / Swing
    Replies: 2
    Last Post: 02-12-2009, 05:26 PM

Tags for this Thread

Posting Permissions

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