Ok, so I'm having a bit of trouble with understanding how swing interfaces work. Here's the problem. Write a Swing program that declares an empty array of grades with a maxium of 10. Implement a JOptionPane input box within a while loop to allow the user to enter grades using a -1 sentinel value.

So here is the code

/*
	Chapter 7:	Programming Assignment
	Programmer:	Your Name
	Date:
	Filename:	Averages.java
	Purpose:	This program creates a Swing interface for averaging grades
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.DecimalFormat;

public class Averages extends JFrame
{
	//construct components
	static JLabel title = new JLabel("Grades");
	static JTextPane textPane = new JTextPane();
	static int numberOfGrades = 0;
	static int total = 0;
 	static DecimalFormat twoDigits = new DecimalFormat ("##0.00");

	//construct  array
	static int[] grades = new int[10];

	//create the content pane
	public Container createContentPane()
	{

		//construct and populate the north panel
		JPanel northPanel = new JPanel();
			northPanel.setLayout(new FlowLayout());
			northPanel.add(title);

		//create the JTextPane and center panel
		JPanel centerPanel = new JPanel();
			textPane = addTextToTextPane();
			JScrollPane scrollPane = new JScrollPane(textPane);
				scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
				scrollPane.setPreferredSize(new Dimension(500, 200));
			centerPanel.add(scrollPane);


		//create Container and set attributes
		Container c = getContentPane();
					c.setLayout(new BorderLayout(10,10));
					c.add(northPanel,BorderLayout.NORTH);
					c.add(centerPanel,BorderLayout.CENTER);

		return c;

	}

	//method to add new text to the JTextPane - use the Document class
   	public static JTextPane addTextToTextPane()
	{
				Document doc = textPane.getDocument();
				try
				{
					//clear previous text
					doc.remove(0, doc.getLength());

					//insert title
					doc.insertString(0,"Grades\n",textPane.getStyle("large"));

					//insert grades and averages; use the for loop; to calculate average divide total by number of grades

					for(int j = 0; j<grades.length; j++)
					{
						doc.insertString(doc.getLength(), grades[j] + "\n",textPane.getStyle("large"));
					}
				 }
				catch(BadLocationException ble)
				{
					System.err.println("Couldn't insert text");
				}

				return textPane;
	}
	
	//method to sort arrays
	public static void sort(int tempArray[], int length)
	{
		//keep in mind that you are sorting an integer array, not a string array
			...........
	}
	//method to swap two elements of an array
	public static void swap(int swapArray[], int first, int second)
	{		.............
	
	}



	//main method executes at run time
  	public static void main(String args[])
   	{
		JFrame.setDefaultLookAndFeelDecorated(true);
      		Averages f = new Averages();
      		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//accept the first grade
		int	integerInput = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the grade (0-100) or -1 to exit"));


		//write the while loop to accept more grades and store them in the array; don't forget to keep count of the number of grades;
		//also, calculate the total score
		while (integerInput != -1)
		{

		}
		//call sort method

		//call to create the content pane and set attributes
		f.setContentPane(f.createContentPane());
		f.setSize(600,375);
		f.setVisible(true);

 	}
}

Don't mind some of the methods that are missing parts. I'm still working on them. The problem is this "//write the while loop to accept more grades and store them in the array; don't forget to keep count of the number of grades;
//also, calculate the total score
while (integerInput != -1)
{

}"
I tried putting in the JOPtionPane box with a for loop to manipulate the count, but that didn't work, so what am I suppose to do to manipulate the while loop?

Thank you for any help.

I think you are making this way too complicated for yourself...

int count = 0;
int[] arr = new int[10];
int num = 0;
while (count<10 && num!=-1)
{
    num = Integer.parseInt(JOptionPane.showInputDialog("Enter Integer Number "+(count+1)));
    if (num!=-1)
        arr[count] = num;
    count++;
}

Shouldn't that do the trick?

I think you are making this way too complicated for yourself...

int count = 0;
int[] arr = new int[10];
int num = 0;
while (count<10 && num!=-1)
{
    num = Integer.parseInt(JOptionPane.showInputDialog("Enter Integer Number "+(count+1)));
    if (num!=-1)
        arr[count] = num;
    count++;
}

Shouldn't that do the trick?

Well, yes and no. The way I wrote it was this:

//accept the first grade
int	integerInput = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the grade (0-100) or -1 to exit"));

while (count<9 && integerInput != -1)
{
    if (integerInput  != -1)
   {
  JOptionPane.showInputDialog(null, "Please enter the grade    (0-100) or -1 to exit"));
   count++;
    }
}
addTextToTextPane();

The problem is, is that it accepts the numbers, but when the JFrame Interface loads up. It displays this in the JTextPane:
Grades
0
0
0
0
0
0
0
0
0
0

Nothing but zeros and not the values the user entered.

That is because the way you wrote it doesn't save any of the values after they are read in - in this section of code, you ignore the return value of showInputDialog, hence, you never store the value the user entered, and it is lost.

JOptionPane.showInputDialog(null, "Please enter the grade (0-100) or -1 to exit"));

Furthermore, your code is probably supposed to stop asking the user to input values if the user ever enters -1. But it doesn't do that. It only stops prompting the user for input values if they enter -1 on their first guess; whereas it should stop if they enter -1 for any of their guesses.

I think you should take a closer look at the code that kvass posted for you. Considering that it is correct. I don't see how your text pane could possibly report the correct values when you never stored them anywhere; in kvass's code, he stores the values in an array. If you use kvass's code and modify your text pane code so that it grabs the values from the array (using a for loop), then your text pane will work correctly.

I'll look at it again.
I think I'm getting confused, thanks to the original skeleton of the program, which is this:

/*
	Chapter 7:	Programming Assignment
	Programmer:	Your Name
	Date:
	Filename:	Averages.java
	Purpose:	This program creates a Swing interface for averaging grades
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.DecimalFormat;

public class Averages extends JFrame
{
	//construct components
	static JLabel title = new JLabel("Grades");
	static JTextPane textPane = new JTextPane();
	static int numberOfGrades = 0;
	static int total = 0;
 	static DecimalFormat twoDigits = new DecimalFormat ("##0.00");

	//construct  array
	static int[] grades = new int[50];

	//create the content pane
	public Container createContentPane()
	{
		//create the JTextPane and center panel
		...................
			................
			................
			................
			................
			................
			................
			................
			

		//create Container and set attributes
		....................
			................
			................

		.............



	}

	//method to add new text to the JTextPane - use the Document class
   	public static JTextPane addTextToTextPane()
	{
		//Clear previous text
		.........

		//insert title
		.........

		//insert grades and averages; use the for loop; to calculate average divide total by number of grades
   		  	................
   		  	................
 			................
   			................
   			................
			................
   			
   	}


	//method to sort arrays
	public static void sort(int tempArray[], int length)
	{
		//keep in mind that you are sorting an integer array, not a string array
		...........

	//method to swap two elements of an array
	public static void swap(int swapArray[], int first, int second)
		.............

	//main method executes at run time
  	public static void main(String args[])
   	{
		JFrame.setDefaultLookAndFeelDecorated(true);
      		Averages f = new Averages();
      		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//accept the first grade
		int	integerInput = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the grade (0-100) or -1 to exit"));

		//write the while loop to accept more grades and store them in the array; don't forget to keep count of the number of grades; 
		//also, calculate the total score 
		while (integerInput != -1)
		{
			...................
		}
		//call sort method

		//call to create the content pane and set attributes
  		................
	    	................
      		................
 	}
}

Then you would just put this same exact statement:

int	integerInput = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the grade (0-100) or -1 to exit"));

into the while loop as well. And in the while loop, you would need to add your "integerInput" numbers into an int array. Kvass gave you basically all of the code. What are you confused about?

Look up how to use arrays in java, how to use while loops in java, and how to use for loops in java. Your teacher spelled out basically everything - his comments tell you what you need to do and the exact spot where you need to do it.

The prompt that you posted above says to write a program that uses a JOptionPane... designing your own GUI is cool, but in this case it is just making ur code convoluted. You shouldn't need JFrames, JPanels, etc. to write the code required. Just use JOptionPanes instead of Scanners as the input method. Unless the teacher specifically wants you to create your own GUI, JOptionPane.showMessageDialog() and JOptionPane.showInputDialog() should be enough to perform all the required tasks.

Ok I did rewrite the loop and the text pane and I still get the same. Results. Pretty done with it

Nevermind I got it to work. It was my methods that were screwed up. Thanks for the help guys. Now just need to the sort and swapmethods.

Alright -- so mark this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.