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

    Default problems with print statement

    The output that I want is this
    The out put should look like this:
    * The grade at index 0 is ___
    * The grade at index 1 is ___
    * The grade at index 2 is ___

    Java Code:
     import java.util.Scanner;
    import javax.swing.JOptionPane;
    class Homework7
    {
    	public static void printArray(int[] print)
    		{
    			String sum = "";
    		    for (int i = 0; i<print.length; i++)
    		    {
       		   		sum = sum + print[i] + "\n";
    			}
    		 	JOptionPane.showMessageDialog(null, "The grade at index " + i + "is " + sum );
    		}
    
    	public static void main(String[] args)
    		{
    			Scanner scanner = new Scanner(System.in);
    			int[] myGrades ;
    			System.out.println("Enter number of Exams:");
    				int numTests = scanner.nextInt();
    			myGrades  = new int[numTests];
    			System.out.println("The number of tests is: " + numTests);
    		    for (int i = 0; i<myGrades.length; i++)
    		    {
    			   System.out.println("Enter Grades:");
    			   myGrades[i] = scanner.nextInt();
    		    }// end for loop
    		    printArray(myGrades);
    }
    }
    My code is not working and I can't get it to work the way i want it to print as i posted above is what i want while using a dialog box

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    If you want to use line breaks in a label (which is what you're doing when displaying a String in a JOptionPane) you need to use HTML tags (i.e. <BR> not \n).

  3. #3
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    if u want to sum all the grades that are integers,then y u have taken sum variable as string,
    String sum = "";
    moreover variable i wont be availabel outside loop, so u have to take ur option pane inside loop.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by programmer_007 View Post
    if u want to sum all the grades that are integers,then y u have taken sum variable as string,

    moreover variable i wont be availabel outside loop, so u have to take ur option pane inside loop.
    Uhm, he is simply concatenating the output lines in that loop. "sum" is simply a badly chosen word for the variable.

  5. #5
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    If you want to use line breaks in a label (which is what you're doing when displaying a String in a JOptionPane) you need to use HTML tags (i.e. <BR> not \n).
    it seems he just want to format the output by line breaks and a all .. is that it ?

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

    Default

    Sorry i was doing this late last night and didn't really care for variable names. My problem isnt the line breaks I just cant get method to print out this way.
    The grade at index 0 is ___
    The grade at index 1 is ___
    all in one dialog box.
    Anybody know the code?

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by soccer_kid_6 View Post
    Sorry i was doing this late last night and didn't really care for variable names. My problem isnt the line breaks I just cant get method to print out this way.
    The grade at index 0 is ___
    The grade at index 1 is ___
    all in one dialog box.
    Anybody know the code?
    And the problem there are the line breaks, as well as modifying the String concatenation in your for loop, rather than doing it in the JOptionPane statement.
    Last edited by masijade; 03-22-2010 at 01:22 PM.

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

    Default

    Quote Originally Posted by masijade View Post
    And the problem there are the line breaks, as well as modifying the String concatenation in your for loop, rather than doing it in the JOptionPane statement.
    im pretty sure all i have to do is use a loop i just can't get it to work

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    IOW not just
    Java Code:
    sum = sum + print[i] + "\n";
    in the for loop, but
    Java Code:
    sum = sum + "starting string" + print[i] + "<BR>";
    // or sum += ..... rather than sum = sum + .......
    Like I said put the string concatenation in the for loop, not in the call to showMessageDialog.

Similar Threads

  1. add an If Else statement and......uh????
    By sonny in forum New To Java
    Replies: 6
    Last Post: 03-04-2010, 06:57 PM
  2. for statement help
    By helpisontheway in forum New To Java
    Replies: 5
    Last Post: 11-14-2009, 04:14 PM
  3. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM
  4. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM
  5. problems when I print in textarea
    By gabriel in forum New To Java
    Replies: 1
    Last Post: 07-26-2007, 06:57 PM

Posting Permissions

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