Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By brookeblood1

Thread: Need help with errors in Add/Subtract Program!

  1. #1
    brookeblood1 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Need help with errors in Add/Subtract Program!

    Hello Everyone! I just got started with Java, and other than the basic 'Hello World!" program, I have yet to make a working program.

    I am attempting to write a program that asks the user whether they want to add or subtract two numbers, have them input the numbers, and complete the equation. This code successfully worked with just adding OR subtracting, but I believe the if, then statement is what is messing it up. Here is the current code.

    Java Code:
    import java.util.Scanner ;
    public class AddSub {
    public static void main (String[] args) {
    Scanner input = new Scanner (System.in);
    int num1, num2, sum, diff ;
    System.out.println ("Type 1 for addition, 2 for subtraction.") ;
    if (input.nextInt=1) {
    System.out.println ("Enter first number."); num1 = input.nextInt();
    System.out.println ("Enter second number."); num2 = input.nextInt();
    sum = num1 + num2 ;
    System.out.printf ("Sum = %d\n" , sum);
    	}
    if (input.nextInt=2) {
    System.out.println ("Enter first number."); num1 = input.nextInt();
    System.out.println ("Enter second number."); num2 = input.nextInt();
    diff = num1 - num2 ;
    System.out.printf ("Difference = %d\n" , diff);
    			}
    		}
    	}
    (Sorry if it's badly formatted..)

    When typing 'javac AddSub.java' in Terminal, I get the following errors.


    Java Code:
    AddSub.java:7: error: cannot find symbol
    if (input.nextInt=1) {
             ^
      symbol:   variable nextInt
      location: variable input of type Scanner
    AddSub.java:13: error: cannot find symbol
    if (input.nextInt=2) {
             ^
      symbol:   variable nextInt
      location: variable input of type Scanner
    2 errors
    Any help would be greatly appreciated! Thank you!

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Need help with errors in Add/Subtract Program!

    Quote Originally Posted by brookeblood1 View Post
    Java Code:
    AddSub.java:7: error: cannot find symbol
    if (input.nextInt=1) {
             ^
      symbol:   variable nextInt
      location: variable input of type Scanner
    AddSub.java:13: error: cannot find symbol
    if (input.nextInt=2) {
             ^
      symbol:   variable nextInt
      location: variable input of type Scanner
    2 errors
    These errors are telling you that there is not a member of class Scanner named "nextInt". Judging from the rest of your code, I think you know how to fix this problem.

    Another problem is that you attempt to compare two integers using "=", but "=" is the assignment operator. You want to use "==" to test if two primitive data types, such as integers, are equal to each other.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    brookeblood1 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: Need help with errors in Add/Subtract Program!

    Thank you for the reply, but I'm still lost as how to fix the issue. On the second issue you mentioned, I'm assuming it would be fixed by this change in code
    Java Code:
    (input.nextInt==1)
    (= changed to ==)

    On the other part of the problem, I have no idea how to fix the errors since I used 'nextInt' in other parts of the code.
    eg:
    Java Code:
    System.out.println ("Enter first number."); num1 = input.nextInt();
    Is the use of the If, then statement correct?

  4. #4
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Need help with errors in Add/Subtract Program!

    Yes, the use of the if-then statement is correct. Edit: However, the user will be prompted for a 1 or 2 a total of 2 times when it should be 1.

    Is nextInt() a variable or a method?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  5. #5
    brookeblood1 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: Need help with errors in Add/Subtract Program!

    I'm pretty sure nextInt() is a method. To fix that from happening twice would I need to keep the first if code, and then change the second to else?

  6. #6
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Need help with errors in Add/Subtract Program!

    Quote Originally Posted by brookeblood1 View Post
    I'm pretty sure nextInt() is a method.
    In that case, typing
    Java Code:
    input.nextInt
    ...will not call the method because you are missing the parenthesis.

    And yes, an if-else statement can solve your problem.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  7. #7
    brookeblood1 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: Need help with errors in Add/Subtract Program!

    Aha! Easy fix! Thank you very much

    Here's the fixed code
    Java Code:
    import java.util.Scanner ;
    public class AddSub {
    public static void main (String[] args) {
    Scanner input = new Scanner (System.in);
    int num1, num2, sum, diff ;
    System.out.println ("Type 1 for addition, 2 for subtraction.") ;
    if (input.nextInt()==1) {
    System.out.println ("Enter first number."); num1 = input.nextInt();
    System.out.println ("Enter second number."); num2 = input.nextInt();
    sum = num1 + num2 ;
    System.out.printf ("Sum = %d\n" , sum);
    	}
    else {
    System.out.println ("Enter first number."); num1 = input.nextInt();
    System.out.println ("Enter second number."); num2 = input.nextInt();
    diff = num1 - num2 ;
    System.out.printf ("Difference = %d\n" , diff);
    			}
    		}
    	}
    awinston likes this.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: Need help with errors in Add/Subtract Program!

    Notice how you have duplicated the code to enter the numbers in both branches of the if statement. Why not have the code to input the numbers once before the if statement and only use the if statement to do the calculation?

Similar Threads

  1. Help with some errors in my program
    By kbud123 in forum Java Applets
    Replies: 5
    Last Post: 05-11-2011, 05:08 AM
  2. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  3. Simple string add or subtract using scanner
    By weezer562 in forum New To Java
    Replies: 12
    Last Post: 10-21-2010, 08:23 PM
  4. Area Subtract
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:07 PM
  5. Replies: 0
    Last Post: 06-22-2008, 11:06 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
  •