tags:

views:

64

answers:

4

This is my first program in java and I haven't found any good websites like this one for C++ and it's confusing for me because I just started writing java and I just came from C++. Anyways, concerning this code, could someone explain how to fix this code because of the line containing Scanner and/or how to simply receive inputs, because I haven't found any simple way to translate cin >> from C++

public class input {
    public static void main(String[] args) {
        double total = 0;          
        Scanner in = new Scanner(System.in);
        System.out.println("As you enter numbers, they will be added.");
        System.out.println("Entering a non-number will stop the program.");

        while (in.hasNextDouble()) {
            double n = in.nextDouble();
            total = total + n;
            System.out.println("The total is " + total);
        }
    }
}
A: 

I'm at work and don't have the jdk installed, so I can't compile and run this. Giving a quick look though, it seems like the only thing you might have problem breaking out of the scanner, though. After entering a few numbers, try pressing ctrl-d - this should signal end of input.

Dave McClelland
+2  A: 

Your code works. Just make sure you have import java.util.Scanner. On a related note, use Eclipse or Netbeans as they would have told you this. Also, you should capitalize class names and put your class in a package instead of in the "default package". I recommend "Head First Java".

package sand1;

import java.util.Scanner;

public class Input {
    public static void main(String[] args) {
        double total = 0;
        Scanner in = new Scanner(System.in);
        System.out.println("As you enter numbers, they will be added.");
        System.out.println("Entering a non-number will stop the program.");

        while (in.hasNextDouble()) {
            double n = in.nextDouble();
            total = total + n;
            System.out.println("The total is " + total);
        }
    }
}

Here is output when I ran it. I think I might consider it a bug that I was able to hit enter with a blank line without it ending.

run:
As you enter numbers, they will be added.
Entering a non-number will stop the program.
12.2
The total is 12.2
43
The total is 55.2

a
BUILD SUCCESSFUL (total time: 11 seconds)
Dave
I compiled the code in cmd `javac Input.java` and then I ran it (java Input) but it came up with this errorException in thread "main" java.lang.NoClassDefFoundError: Input (wrong name: sand1/Input)
danutenshu
@dan If you put the class in a package you need to include that when you run it: `java sand1.Input`
Michael Mrozek
@Michael I ran the code without any sand1 even though I placed it in a Java project (in Eclipse) and it worked just fine in cmd
danutenshu
@dan I'm not sure what you're saying; it "worked just fine in cmd" but you got an exception when you ran it
Michael Mrozek
@Michael I'll ask it when i need to, but in the time being, it worked
danutenshu
A: 

As Borealid said you need to add the following line at the top of the class to get it to compile:

import java.util.Scanner;

Also note that by convention in java classes are named with an uppercase character Input, not input.

Finally, you can obtain input directly through System.in.read() and the other overloaded permutations of the read() method, against System.in

crowne
A: 

Check out the Java Tutorials,they're quite good for a beginner

Phobia