Results 1 to 8 of 8
- 08-12-2012, 04:52 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 4
- Rep Power
- 0
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.
.gif)
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.
(Sorry if it's badly formatted..)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); } } }
When typing 'javac AddSub.java' in Terminal, I get the following errors.
Any help would be greatly appreciated! Thank you!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
- 08-12-2012, 05:13 AM #2
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Need help with errors in Add/Subtract Program!
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
- 08-12-2012, 05:22 AM #3
Member
- Join Date
- Aug 2012
- Posts
- 4
- Rep Power
- 0
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
(= changed to ==)Java Code:(input.nextInt==1)
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:Is the use of the If, then statement correct?Java Code:System.out.println ("Enter first number."); num1 = input.nextInt();
- 08-12-2012, 05:25 AM #4
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
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
- 08-12-2012, 05:35 AM #5
Member
- Join Date
- Aug 2012
- Posts
- 4
- Rep Power
- 0
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?
- 08-12-2012, 05:41 AM #6
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Need help with errors in Add/Subtract Program!
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 08-12-2012, 05:47 AM #7
Member
- Join Date
- Aug 2012
- Posts
- 4
- Rep Power
- 0
Re: Need help with errors in Add/Subtract Program!
Aha! Easy fix! Thank you very much
Here's the fixed codeJava 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); } } }
- 08-13-2012, 05:05 AM #8
Similar Threads
-
Help with some errors in my program
By kbud123 in forum Java AppletsReplies: 5Last Post: 05-11-2011, 05:08 AM -
First Java Program-Compile Errors (errors are posted)-simple GUI
By cc11rocks in forum AWT / SwingReplies: 4Last Post: 01-04-2011, 12:36 AM -
Simple string add or subtract using scanner
By weezer562 in forum New To JavaReplies: 12Last Post: 10-21-2010, 08:23 PM -
Area Subtract
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:07 PM -
Area Calculation: Add, Subtract, Intersect, Exclusive Or
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:06 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks