1

I'm working on an assignment for my Java class and I keep getting compiler errors.

The errors I'm getting are "not a statement subtotal ++ total;" and "error: ';' expected subtotal ++ total;".

Any advice would be greatly appreciated.

The assignment is to create a program that adds numbers together and prints the subtotal once the user enters a zero, and prints the complete total after two consecutive zeros.

I am using this website for the programming and compiling: http://www.compileonline.com/compile_java_online.php

Thanks in advance.

public class Homework4{

 public static void main(String []args){

    int n;      
    int previous = -99999;      
    int total = 0;      
    int subtotal = 0;         

    System.out.println("This program will add numbers you input.");
    System.out.println("Once you input a number, press enter.");
    System.out.println("When you want the subtotal of your numbers, input 0.");
    System.out.println("When you want the complete total, input 0 once more.");

    n = scanner.nextInt ( );      
    while (true) {          
        if (n == 0 && previous == 0) {              
            System.out.println("Total: " + total);          
            } else if (n == 0) {             
                subtotal ++ total;         
                System.out.println("Subtotal: " +subtotal);
                subtotal == 0;              
                previous == 0;          
            } else {     
                n ++ subtotal;
                previous == n;              
                      }          
                n = scanner.nextInt ( );     
                }  


     }
}

2 Answers 2

2

Unary addition is not ++. It is +=

subtotal += total; 

is equivalent to

subtotal=subtotal+total; 

and is a convenient shorthand.

To add 1 to a variable, use:

varToIncrement++;

Note there is nothing on the other side of the operator.

On that note I recommend that you install an IDE such as Eclipse, and the JDK, as a site like writecodeonline is less powerful and will not let you try your Java code to its full potential.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help, it runs great now. I'd like to use Eclipse but the only computer I have available to me right now is a chromebook.
@HectorBrass If this answer helped please click the grey checkmark to the left of it to mark it as accepted.
0

You should use += instead of ++. ++ is to increment a counter not to add.
Query: what if user wants to add ten(10)? will the check on 0 work?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.