So I have this input file that I am trying to read with scanner.The file looks like this.
20 0.4
3 5.6
As you see it has an int and then a double and this goes on until the end of the file.I tried reading both of them as doubles:
while(scanner.hasNextDouble()){
x = (int) scanner.nextDouble();
y = scanner.nextDouble();
}
And I also tried this:
while(scanner.hasNext()){
x = scanner.nextInt();
y = scanner.nextDouble();
}
But none of them seems to work.What may be a solution to this?
Your second loop works fine.
PROOF
String input = "20 0.4\n" +
"3 5.6\n";
Scanner scanner = new Scanner(input);
while (scanner.hasNext()) {
int x = scanner.nextInt();
double y = scanner.nextDouble();
System.out.printf("x=%d, y=%f%n", x, y);
}
OUTPUT
x=20, y=0.400000
x=3, y=5.600000
This is what is called a Minimal, Complete, and Verifiable example (MCVE).
Related
I can't figure out what is wrong on line 34 and I need help fixing it. I don't know if I incorrectly input something or if I need to convert something.
import java.util.Scanner;
import java.lang.Math;
public class PaintEstimator {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double wallHeight;
double wallWidth;
double wallArea;
System.out.println("Enter wall height (feet): ");
wallHeight = scnr.nextDouble();
System.out.println(wallHeight);
wallWidth = scnr.nextDouble();
System.out.println("Enter wall width (feet): ");
System.out.println(wallWidth);
wallArea = wallHeight * wallWidth;
System.out.println("Wall area: " + (wallArea) + " square feet");
double paintNeeded;
int cansNeeded;
final double squareFeetPerGallons;
final double gallonsPerCan;
squareFeetPerGallons = 350.0;
gallonsPerCan = 1.0;
System.out.print("Paint needed: ");
paintNeeded = wallArea / squareFeetPerGallons;
System.out.print(paintNeeded);
System.out.println(" gallons");
cansNeeded = paintNeeded / gallonsPerCan;
System.out.print("Cans needed: ");
System.out.print(cansNeeded);
System.out.println(" can(s)");
}
}
Integers don't have decimal points, unlike doubles, which do.
You can only assign an integer to an int, not a double etc,.
In your case, you probably want to define cansNeeded as a double (every single other variable is a double). It should look like this:
double cansNeeded = paintNeeded / gallonsPerCan
Then you can deal with the result accordingly (round up, round down etc,.). This is more precise.
To round up, use Math.ceil(cansNeeded). To round down, use Math.floor(cansNeeded).
The result of dividing doubles is a double, which can't be assigned to an int. In your case, you probably want to use Math.ceil and then cast to int.
cansNeeded = (int) Math.ceil(paintNeeded / gallonsPerCan);
This question already has answers here:
Is there an equivalent method to C's scanf in Java?
(7 answers)
How to read multiple Integer values from a single line of input in Java?
(21 answers)
Closed 2 years ago.
How do you scan 3 variable in one line
its like i have 3 int variable named ( x , y and z)
I want to input the three of them in a single line
i can input like this
7 21 35 < single line
int x = 7;
int y = 21;
int z = 35;
x = sc.nextInt();
y = sc.nextInt();
z = sc.nextInt();
System.out.printf("%2d, %2d, %2d\n", x, y, z);
I have found something in C++ their code is like this ( scanf ("%lf %lf %lf", &x, &y, &z); )
Here is small example for you
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String[] input = new String[3];
int x;
int y;
int z;
System.out.print("Please enter Three integers: ");
input = in.readLine().split(" ");
x = Integer.parseInt(input[0]);
y = Integer.parseInt(input[1]);
z = Integer.parseInt(input[2]);
System.out.println("You input: " + x + ", " + y + " and " + z);
}
Desired:
The first coordinate is (input1, input2).
What I Get:
The first coordinate is (input1
, input2
).
The code I used:
Scanner Narwhal = new Scanner(System.in);
System.out.print("The first coordinate value is (");
double x = Narwhal.nextDouble();
System.out.print(", ");
double y = Narwhal.nextDouble();
System.out.print(").");
Thanks!
Print it all out at once using System.out.printf. This assumes you've moved the nextDouble calls before your print statement.
System.out.printf("The first coordinate value is (%0.1f, %0.1f).", x, y);
First you need to get the values from a user's input and after that you should print. You are mixing input and output.
You need something like this:
Scanner Narwhal = new Scanner(System.in);
double x = Narwhal.nextDouble();
double y = Narwhal.nextDouble();
System.out.print("The first coordinate value is (" + x);
System.out.print(", " + y);
System.out.print(").");
You did not print the variables:
System.out.print("The first coordinate value is (");
double x = Narwhal.nextDouble();
System.out.print(x + ", ");
double y = Narwhal.nextDouble();
System.out.print(y + ").");
I'm a beginner programmer and new to stackoverflow.
So i have been making a Math Formula Solver and I have 4 formulas up and functioning. I am working on my Sine formula solver and it won't work. I use a scanner and switch to select the formula and input the variables. Here is what i have.
Scanner input = new Scanner(System.in);
System.out.println("Enter '0' for list of formula call numbers");
System.out.print("Enter the formula request number: " );
int mFormula = input.nextInt();
switch(mFormula)
{
case 5 :
System.out.println("Sine Problem Solver, please enter your variables below: ");
System.out.println();
System.out.print("Value for known side: ");
int x = input.nextInt();
System.out.println();
System.out.print("Value for degrees: ");
int x3 = input.nextInt();
System.out.println();
double Sine = (x * Math.sin(x3));
System.out.print("The side length is: " + Sine);break;
}
im just really confused on why it isn't working.
It seems that your main problem is that Math.sin doesn't take degrees but radians. Try
Math.sin(Math.toRadians(x3))
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How would I return an exact value to a calculation, i. e.: 4 * sqrt(3) as opposed to the decimal form?
EDIT: Sorry, I thought this was a basic question. Here is my code:
package physics;
import java.util.Scanner;
public class Physics {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("do you want to \n [1] find the magnitude from components \n [2] find the components from magnitude and angle \n [3] Dot product from magnitude" +
"\n [4] Gravitational problems" );
int key = in.nextInt();
switch(key){
case 1:
compsToMag();
break;
case 2:
magToComps();
break;
case 3:
dotProductWithMags();
break;
case 4:
gravity();
break;
}
}
public static double compsToMag(){
Scanner input = new Scanner(System.in);
System.out.println("enter x, [Enter], then y comp");
double x = input.nextDouble();
double y = input.nextDouble();
double mag = Math.sqrt((x*x) + (y*y));
System.out.print("Magnitude = " + mag);
return mag;
}
public static void magToComps(){
Scanner input = new Scanner(System.in);
System.out.println("enter magnitude to find components");
int magnitude = input.nextInt();
System.out.println("enter angle in degrees");
double angle = input.nextDouble();
double x = magnitude * (Math.cos(angle));
double y = magnitude * (Math.sin(angle));
System.out.println("X = " + x + " \n Y = " + y);
}
public static double dotProductWithMags(){
Scanner input = new Scanner(System.in);
System.out.println("enter magnitude of vector 1");
int mag1 = input.nextInt();
System.out.println("enter magnitude of vector 2");
int mag2 = input.nextInt();
System.out.println("enter angle");
double angle = input.nextDouble();
double result = mag1 * mag2 * Math.cos(angle);
System.out.print("dot product result = " + result);
return result;
}
public static void gravity(){
Scanner input = new Scanner(System.in);
double G = 6.674 * Math.pow(10, (-11));
System.out.println("Mass 1 'base' [base * 10^(exponent)]:");
double mass1 = input.nextDouble();
System.out.println("Mass 1 'exponent' [base * 10^(exponent)]:");
double mass1Exp = input.nextDouble();
System.out.println("");
System.out.println("Mass 2 'base':");
double mass2 = input.nextDouble();
System.out.println("Mass 2 'exponent':");
double mass2Exp = input.nextDouble();
System.out.println("");
System.out.println("radius/distance apart:");
double rad = input.nextDouble();
double force = (G * (mass1 * Math.pow(10, mass1Exp)) * (mass2 * Math.pow(10, mass2Exp))) / (Math.pow(rad, 2));
double accelerationMass1 = force/mass1;
double accelerationMass2 = force/mass2;
System.out.println("Force of gravity = " + force);
System.out.println("Acceleration of mass1 = " + accelerationMass1);
System.out.println("Acceleration of mass2 = " + accelerationMass2);
}
}
I just want to be able to return an exact value with a square root sign as opposed to a decimal
If you need the exact value. You should consider using a math library that provides symbolic calculations.
Take a look at this question.