I'm going through my Java text book and for some reason I cannot compile the following code.
import java.util.*;
public class ComputeAreaWConsoleInput
{
public static void main (String [] args)
{
//Create Scanner Obj
Scanner sc = New Scanner(System.in);
//Get Radius
System.out.print("Please Enter the Radius: ");
double radius = sc.nextdouble();
//determine area
double area = 3.14159 * radius * radius;
//display results
System.out.println("The Area of the Circle w/ radius(" + radius +") is: "
+ area);
}
}
I am getting the following error:
/tmp/java_H98cOI/ComputeAreaWConsoleInput.java:8: error: ';' expected
Scanner sc = New Scanner(System.in);
^
1 error
What shall be done to compile the code?
Two changes to your program.
Change New to new.Change the line
Scanner sc = New Scanner(System.in);
to
Scanner sc = new Scanner(System.in);
and other error in the program is scanning a double. Please change double to Double.So change the below line
double radius = sc.nextdouble();
to
double radius = sc.nextDouble();
It should work fine!
See my comment: Here is the fixed version of your code:
public static void main (String []
{
//Create Scanner Obj
Scanner sc = new Scanner(System.in);
//Get Radius
System.out.print("Please Enter the Radius: ");
double radius = sc.nextDouble();
//determine area
double area = 3.14159 * radius * radius;
//display results
System.out.println("The Area of the Circle w/ radius(" + radius +") is: " + area);
sc.close(); // DO NOT forget this
}
You've written:
New Scanner(System.in);
You N in New is capital.
The actual keyword is new and not New.
Solution:
Change your line of code to:
new Scanner(System.in);
And there is another error.
It should be:
sc.nextDouble(); // with 'D' capital
and not
sc.nextdouble();
Related
I need to use scannner to take input from user for data type double separated by white space.
Scanner input = new Scanner(System.in);
System.out.print("exampleDouble: ");
double db = input.nextDouble();
db+=input.nextLine();
I thought this would work, looking for a simple statement for capturing 2 values (both double, separated by space). no arrays. only need to capture 2 values , not more.
Example : 58.0 57.3
WITHOUT USING ARRAYS
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
double double1 = input.nextDouble();
double double2 = input.nextDouble();
System.out.println("Double1 is "+double1);
System.out.println("Double2 is "+double2);
}
}
WITH ARRAYS
You can split it into a String array, and then parse them using Double.parseDouble like so:
Scanner input = new Scanner();
String[] userinput = input.nextLine().split(" ");
double double1 = Double.parseDouble(userinput[0]);
double double2 = Double.parseDouble(userinput[1]);
Therefore, the full code would be:
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
String[] userinput = input.nextLine().split(" ");
double double1 = Double.parseDouble(userinput[0]);
double double2 = Double.parseDouble(userinput[1]);
System.out.println("Double1 is "+double1);
System.out.println("Double2 is "+double2);
}
}
Sample I/O
Input
2.5 3.5
Output
Double1 is 2.5
Double2 is 3.5
HOWEVER: As #Pschemo so aptly pointed out - you should just be using input.nextDouble() twice.
I am attempting to write a program that creates a table based on parameters given by the user that describe a graph. I am merely a beginner in Java and am just starting out, so if I am using inefficient methods then that might be the reason for that. The problem that I am encountering while writing the code is that there seems to be a strange occurrence when I run the program that causes one row to have a different spacing between columns than other rows. This is extremely infuriating and I would love to have a solution to this. Again, I am an extreme beginner and am still wrapping my mind around writing mid sized programs. T
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
Scanner input3 = new Scanner(System.in);
Scanner input4 = new Scanner(System.in);
Scanner input5 = new Scanner(System.in);
double xjumps;
double yjumps;
double start;
double length;
double intercept;
System.out.printf("Please input the length of your table: ");
length = input.nextDouble();
System.out.printf("\n");
System.out.printf("Please input the x-axis incrementation: ");
xjumps = input2.nextDouble();
System.out.printf("\n");
System.out.printf("Please input the y-axis incrementation: ");
yjumps = input3.nextDouble();
System.out.printf("\n");
System.out.printf("When do you want the table to start: ");
start = input4.nextDouble();
System.out.printf("\n");
System.out.printf("Please input the y intercept: ");
intercept = input5.nextDouble();
double x = start;
double y = (((start/xjumps) * yjumps) + intercept);
System.out.printf("\n");
System.out.println("X\t\t\tY");
System.out.println(x + "\t\t" + y);
for(double z = -1;z <= length;z++){
x += xjumps;
y += yjumps;
System.out.println(x + "\t\t" + y);
}
I'm having a lot of trouble having the user input a string into the console and the specific string they typed equal an if statement. I want to input "SquareRoot" into the console and it go to the if statement, but when I type it in, nothing happens. What can I do to fix this? How do I make the user input equal to the string and the if statement? Is there something wrong with my "if" statement?
Scanner userInput = new Scanner(System.in);
String SquareRoot;
System.out.println("Type 'SquareRoot' - find the square root of (x)")
SquareRoot = userInput.next();
if(SquareRoot.equals("SquareRoot")) {
Scanner numInput = new Scanner(System.in);
System.out.println("Enter a number - ");
double sR;
sR = numInput.nextDouble();
System.out.println("The square root of " + sr + "is " + Math.sqrt(sR));
Your code was mostly correct:
You had some typos that would prevent your code compiling successfully. You
should consider using an IDE such as Eclipse, as it will highlight these
kinds of issues for you as you type.
You shouldn't create a 2nd Scanner object, reuse the existing one
Be sure to close your Scanner when done
Here's your corrected code:
public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in);
String SquareRoot;
System.out.println("Type 'SquareRoot' - find the square root of (x)");
SquareRoot = userInput.next();
if (SquareRoot.equals("SquareRoot"))
{
// You shouldn't create a new Scanner
// Scanner numInput = new Scanner(System.in);
System.out.println("Enter a number - ");
double sR;
// Reuse the userInput Scanner
sR = userInput.nextDouble();
System.out.println("The square root of " + sR + " is " + Math.sqrt(sR));
}
// Be sure to close your Scanner when done
userInput.close();
}
I have some code with no compile errors, but after I enter the second number while its running it crashes on me :(
Heres what I have:
import java.util.Scanner;
public class Assignment536 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of sides: ");
int numberOfSides = input.nextInt();
System.out.println("Enter the side: ");
double side = input.nextInt();
System.out.println("The area of the polygon is: " +area(numberOfSides, side));
input.close();
}
public static double area(int n, double side) {
double answer = (n*(side*side))*(4*(Math.tan((Math.PI*n))));
return answer;
}
}
Any help would be greatly appreciated!
Thank you,
Sebastian
Add input.nextLine() between the numberOfSlices and side request...
System.out.println("Enter the number of sides: ");
int numberOfSides = input.nextInt();
input.nextLine();
System.out.println("Enter the side: ");
double side = input.nextInt();
After requesting numberOfSlices there is still a carriage return/line feed in the input buffer, when you try and request the side value Scanner fails because it can't convert the the carriage return/line feed to a double type.
You should change:
final double side = input.nextInt();
for
final double side = input.nextDouble();
if you want to read a double.
The last part of your formula is wrong. It should be (Math.pi/n) .
import java.util.Scanner;
public class CalcAreaPolygon {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
double area;
final double PI; /method in the formula
double side;
int n;
PI = Math.PI;
System.out.println("Enter the number of sides: ");
n = scanner.nextInt();
scanner.nextLine();
System.out.println("Enter the side: ");
side = scanner.nextDouble();
area = (n * (side * side)) / (4 * (Math.tan(PI/n)));
System.out.println("The area of the polygon is " + area);
}
}
I have to write a program where I put in baseball stats and it comes out with slugging % batting avg and re says the name of the player then put it on a sentinel loop. My current code is below. I'm trying to get it to work with just one before I turn it into a loop. When I run to test, I get UnknownFormatConversionException. What does it mean? How can I fix my code?
import java.util.Scanner;
public class bata
{
public static void main(String[] args) throws Exception
{
double ba,sp,tb;
int tri,dou,sin,hr,ab,hits;
String name;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Singles");
sin = sc.nextInt();
System.out.print("Enter Doubles");
dou = sc.nextInt();
System.out.print("Enter Triples");
tri = sc.nextInt();
System.out.print("Enter Homeruns");
hr = sc.nextInt();
System.out.print("Enter At bats");
ab = sc.nextInt();
System.out.print("Enter Player name");
name = sc.nextLine();
System.in.read();
tb= (sin + (dou*2) + (tri *3) +(hr *4));
hits = sin+dou+tri+hr;
sp= tb/ab;
ba= hits/ab;
System.out.println(""+name);
System.out.printf("Slugging % is %.3f\n", sp);
System.out.printf("Batting avg os %.3f\n", ba);
}
}
Escape % sign,by using double %%:
System.out.printf("Slugging %% is %.3f\n", sp);
// ^------------- escaping
UnknownFormatConversionException happens when you are expecting an integer and read a string from your scanner. It would be helpful if you could post your input file.
Also, escape the % sign using another %.
System.out.printf("Slugging %% is %.3f\n", sp);