Java - sc.nextDouble() - make another line - java

Sorry for my English but it is not my native language. I have to create program to calculate some math equations. And I have find little annoying problem. I need to get my input number in one line, but when i use
sc = new Scanner(System.in);
sc.useLocale(Locale.US);
System.out.print("a=");
double a = sc.nextDouble();
System.out.print(", b=");
double b = sc.nextDouble();
I get something like this:
a=Some Number
, b=Some Number
But I need this(to be an the same line):
a=Some Number, b=Some Number
I have tried to find the answer but after 4 hours I can't find it or I don't understand it. Thanks for help.

You would do so:
double a = sc.nextDouble();
double b = sc.nextDouble();
System.out.print("a=" + a + ",b=" + b);

double a = sc.nextDouble();
double a = sc.nextDouble();
System.out.print("a ="+ a + ","+ "b ="+b );

Scanner sc = new Scanner(System.in);
sc.useLocale(Locale.US);
double a = sc.nextDouble();
double b = sc.nextDouble();
System.out.println("a="+a+"b="+b);

Related

Trying To Make A Table, Strange Tab Spacing

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);
}

Have trouble getting an input using Scanner Class

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();

Java - Issues parsing a string for a simple calculator program

I have code that takes input and then figures out what you are wanting to do with it
eg. You would type in "x (+,-,..etc) y" and it would calculate it for you.
Im currently using a Scanner and splitting it up such that
double x = input.nextDouble();
String z = input.next();
double y = input.nextDouble();
Now I have run into a problem. Say I want to do a factorial I would then input "x !" but the code is still wanting the last input.nextDouble();
How would I go about (using what I am doing, if possible) checking to see if all 3 have inputs and then selecting between the methods using an if statement or if only 2 have inputs.
Relative code
System.out.print("> ");
double x = input.nextInt();
String z = input.next();
double y = input.nextInt();
if (x == 0) {
running = false;
} else if (z.equalsIgnoreCase("+")) {
System.out.println(addition(x, y));
}
Instead of getting three different inputs, just input a single line of string, parse the string accordingly and type cast them to the necessary types. This way you can determine from the string is a factorial (1 variable) or any other operation on it is necessary.
Using scanner.
boolean binary = true;
Scanner input = new Scanner(System.in);
double x = input.nextInt();
String z = input.next();
//check if z is a unary operator ie.
if(z=='!')
binary = true;
if(binary)
double y = input.nextInt();
If you have two inputs, then your code will throw NoSuchElementException. To avoid that you should use input.hasNext().
double x = input.nextInt();
String z = input.next();
if (input.hasNext()) {
// input has y
y = input.nextInt();
// perform operation on two elements
} else {
// no y
// perform operation on one element
}
Add an extra if statement and ask for 'y' only if z = '+'.
System.out.print("> ");
double x = input.nextInt();
String z = input.next();
if (x == 0) {
running = false;
} else if(z.equalsIgnoreCase("!")){
factorial(x);
}
else if (z.equalsIgnoreCase("+")) {
double y = input.nextInt();
System.out.println(addition(x, y));
}
try this code
double y = 0.0;
if(!z.contains("!")){
y = sc.nextDouble();
}
you must get the input as a string so you must use String.indexof() and divide the main string in two string you must parse the first part like this:
int a=Integer.parse(someString);
and the second part of your string would show you what you must to do with that.

How to put Input on next line in Java's Scanner utility

I have a basic java question about the scanner utility. Let's say i have a simple program that takes the user input and stores it in a variable. My question is when i run the program that asks for multiple inputs, the cursor starts at the beginning of the question and not after it.
My code is:
public class question3 {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println("Enter the first number:");
Float a = s.nextFloat();
System.out.println("Enter the second number:");
Float b = s.nextFloat();
System.out.println("Sum = " + (a+b));
System.out.println("Difference = " + (a-b));
System.out.println("Product = " + (a*b));
}
}
When I run this program it will look like Enter First Number then i type the number, and then |Enter Second Number. "|" meaning where the blinking cursor is. When I type it'll show up underneath, but it could confuse the user so I was wondering what the solution could be.
It is an IDE problem, since nothing else is wrong with the code.
Instead of println(String) before each input, change it to print(String). So it would look something like this:
public class question3{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.print("Enter the first number:");
Float a = s.nextFloat();
System.out.print("Enter the second number:");
Float b = s.nextFloat();
System.out.println("Sum = " + (a+b));
System.out.println("Difference = " + (a-b));
System.out.println("Product = " + (a*b));
}
}
Also, just a note, you should use proper/appropriate naming conventions for your variables. Like for your Scanner, you should call it reader or input; something which represents its function. The same idea goes for the rest of your variables. Also, class names start with a capital.
Here is what the finished result looks like:
System.out.println prints out string then a new line, so your input is being placed on a new line. Try making it read
System.out.print("Enter the first number:");
Float a = s.nextFloat();
System.out.println();
System.out.print("Enter the second number:");
Float b = s.nextFloat();
System.out.println();
This can save you some seconds, by typing few lines:
System.out.print("Enter the first number:");
Float a = s.nextFloat();
System.out.print("\nEnter the second number:");
Float b = s.nextFloat();
System.out.println("\nSum = " + (a+b)
+"\nDifference = " + (a-b)
+"\nProduct = " + (a*b));

UnknownFormatConversionException in a program that prints a percentage

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);

Categories

Resources