space55
space55

Reputation: 65

System.out.println not working with numbers

I have the following code:

    import java.util.Scanner;

    import javax.swing.JOptionPane;


    public class weatherCalc {
        public static void main(String args[]) {
            while (true) {
                int division = 8125/1000;
                Scanner in = new Scanner;
                System.out.println("How far, in inches, is it moving on a 50-mile = 0.75 in? (Please use decimels)");
                int weatherInput = in.nextInt();
                System.out.println("How long is the time period in hours? (Please use decimels)");
                int weatherTime = in.nextInt();
                int weatherOutput = weatherInput/division*50/weatherTime;
                System.out.println("The storm is travling at "+ weatherOutput +"MPH.");
            }
        }
    }

You see, the System in "System.out.println("How far, in inches, is it moving on a 50-mile = 0.75 in? (Please use decimels)");" is underlined with red along with the end of "Scanner in = new Scanner;" I can not figure out why, and am just trying to develop this for myself. I might later work on it a little. If anyone can tell me why, it would help a lot.

Upvotes: 0

Views: 151

Answers (3)

Vishal K
Vishal K

Reputation: 13066

Your while loop should be like this: (watch the comments specified against each changed code)

Scanner in = new Scanner(System.in);//Move the Scanner declaration outside loop . Don't create it every-time within the loop.
while (true) {
  int division = 8125/1000;
  System.out.println("How far, in inches, is it moving on a 50-mile = 0.75 in? (Please use decimels)");
  int weatherInput = in.nextInt();
  System.out.println("How long is the time period in hours? (Please use decimels)");
  double weatherTime = in.nextDouble();//Take double value from input as u have specified that you want input in decimals.
  double weatherOutput = weatherInput/division*50/weatherTime;//Changed weatherOutput type to double so that you get the result in double.
  System.out.println("The storm is travling at "+ weatherOutput +"MPH.");
 }

Upvotes: 1

Eng.Fouad
Eng.Fouad

Reputation: 117587

Scanner in = new Scanner;

should be:

Scanner in = new Scanner(System.in);

Upvotes: 4

martinez314
martinez314

Reputation: 12332

Scanner in = new Scanner(System.in);

Upvotes: 4

Related Questions