Results 1 to 4 of 4
  1. #1
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default Printing multiple array elements using only one dialog box using a for loop

    I am trying on how to print values from an array all to show up on a dialog using a for loop. Can anybody help me with this?
    Java Code:
    double[] dailyTemperatures;
    		  dailyTemperatures= new double[7];
    		  for (int d = 0; d<dailyTemperatures.length; d++)
    		  		{
    		  			String t;
    		  			t = JOptionPane.showInputDialog(null, "Enter daily temperature values:");
    		  			dailyTemperatures[d] = Double.parseDouble(t);
    		        }// end for loop
    		  for (int i =0; i <= 6; i++)
    i want to print all my values in just one dialog box. I would rather use a for loop but if you guys can not tell me another way it is okay
    Last edited by soccer_kid_6; 03-22-2010 at 01:58 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    First it might help to make your proposed task precise ... but not too precise. (Didn't Einstein say something along those lines?) Do you really have to use a for loop? Are all the array elements to be printed, or just some of them? What is the type of the Array?

    Secondly, post your code. That gives everybody something to work with.

    If you're stuck on how to begin have a look at the Arrays.toString() methods. And the JOptionPane class. Then write your code and post it.

    If you want some particular format for the array contents, try writing a small class with a single method that does just that: takes an array of the correct sort and prints the string to the console. That is, split your problem in two. One part to generate the string, the other part to display it in a dialog box.

  3. #3
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default

    i want to print all my values in just one dialog box. I would rather use a for loop but if you guys can not tell me another way it is okay

    Also i edited my post with code

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Do you know about string concatenation? Inside the for loop that you have started concatenate the array values (and spaces).

    Java Code:
    String foo = "hel";
    String bar = "lo";
    String baz = "world";
    String space = " ";
        // string concatenation builds long strings from short ones
    System.out.println(foo + bar + space + baz);

    Concatenating strings is not so different from using a for loop to add the array elements together. Finding the sum would look something like

    Java Code:
    // first for loop as before
    double sum = 0.0; // give it a sensible starting value
    for(int i = 0; i < 7; i++) { //(not <=6)
        sum = sum + dailyTemperatures[i];
    }
    // now you are in a position to display the total
    Try something like that for making a String rather than a double total. Post if you get stuck.

Similar Threads

  1. Replies: 0
    Last Post: 03-10-2010, 03:13 AM
  2. Printing Array elements
    By new person in forum New To Java
    Replies: 2
    Last Post: 02-23-2009, 09:57 PM
  3. Help printing specific ArrayList elements
    By CirKuT in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 01:24 AM
  4. Replies: 2
    Last Post: 06-03-2008, 10:07 PM
  5. Printing (no dialog)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-04-2008, 10: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
  •