Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-22-2010, 06:03 AM
Member
 
Join Date: Feb 2010
Posts: 23
Rep Power: 0
soccer_kid_6 is on a distinguished road
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 ___

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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 03-22-2010, 08:23 AM
Senior Member
 
Join Date: Jun 2008
Posts: 1,622
Rep Power: 4
masijade is on a distinguished road
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).
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-22-2010, 08:24 AM
programmer_007's Avatar
Senior Member
 
Join Date: Aug 2009
Posts: 125
Rep Power: 0
programmer_007 is on a distinguished road
Default
if u want to sum all the grades that are integers,then y u have taken sum variable as string,
Quote:
String sum = "";
moreover variable i wont be availabel outside loop, so u have to take ur option pane inside loop.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-22-2010, 08:44 AM
Senior Member
 
Join Date: Jun 2008
Posts: 1,622
Rep Power: 4
masijade is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-22-2010, 08:52 AM
programmer_007's Avatar
Senior Member
 
Join Date: Aug 2009
Posts: 125
Rep Power: 0
programmer_007 is on a distinguished road
Default
Quote:
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 ?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 03-22-2010, 01:14 PM
Member
 
Join Date: Feb 2010
Posts: 23
Rep Power: 0
soccer_kid_6 is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 03-22-2010, 01:20 PM
Senior Member
 
Join Date: Jun 2008
Posts: 1,622
Rep Power: 4
masijade is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 03-22-2010, 01:41 PM
Member
 
Join Date: Feb 2010
Posts: 23
Rep Power: 0
soccer_kid_6 is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 03-22-2010, 02:24 PM
Senior Member
 
Join Date: Jun 2008
Posts: 1,622
Rep Power: 4
masijade is on a distinguished road
Default
IOW not just
Code:
sum = sum + print[i] + "\n";
in the for loop, but
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 09:09 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org