-1

I am completely new to Java, this is the first program I have ever attempted in the language. I've googled around a bit, but can't seem to find an understandable explanation of how to use scanner to assign user input to a variable. I'm not entirely sure what scanner is, which is probably part of the problem.

I've read the documentation here (http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html). It talks a lot about reg ex, which I do not need and makes me think I may be looking in the wrong direction. Any help would be much appreciated, thanks!

import java.util.*;

public final class Chapter_02 {
    public static void main(String[] args) {
        //Declare variables to be used (Height, Width, and Area)
        double length;
        double width;
        double area;

        //User input of length and width
        System.out.print("Please input the length of the property in feet:");
        width = ;
        System.out.print("Please input the length of the property in feet:");
        length = ;

        //Calculate area based on user input
        //Divide by number of feet in one acre
        area = (length * width) / 43560;

        //Additional spacing for readability
        System.out.println();
        System.out.println();

        //Post calculation result
        System.out.println("The property is " + area + " acres!");
    }
}    
Jordan Frankfurt
  • 304
  • 1
  • 4
  • 14

2 Answers2

4

Try this sexy code on for size:

Scanner scan = new Scanner(System.in);

//User input of length and width
System.out.print("Please input the width of the property in feet:");
width = scan.nextDouble();
System.out.print("Please input the length of the property in feet:");
length = scan.nextDouble();
DJClayworth
  • 24,627
  • 8
  • 50
  • 71
Greg
  • 1,595
  • 2
  • 9
  • 20
0

yes you can use the scanner or use console:

Console console = System.console();
String inputWidth = console.readLine("Please input the length of the property in feet:");

and then convert the string to double:

double width = Double.parseDouble(inputWidth);
mmohab
  • 2,073
  • 4
  • 23
  • 40