Results 1 to 6 of 6
  1. #1
    Praetorian is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default A little help for n00b? (Incompatible types)

    Hi all. Java n00b here,

    Taking an introductory course and am working on a simple program that involves calculating the average of three test scores. The main purpose of the assignment is to acquaint us with input and output panes. Got that fine, but I seem to be having issues with operations:

    Average_Ron_Arnette3.java:41: error: incompatible types
    average = (firstScore + secondScore + thirdScore);

    Can somebody point me in the right direction? I would greatly appreciate it.

    Here's code:

    Java Code:
    import javax.swing.JOptionPane;
    
    //This program will demonstrate String Variables
    //Author: Ron Arnette
    //Course: CTI110-80
    public class Average_Ron_Arnette3
    {
    	public static void main(String[] args)
    	{
    		String firstScore;  //The user's first test score.
    		String secondScore; //The user's second test score.
    		String thirdScore; //The user's third score.
    		double average;
    		
    		
    		
    		
    		//Get the user's first test score.
    		firstScore = 
    			JOptionPane.showInputDialog("Please enter your first test score.");
    			
    		//Confirm the value entered.
    		JOptionPane.showMessageDialog(null, "You entered " + firstScore);
    			
    		//Get the user's second test score.
    		secondScore =
    			JOptionPane.showInputDialog("Very well. Now then, please enter your second test score.");
    			
    		//Confirm the value entered.
    		JOptionPane.showMessageDialog(null, "You entered " + secondScore);
    		
    			
    		//Get the user's third test score.
    		thirdScore =
    			JOptionPane.showInputDialog("And finally, please enter your third test score.");
    			
    		//Confirm the value entered.
    		JOptionPane.showMessageDialog(null, "You entered " + thirdScore);
    			
    		//Now have these values calculated.
    		average = (firstScore + secondScore + thirdScore)/ 3;
    																
    		System.exit(0);
    													
    			
    	}	
    }
    Just remembered to convert strings to numbers:

    Java Code:
    import javax.swing.JOptionPane;
    
    //This program will demonstrate String Variables
    //Author: Ron Arnette
    //Course: CTI110-80
    public class Average_Ron_Arnette3
    {
    	public static void main(String[] args)
    	{
    		String firstScore;  //The user's first test score.
    		String secondScore; //The user's second test score.
    		String thirdScore; //The user's third score.
    		double average;
    		
    		
    		
    		
    		//Get the user's first test score.
    		firstScore = 
    			JOptionPane.showInputDialog("Please enter your first test score.");
    			
    		//Convert the value entered to a double.
    			firstScore = Double.parseDouble(inputString);
    			
    		//Confirm the value entered.
    		JOptionPane.showMessageDialog(null, "You entered " + firstScore);
    			
    		//Get the user's second test score.
    		secondScore =
    			JOptionPane.showInputDialog("Very well. Now then, please enter your second test score.");
    			
    		//Convert the value entered to a double.
    		secondScore = Double.parseDouble(inputString);
    			
    		//Confirm the value entered.
    		JOptionPane.showMessageDialog(null, "You entered " + secondScore);
    		
    			
    		//Get the user's third test score.
    		thirdScore =
    			JOptionPane.showInputDialog("And finally, please enter your third test score.");
    			
    		//Convert the value entered to a double.
    		secondScore = Double.parseDouble(inputString);
    			
    		//Confirm the value entered.
    		JOptionPane.showMessageDialog(null, "You entered " + thirdScore);
    			
    		//Now have these values calculated.
    		average = (firstScore + secondScore + thirdScore)/ 3;
    																
    		System.exit(0);
    													
    			
    	}	
    }
    Still no go....
    Last edited by pbrockway2; 06-16-2012 at 03:14 AM. Reason: code tags added

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: A little help for n00b? (Incompatible types)

    As the error said, "incompatible types", clearly it means that there are types incompatibility in your code. Java is a static-typed language. Once a variable given a type it cannot change. A string will always be string, an integer will always an integer. So you can't add three string variables and expect it become an integer for instance and then divide it. You need to convert those strings to numerals (integer) first and then you can do the calculation.

  3. #3
    Praetorian is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Re: A little help for n00b? (Incompatible types)

    Thanks wsaryada. I'm in the process of doing that now. Still having bugs, but I'll get them worked out. Thanks again.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: A little help for n00b? (Incompatible types)

    Hi Praetorian, welcome to the forums!

    When you post code, use the "code" tags. Put [code] at the start of the code and [/code] at the end so that formatting is preserved. There are other formatting tags you can use described here: BB Code List - Java Programming Forum

    -----

    In your updated code you attempt to convert from a string to a double value. But, as wsaryada says, variables have a static type, so to do this conversion you might need two variables - one corresponding to the string that showInputDialog() returns and another one corresponding to the double returned by parseDouble().

  5. #5
    Praetorian is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Re: A little help for n00b? (Incompatible types)

    Thanks pbrockway2! Glad to be a member. Yeah, I looked at a sample in my text that had done just was you suggested. I saw the logic fixed it right up!

    Here's what I finished with:

    Java Code:
    import javax.swing.JOptionPane;
    
    //This program will demonstrate String Variables
    //Author: Ron Arnette
    //Course: CTI110-80
    public class Average_Ron_Arnette
    {
    	public static void main(String[] args)
    	{
    		String inputString;  //The user's first test score.
    		String inputString2; //The user's second test score.
    		String inputString3; //The user's third test score.
    		double firstScore;   //The first input string converted to double.
    		double secondScore;  //The second input string converted to double.
    		double thirdScore;   //The third input string converted to double.
    		double average;      //The average of the 3 inputs that will be displayed.
    		
    		
    		
    		
    		//Get the user's first test score.
    		inputString = 
    			JOptionPane.showInputDialog("Please enter your first test score.");
    			
    			
    		//Confirm the value entered.
    		JOptionPane.showMessageDialog(null, "You entered " + inputString);
    		
    		//Convert the value entered to a double.
    			firstScore = Double.parseDouble(inputString);
    			
    		//Get the user's second test score.
    		inputString2 =
    			JOptionPane.showInputDialog("Very well. Now then, please enter your second test score.");
    			
    		//Convert the value entered to a double.
    		secondScore = Double.parseDouble(inputString2);
    			
    		//Confirm the value entered.
    		JOptionPane.showMessageDialog(null, "You entered " + inputString2);
    		
    			
    		//Get the user's third test score.
    		inputString3 =
    			JOptionPane.showInputDialog("And finally, please enter your third test score.");
    			
    		//Convert the value entered to a double.
    		thirdScore = Double.parseDouble(inputString3);
    			
    		//Confirm the value entered.
    		JOptionPane.showMessageDialog(null, "You entered " + inputString3);
    			
    		//Now have these values calculated.
    		average = (firstScore + secondScore + thirdScore)/ 3;
    		
    		//Display Average.
    		JOptionPane.showMessageDialog(null, "Alright then, according to my calculations" +
    														"your test average is " + average);
    																
    		System.exit(0);
    													
    			
    	}	
    }
    Last edited by Praetorian; 06-16-2012 at 07:15 AM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: A little help for n00b? (Incompatible types)

    I'm glad you've got it sorted out! Don't forget the [code] ... [/code] tags - you should be able to edit your post to add them. (There's an "Edit Post" link at the bottom of the panel where the post appears).

Similar Threads

  1. incompatible types
    By angelicatomnob in forum New To Java
    Replies: 3
    Last Post: 01-17-2012, 03:25 PM
  2. incompatible types!!
    By sneha343 in forum New To Java
    Replies: 7
    Last Post: 11-30-2011, 11:49 PM
  3. incompatible types
    By effa in forum New To Java
    Replies: 16
    Last Post: 02-03-2011, 09:50 AM
  4. Incompatible types
    By bayan in forum New To Java
    Replies: 5
    Last Post: 11-04-2010, 08:43 AM
  5. Incompatible types
    By coltragon in forum New To Java
    Replies: 5
    Last Post: 01-15-2010, 04:47 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
  •