9 Replies - 1729 Views - Last Post: 04 April 2010 - 02:06 PM Rate Topic: -----

#1 Guest_Coalman*


Reputation:

Problem if JOption output message

Posted 04 April 2010 - 07:18 AM

Hey everyone I trying to create a program that will allow the user to enter in dialog boxes using javax.swing the number of tests to be graded then use an array to input the test scores. Then an output message saying the total number of test grades entered, and the average grade of the test grades in an dialog box. There probably problems elsewhere in my code but the main one Im having now is getting the output message to work, i keep getting errors.

Code:
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import javax.swing.JFrame;


public class apr2 {

public static void main(String[] args) {
int numOfGrades = 0; // number of grades
int[] scores; // Array of grades


// Get number of grades to be entered
String numOfGradesString = JOptionPane.showInputDialog(null,
"Please enter the number of grades to be averaged:",
"Easy Grader 1.0", JOptionPane.QUESTION_MESSAGE);

// Convert string into integer
numOfGrades = Integer.parseInt(numOfGradesString);

// Create array scores
scores = new int[numOfGrades];

// gets grades and avgs grade
for (int i = 0; i < scores.length; i++) {
String scoreString = JOptionPane.showInputDialog(null,
"Please enter a score:",
"Easy Grader 1.0", JOptionPane.QUESTION_MESSAGE);

// Convert string into integer
scores[i] = Integer.parseInt(scoreString);

int sum = 0;
for (int x = 0; x < scores.length; i++)
sum += scores[x] / numOfGrades;
}

// Display the result
----> problem area: JOptionPane.showMessageDialog(null, "Your final grade is: " sum + \n "Your letter grade earned is :" + \n "Total number of grades entered: " numOfGrades, JOptionPane.INFORMATION_MESSAGE "Easy Grader 1.0);



System.exit();
}
}



Error msg:
')' expected

This is a project for my java class that the instructor came up with. He expects if he can do it we can do it. The only problem is he has a bachelors degree in this stuff from a really good school and this is my first java class. Im not sure if i am on the right track or not, I dont know how to fix that error and I looked at almost every swing tutorial there is and I just cant get it. Im probably going to fail.

Is This A Good Question/Topic? 0

Replies To: Problem if JOption output message

#2 CoalRegion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-September 09

Re: Problem if JOption output message

Posted 04 April 2010 - 07:40 AM

Sorry also getting this error:
illegal character: \92

The post above was mine, forgot to login sorry.

Also Happy Easter coders!

This post has been edited by CoalRegion: 04 April 2010 - 07:40 AM

Was This Post Helpful? 0
  • +
  • -

#3 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Problem if JOption output message

Posted 04 April 2010 - 08:15 AM

Firstly, the JOptionPane showMessageDialog have these parameters below

showMessageDialog(Component, Object)
showMessageDialog(Component, Object, String, int)
showMessageDialog(Component, Object, String, int, Icon)

For example...
JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.");


You have got this..
JOptionPane.showMessageDialog(null, "Your final grade is: " sum + \n "Your letter grade earned is :" + \n "Total number of grades entered: " numOfGrades, JOptionPane.INFORMATION_MESSAGE "Easy Grader 1.0);


Firstly, initialize sum as a global variable not within the for loop.
Secondly, when using the \n, it has to be like this
"\n new line.."


Edit -

Once you have done these changes, make sure your JOptionPane's parameters matches one of the ones above.

This post has been edited by m-e-g-a-z: 04 April 2010 - 08:17 AM

Was This Post Helpful? 0
  • +
  • -

#4 CoalRegion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-September 09

Re: Problem if JOption output message

Posted 04 April 2010 - 08:41 AM

Im confuesed on how to add more to the output dialog box. I need to add the average and the number of tests entered.

this works for just the average:
JOptionPane.showMessageDialog(null, "The average is " + total);



My mind if numb right now I have been working on this project for 2 and a half weeks now and Im sick of it every stinking project he gives hes the only one who knows how to do it. Getting people frustrated beyond belief with this is ridiculous. I have a lot of other troubling things going on in my life right now and spending over 30+ hours on this is not helping anything. Sorry I needed to vent. I just cant figure out how to add additional elements to the output dialog. Sorry for being a pita.

This post has been edited by CoalRegion: 04 April 2010 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

#5 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Problem if JOption output message

Posted 04 April 2010 - 08:55 AM

I am sorry to hear about your problems, taking a short 15 min break can always do wonders.

Anyways back to the topic,

If its the component and object JOption Pane you want to use, adding a variable is only the matter of adding it to each speech mark.

int  average=86;
int tests=4;
JOptionPane.showMessageDialog(null, " The average is " +average+" and the number of tests taken are "+tests+".");


Was This Post Helpful? 1
  • +
  • -

#6 CoalRegion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-September 09

Re: Problem if JOption output message

Posted 04 April 2010 - 09:05 AM

OH I see now. Thank you very much This has been driving me crazy. I been having some tough personal problems which makes this even harder. Thank you very much tho.
Was This Post Helpful? 0
  • +
  • -

#7 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Problem if JOption output message

Posted 04 April 2010 - 09:10 AM

No Problem, glad i could help :)
Was This Post Helpful? 0
  • +
  • -

#8 CoalRegion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-September 09

Re: Problem if JOption output message

Posted 04 April 2010 - 01:47 PM

Ok Im not using arrays anymore for this project hehe its driving me up a wall so I used an accumulator instead and everything works now and here is my code:
//forgot had personal info in here whooops

import javax.swing.*;

public class Another {
    public static void main(String[] args) {

        //... Local variables
        String intStr;         // String version of the input grade.
        int    total = 0;      // total before division
        int numOfGrades;	   // number of grades user wishes to have averaged/scored
        int count = 0;		   // just a simple counter
        int grades;			   // input grades
		int onGrade = 1;			   // The number for the test input counter during the loop
        int average = 0;	   // average of grades for output
        String letter = "";

		String numOfGradesString = JOptionPane.showInputDialog(null,
		"Please enter the number of grades to be averaged:",
		"Easy Grader 1.0", JOptionPane.QUESTION_MESSAGE);
		numOfGrades = Integer.parseInt(numOfGradesString);


        //while loop reading grades until user hits CANCEL or leaves input blank
        while (count < numOfGrades)
         {

------>            intStr = JOptionPane.showInputDialog(null, " Please enter a grade ","Grade:" +onGrade+", Of "+numOfGrades+",  ." ); 
            if (intStr == null) break;       // for cancel button and x
            if (intStr.equals("")) break;    // when user does not enter anything for input

            int n;
            n = Integer.parseInt(intStr);    // Convert string to integer

            total = total + n ;              // acculamator total
            average = total / numOfGrades;	 // average of all grades
            count++;				 // just a counter bud
            onGrade++;
        }
		//assigns a letter grade to the average
 		if (average >= 90)
              {letter = "A";}
           else if(average >= 80)
          	    {letter = "B";}
           	else if(average >=70)
        	      {letter = "C";}
       	   		else if(average >=60)
              		{letter = "D";}
        	     else
              {letter ="F";}


        //Output the average, letter grade, number of tests.

        JOptionPane.showMessageDialog(null, " The average is " +average+ ", \nThe number of tests entered are "+numOfGrades+", \nThe letter grade is " +letter);

    }
   }




The problem I am having now is instead of the test grade dialog saying "test 1 of (number entered) in the dialog box itself it displays it in the area for the user to enter the text. Anyone have any thoughts on this?
Thanks again and I bolded the area of code I am talking about

Edit:
Bold did not seem to be working so I drew another arrow.

This post has been edited by CoalRegion: 04 April 2010 - 07:07 PM

Was This Post Helpful? 0
  • +
  • -

#9 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Problem if JOption output message

Posted 04 April 2010 - 01:58 PM

Instead of

intStr = JOptionPane.showInputDialog(null, " Please enter a grade ","Grade:" +onGrade+", Of "+numOfGrades+",  ." );


It should be

intStr = JOptionPane.showInputDialog(null, " Please enter a grade Grade: " +onGrade+" Of "+numOfGrades+" ." ); 


You had some commas mixed up which i changed as you can see. By having these commas, you are having an extra Parameter within the JOptionPane thats why it was causing your text to display in the input dialog box.
Was This Post Helpful? 2
  • +
  • -

#10 CoalRegion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-September 09

Re: Problem if JOption output message

Posted 04 April 2010 - 02:06 PM

Thanks again, I have been looking at this wayyy to long I believe.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1