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 + ").");
Related
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).
It may be rather simple but I am having issues returning a value for "circum" here is my code.
double circRad = 0;
double circum = 0;
if(userOption == 2){
circRad = myRad();
System.out.println("The Radius of the circle is " + circRad + " and it perimeter is "+ circum);
public static double myRad(){
int i= 1;
double circRad = 0;
Scanner myInput=new Scanner(System.in);
switch(i)
{
case 1:
System.out.print("Please enter a positive Radius for the circle ");
circRad = myInput.nextDouble();
while(circRad <0){
System.out.print("Please enter a POSITIVE Radius for the circle ");
circRad = myInput.nextDouble();
}
++i;
}
return circRad;
}
public static double myRad(double circRad){
double circum = 0;
circum = 2* Math.PI * circRad;
return circum;
}
Perhaps you are missing the following:
if(userOption == 2){
circRad = myRad();
circum = myRad(circRad); // <-- missing setting local circum value
System.out.println("The Radius of the circle is " + circRad + " and it perimeter is "+ circum);
}
Only thing I could guess about the code with the way it is presented is that circRad and circum are global variables, therefore when you call the first myRad() only circRad changes. However, circum remains 0.
If you could sent the entire code, it'll make everything easier.
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))
int single = 0, doub=0, triple=0, homer=0, atbats=0, totalbase, totalhits;
double slug, battingavg;
Scanner sc = new Scanner(System.in);
System.out.print("Enter singles (-1 to end): ");
single = sc.nextInt();
while (single != -1)
{
System.out.print("Enter doubles: ");
doub = sc.nextInt();
System.out.print("Enter triples: ");
triple = sc.nextInt();
System.out.print("Enter home runs: ");
homer = sc.nextInt();
System.out.print("Enter total at bats: ");
atbats = sc.nextInt();
System.out.print("Enter the player's name: ");
String name = sc.next();
totalbase = (single + doub * 2 + triple * 3 + homer * 4);
slug = totalbase / atbats;
battingavg = (single + doub + triple + homer) / atbats;
System.out.println("Player's name is " + name);
System.out.printf("The slugging percentage is %.3f\n", + slug);
System.out.printf("The batting percentage is %.3f\n", + battingavg);
System.out.print("Enter singles (-1 to end): ");
single = sc.nextInt();
}
This program will only output a 1 or a 0 after the calculations. Everything else works fine, but It just doesn't seem to do the calculations.
The problem is that when you do integer divisions, you'll get integer values. Instead use double or float data types. I see slug and battingavg are already doubles, but you're assigning the result of an integer division to them. If you cast at least one of the values in your calculations to a double you should get the output you expect. Example:
slug = totalbase / (double) atbats;
battingavg = (single + doub + triple + homer) / (double) atbats;
When you divide integer values and store the result in a double that is a widening conversion, but the value was calculate as an integer and thus you're widening the integer value.
Change this,
slug = totalbase / atbats;
battingavg = (single + doub + triple + homer) / atbats;
to something like this,
slug = ((double) totalbase / atbats);
battingavg = ((double) (single + doub + triple + homer) / atbats);
to get double values into your double variables.
I am a beginner at java and for my class we are learning about methods. For my first program I have to write a method and provide a program for: double average(double x, double y, double z) returning the average of the arguments. Here is my program so far
//**METHOD**//
Scanner in = new Scanner(System.in);
System.out.println("Enter variable x");
double x = in.nextDouble();
System.out.println("Enter variable y");
double y = in.nextDouble();
System.out.println("Enter variable z");
double z = in.nextDouble();
double average;
System.out.println("The average is: " + average);
//TEST PROGRAM//
public static double average(double x, double y, double z)
{
double average = (x * y * z)/3;
return average;
}
A method is only useful if you use it. You will want to reference (or call) the method where you want to use it.
In your code:
System.out.println("The average is: " + average(x, y, z) );
And to clarify, the average is the sum of all values divided by the number of values.
In this case it would be (x + y + z) / 3 not (x * y * z) /3
double average = (x * y * z)/3;
// ^ ^ what?
Here's your first problem. Why are you multiplying?
double average;
System.out.println("The average is: " + average);
Your second problem is that you didn't actually call the method. If you just define a method and don't call it, Java doesn't know when you want it called or what arguments you want to use. Defining a variable with the same name doesn't implicitly call the method or anything like that. The method won't run at all. Call it with the arguments you want to get a value:
System.out.println("The average is: " + average(x, y, z));
You also need your code to exist within a class and it will need a public static void main(final String[] args) {} method in order to run.
Call the method like this:
double average = average(x, y, z);
System.out.println("The average is: " + average);
And for the record, the average is the sum of the terms divided by the number of terms, not the product. So change your average method to this:
public static double average(double x, double y, double z)
{
double average = (x + y + z) / 3;
return average;
}