Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Average program with array and loop and JOptionPane.

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    10
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Average program with array and loop and JOptionPane.

    The project is to do a grade averaging program in a loop using JOptionPane.
    Bellow is the errors I am getting, does int[] array = new int[numberOfGradesString]; have to be defined like int[] array = new int[100];?
    Because I have seen examples that compiled Java: Arrays





    Thank you for the help.

    import javax.swing.*;
     
    public class Project2TestEdit1 {
    	public static void main( String[] args ) {
     
     
    		//...Varibles declared for whole program.
     
    		String numberOfGradesString, gradeString;
    		int average, sumOfGrades, selection;
    		int numberOfGrades = 1;
    		double quotient;  
    	  	boolean runAgain = true;  		  
     
     
    		  //...Loop
    		  do{
     
     
    				//...Enter a Grade to be averaged for each iteration of the loop.
    				gradeString = JOptionPane.showInputDialog( "Enter a grade to be averaged." );
     
    				//...Asks if you want to run anouther.
    				selection = JOptionPane.showConfirmDialog(null,
    			   				  "Would you like to run another?","Confirmation",
    									JOptionPane.YES_NO_OPTION);
     
    				//...Runs loop again if option YES is selected.
    				runAgain = (selection == JOptionPane.YES_OPTION);
     
    				//...Adds anouther place value to the array increment each iteration of the loop.
    				numberOfGrades = numberOfGrades++;
     
      		 //...Continues the loop.	   
    		}while (runAgain);
     
    			//...The array, each iteration of the loop a place value is added with the grade intered as the value.
    			int[] array = new int[numberOfGradesString {gradeString}];
     
    				//...Avergaing formula.
     
    				sumOfGrades = numberOfGrades + gradeString;
    				average = (sumOfGrades / numberOfGrades);
     
    				//...
    			  	JOptionPane.showMessageDialog(null,
    								average + " Is the Average" );
     
             System.exit(0); //...Ends the window.  
       }
    }
     
    ERRORS
         ----jGRASP exec: javac -g C:\Documents and Settings\jeremy\Desktop\SCHOOL\CMIS 141\week 7\Project2TestEdit1.java
     
        Project2TestEdit1.java:45: incompatible types
        found   : java.lang.String
        required: int
        			int[] array = new int[numberOfGradesString];
        			                      ^
        Project2TestEdit1.java:49: incompatible types
        found   : java.lang.String
        required: int
        				sumOfGrades = numberOfGrades + gradeString;
        				                             ^
        2 errors
     
         ----jGRASP wedge2: exit code for process is 1.
         ----jGRASP: operation complete.


  2. #2
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 6 Times in 5 Posts

    Default Re: Average program with array and loop and JOptionPane.

    int[] array = new int[numberOfGradesString]
    found String, expect int.

    try using Integer.parseInt(numberOfGradesString)
    [B][U]int[] array = new int[Integer.parseInt(numberOfGradesString)][/U][/B]
    ---
    basically, you are trying to set the size of the array using a String when really it has to be a number...the code above should fix everything.

    still confused? google "convert string to int java"

    example: Convert string to int : ConvertLanguage BasicsJava
    Last edited by helloworld922; October 25th, 2009 at 12:06 AM.

  3. The Following User Says Thank You to rsala004 For This Useful Post:

    jeremykatz (October 25th, 2009)

  4. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    10
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Average program with array and loop and JOptionPane.

    Now I am getting this error?

    Also am I missing something here gradeString is the value entered for the grade, numberOfGradesString defines the size of the arrayand array is the name of the array. So shouldn't I some how have gradeString defined in
    int[] array = new int[Integer.parseInt(numberOfGradesString)];

    sumOfGradesString = numberOfGrades + gradeString;
    average = (sumOfGradesString / numberOfGradesString);

    Project2TestEdit1.java:50: operator / cannot be applied to java.lang.String,java.lang.String
    average = (sumOfGradesString / numberOfGradesString);
    ^
    1 error

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

    Thank you.

  5. #4
    Junior Member
    Join Date
    Oct 2009
    Posts
    10
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Average program with array and loop and JOptionPane.

    /*...I think this fixes my array but I am not sure and I have a few other qestions, gradeString is the actual value entered for each grade, numberOfGradeStrings keeps track of each iteration of the loop and makes up the array size, array is just the name of the array.*/

    int[] array = new int[Integer.parseInt(numberOfGradesString)];

    Am I right in this logic or do I have things confused?

    Can a string not be a part of an arithmetic problem?

    /*This is the last compile error that I am getting.
    Error
    Project2TestEdit1.java:50: operator / cannot be applied to java.lang.String,java.lang.String
    average = (sumOfGradesString / numberOfGradesString);
    /\ */


    import javax.swing.*;
     
    public class Project2TestEdit1 {
    	public static void main( String[] args ) {
     
     
    		//...Varibles declared for whole program.
     
    		String numberOfGradesString, gradeString, sumOfGradesString;
    		int average, selection;
    		int numberOfGrades = 1;
    		double quotient;  
    	  	boolean runAgain = true;  		  
     
     
    	 //...Loop
    	 do{
     
     
    	//...Enter a Grade to be averaged for each iteration of the loop.
    	gradeString = JOptionPane.showInputDialog( "Enter a grade to be averaged." );
     
    	//...Asks if you want to run anouther.
    	selection = JOptionPane.showConfirmDialog(null,
    	"Would you like to run another?","Confirmation",
    									JOptionPane.YES_NO_OPTION);
     
    	//...Runs loop again if option YES is selected.
    	runAgain = (selection == JOptionPane.YES_OPTION);
     
    	//...Adds anouther place value to the array increment each iteration of the loop.
    		numberOfGrades = numberOfGrades++;
     
      		 //...Continues the loop.	   
    	}while (runAgain);
     
    	//...The array, each iteration of the loop a place value is added with the grade intered as the value.
    	int[] array = new int[Integer.parseInt(numberOfGradesString)];
     
    	//...Avergaing formula.
     
    	sumOfGradesString = numberOfGrades + gradeString;
    	average = (sumOfGradesString / numberOfGradesString);
     
    	//...
    	JOptionPane.showMessageDialog(null,
    	average + " Is the Average" );
     
    System.exit(0); //...Ends the window.  
       }
    }

  6. #5
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Average program with array and loop and JOptionPane.

    You cannot mix Strings with primitive types to perform arithmetic operations.

    The way you initialized the array in your first code snippet is correct.

  7. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    10
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Average program with array and loop and JOptionPane.

    Quote Originally Posted by literallyjer View Post
    You cannot mix Strings with primitive types to perform arithmetic operations.
    I tried declaring them as int and got an error expecting string, so I changed them to string and got an arithmetic error. Is there another way that I should declare these variables.

    String numberOfGradesString, gradeString, sumOfGradesString;

    Quote Originally Posted by literallyjer View Post
    The way you initialized the array in your first code snippet is correct.
    So I do have this array right, I have not been able to compile due to other errors, so I have not been able to prove that I have variable which defines the size of my array right. Don't I have to declare gradeString in the array some were since that is the actual value being held in the array?

    int[] array = new int[Integer.parseInt(numberOfGradesString)];
    Last edited by helloworld922; October 25th, 2009 at 02:28 PM.

  8. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Average program with array and loop and JOptionPane.

    No, you need some of them as Strings and some of them as int's. Int's can automatically be casted up to Strings, but not the other way around. Also, Strings don't really have any mathematical operators that can be used with them except '+', which takes the first string and combines it with the second string (ex: what is "hello" / "world" suppose to mean?). Now, the Integer class does provide a handly little method that will perform the necessary conversion from String to int, and you must use this method to do any "real" math that your program needs. The method is called Integer.parseInt().

Similar Threads

  1. Using Icons in JOptionPane
    By Jchang504 in forum AWT / Java Swing
    Replies: 3
    Last Post: September 19th, 2014, 02:28 PM
  2. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM
  3. JOptionPane Question/ Printing
    By 03EVOAWD in forum AWT / Java Swing
    Replies: 2
    Last Post: August 31st, 2009, 09:17 AM
  4. help me to do this Array Program
    By fahdsaad in forum Member Introductions
    Replies: 1
    Last Post: June 30th, 2009, 02:19 AM
  5. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 04:31 AM