I can't input a double number - java

enter code here package com.company;
import java.util.Locale;
import java.util.Scanner;
public class Main {
public static double min (double n1 , double n2 , double n3)
{
double minNumber = n1;
if (minNumber>n2)
minNumber=n2;
if (minNumber>n3)
minNumber=n3;
return minNumber;
}
public static double max (double n1 , double n2 , double n3){
double maxNumber = n1;
if (maxNumber<n2)
maxNumber=n2;
if (maxNumber<n3)
maxNumber=n3;
return maxNumber;}
public static void main(String[] args) {
while (true) {
Scanner input = new Scanner(System.in);
System.out.println("Enter 1 if you want minimum \nEnter 2 if you want maximum \nEnter 3 if you want to stop the program");
byte Enter = input.nextByte();
if (Enter==1) {
Scanner in = new Scanner(System.in);
System.out.println("Enter first number");
double x = in.nextInt();
System.out.println("Enter second number");
double y = in.nextInt();
System.out.println("Enter third number");
double z = in.nextInt();
double min = min(x,y,z);
System.out.println("the minimum number is :" + min);
}
else if (Enter == 2) {
Scanner in = new Scanner(System.in);
System.out.println("Enter first number");
double x = in.nextInt();
System.out.println("Enter second number");
double y = in.nextInt();
System.out.println("Enter third number");
double z = in.nextInt();
System.out.println("the maximum number is :" + max(x, y, z));
}
else if (Enter==3){
break;
}
else System.out.println("Enter again");
}
}
}
/* output is :
Enter 1 if you want minimum
Enter 2 if you want maximum
Enter 3 if you want to stop the program
1
Enter first number
12.3
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at com.company.Main.main(Main.java:39)
Process finished with exit code 1

You need write in.nextDouble() instead in.nextInt()

Related

how do you deal with Exception in thread "main" java.util.NoSuchElementException?

i still new at coding, and i am wonder if anyone could help me with java code.
additionally, explaining how to deal with this issue.
here is my code below:
import java.util.Scanner;
public class SumAverage {
public static void main(String[] args) {
// create a Scanner to obtain input from the command window
Scanner input = new Scanner(System.in);
int number1, number2, sum;
System.out.print("Enter first integer: "); // prompt
number1 = input.nextInt(); // read 1st number from user
System.out.print("Enter second integer: "); // prompt
number2 = input.nextInt(); // read 2nd number from user
sum = number1 + number2;
double average = (double) sum / 2;
String message = "Sum is";
System.out.printf("%s %d\n", message, sum);
System.out.printf("Average is %.2f\n", average);
} // end main method
} // end class SumAverage

static reference error with class methods java [duplicate]

This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 5 years ago.
New to java. Do not understand error. Basically trying to return value to then determine output but error "Cannot make static reference to non static field appears on line 13" in the class bosscalc. Return values from operators class.Please help. I have indicated line 13 in the class bosscalc. Thanks
package calculator;
import java.util.Scanner;
public class bosscalc {
Scanner input = new Scanner(System.in);
public static void main(String args[]) {
operators operatorobjects=new operators();
String answer;
System.out.println("What would you like to do? ");
answer =input.nextLine(); -------------------------LINE 13
if (answer=="a"){
double adding = operatorobjects.add();
}
if (answer=="s") {
double subtrat = operatorobjects.sub();
}
if (answer=="m") {
double multiply = operatorobjects.sub();
}
}
}
Class operators:
package calculator;
import java.util.Scanner;
public class operators {
double add() {
double n1,n2,a;
Scanner input=new Scanner(System.in);
System.out.print("Enter number 1 ");
n1=input.nextDouble();
System.out.print("Enter number 2 ");
n2=input.nextDouble();;
a=n1+ n2;
return a;
}
double sub() {
double n1,n2,d;
Scanner input=new Scanner(System.in);
System.out.print("Enter number 1 ");
n1=input.nextDouble();
System.out.print("Enter number 2 ");
n2=input.nextDouble();;
d=n1 - n2;
return d;
}
double m() {
double n1,n2,m;
Scanner input=new Scanner(System.in);
System.out.print("Enter number 1 ");
n1=input.nextDouble();
System.out.print("Enter number 2 ");
n2=input.nextDouble();;
m=n1/n2;
return m;
}
}
As the error message says: From a static context (your static main function) you cannot reference a non-static variable (input).
You can fix it by making input static, i. e. declare it as follows:
static Scanner input = new Scanner(System.in);
I have spent five minutes changing (refactoring) your code. There were a few simple errors. I have moved everything into a single class, and added some comments.
There are lots of improvements which can be made. But this is all down to practice and experience:
import java.util.Scanner;
public class Operators {
/**
* add numbers
* #return n1 + n2
*/
double add() {
double n1, n2, a;
Scanner input = new Scanner(System.in);
System.out.print("Enter number 1 ");
n1 = input.nextDouble();
System.out.print("Enter number 2 ");
n2 = input.nextDouble();
a = n1 + n2;
return a;
}
/**
* subtract numbers
* #return n1 - n2
*/
double sub() {
double n1, n2, d;
Scanner input = new Scanner(System.in);
System.out.print("Enter number 1 ");
n1 = input.nextDouble();
System.out.print("Enter number 2 ");
n2 = input.nextDouble();
d = n1 - n2;
return d;
}
/**
* multiply numbers
* #return n1 * n2
*/
double multiply() {
double n1, n2, m;
Scanner input = new Scanner(System.in);
System.out.print("Enter number 1 ");
n1 = input.nextDouble();
System.out.print("Enter number 2 ");
n2 = input.nextDouble();
m = n1 * n2;
return m;
}
/**
* divide numbers
* #return n1 / n2
*/
double divide() {
double n1, n2, m;
Scanner input = new Scanner(System.in);
System.out.print("Enter number 1 ");
n1 = input.nextDouble();
System.out.print("Enter number 2 ");
n2 = input.nextDouble();
m = n1 / n2;
return m;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Operators operatorobjects = new Operators();
String answer;
System.out.println("What would you like to do? ");
answer = input.nextLine();
/**
* String equality use String.equals()
*/
if (answer.equals("a")) {
double adding = operatorobjects.add();
/**
* Debug output println
*/
System.out.println("adding = " + adding);
} else if (answer.equals("s")) {
double subtract = operatorobjects.sub();
System.out.println("subtract = " + subtract);
} else if (answer.equals("m")) {
double multiply = operatorobjects.multiply();
System.out.println("multiply = " + multiply);
} else if (answer.equals("d")) {
double divide = operatorobjects.divide();
System.out.println("divide = " + divide);
}
/**
* More debug exiting
*/
System.out.println("exiting");
}
}
I have added a divide method, and renamed to multiply. The output from running is:
What would you like to do?
a
Enter number 1 10
Enter number 2 10
adding = 20.0
exiting
What would you like to do?
s
Enter number 1 10
Enter number 2 2
subtract = 8.0
exiting
What would you like to do?
m
Enter number 1 2
Enter number 2 5
multiply = 10.0
exiting
What would you like to do?
d
Enter number 1 6
Enter number 2 3
divide = 2.0
exiting

only allowing java scanner to allow numbers

I've been having issues catching non numbers.
I tried try / catch but I can't grasp a hold of it. If anything I get it to catch non numbers, but doesn't let the user try entering again... It just stops my code completely.
Here is my code:
package triangle;
import java.util.Scanner;
public class traingle {
public static void main(String[] args){
//explaining what the program is going to do
System.out.println("You will be able to enter 3 angles to equal the sum of a triangle,\nthis automated program will tell you if the angle you entered is issosoliese, eqilateral or scalene");
//creating input 1,2,3 for user
Scanner angle1 = new Scanner(System.in); // Reading from System.in
System.out.println("Enter angle degree number 1: ");
int n = angle1.nextInt();
Scanner angel2 = new Scanner(System.in);
System.out.println("Enter angle degree number 2: ");
int n2 = angel2.nextInt();
Scanner angel3 = new Scanner(System.in);
System.out.println("Enter angle degree number 3: ");
int n3 = angel3.nextInt();
//this is just telling how much degrees the user had in total if they didnt match up to triangle standards
This should get you started.
package triangle;
import java.util.Scanner;
public class triangle {
public static void main(String[] args){
//explaining what the program is going to do
System.out.println("You will be able to enter 3 angles to equal the sum of a triangle,\n");
System.out.println("this automated program will tell you if the angle you entered is isoceles, eqilateral or scalene");
//creating input 1,2,3 for user
Scanner s = new Scanner(System.in); // Reading from System.in
String line;
int [] angles = new int [3];
int count = 0;
do {
System.out.print("Enter angle degree number " + (count+1) + ": ");
line = s.nextLine();
while (true ) {
try {
angles[count] = Integer.parseInt(line);
System.out.println("You entered " + angles[count]);
count++;
break;
} catch (NumberFormatException e ) {
System.out.println("Invalid number: try again: ");
line = s.nextLine();
}
}
} while (count < 3);
}
}
You have a simple issue.. You just use one Scanner for the code in main method.
package triangle;
import java.util.Scanner;
public class traingle {
public static void main(String[] argc){
System.out.println("You will be able to enter 3 angles to equal the sum of a triangle,\nthis automated program will tell you if the angle you entered is issosoliese, eqilateral or scalene");
Scanner angle1 = new Scanner(System.in);
System.out.println("Enter angle degree number 1: ");
int n = angle1.nextInt();
System.out.println("Enter angle degree number 2: ");
int n2 = angel2.nextInt();
System.out.println("Enter angle degree number 3: ");
int n3 = angel3.nextInt();

Prompt and input numbers and calculate sum, difference, and product

The assignment is to "write a Java program to prompt for and input three numbers. Output the sum of the largest two numbers. Output the difference of the largest and smallest number. Output the product of the smallest two numbers."
I've written the code out but it's not calculating properly. I've included my code below so please feel free to take a look and critique as needed.
import java.util.Scanner;
public class Homework4a {
public static void main (String[] args) {
//Declare Scanner object and three numbers (ints) and ints for smallest and largest numbers
Scanner keyboard;
int firstUsernumber;
int secondUsernumber;
int thirdUsernumber;
int largestnumber;
int smallestnumber;
int largestnumber2;
int smallestnumber2;
int largestnumber3;
int smallestnumber3;
//Instantiate keyboard
keyboard = new Scanner(System.in);
//Prompt the user for input
System.out.print("Enter your first number here: ");
//Obtain and store first number
firstUsernumber = keyboard.nextInt();
//Prompt the user for second input
System.out.print("Enter your second number here: ");
//Obtain and store the second input
secondUsernumber = keyboard.nextInt();
//Prompt the user for third input
System.out.print("Enter your third number here: ");
//Obtain and store the second input
thirdUsernumber = keyboard.nextInt();
//Determine largest number
if (firstUsernumber > secondUsernumber) {
largestnumber = firstUsernumber;
smallestnumber = secondUsernumber;
} else {
largestnumber = secondUsernumber;
smallestnumber = firstUsernumber;}
if (secondUsernumber > thirdUsernumber) {
largestnumber2 = secondUsernumber;
smallestnumber2 = thirdUsernumber;
} else {
largestnumber2 = thirdUsernumber;
smallestnumber2 = secondUsernumber;}
if (firstUsernumber > thirdUsernumber) {
largestnumber3 = firstUsernumber;
smallestnumber3 = thirdUsernumber;
} else {
largestnumber3 = thirdUsernumber;
smallestnumber3 = firstUsernumber;
}//Ending bracket of if statement
//Calculate sum of largest numbers
System.out.println("The sum of the largest numbers is: " + (largestnumber + largestnumber2));
//Calculate the difference of the largest and smallest number
System.out.println("The difference of the largest number and smallest number is: " + (largestnumber - smallestnumber));
//Calculate the product of the smallest numbers
System.out.println("The product of the smallest numbers is: " + (smallestnumber*smallestnumber3));
}//Ending bracket method main
}//Ending bracket class Homework4a
import java.util.Scanner;
//this program takes three integers from the user and outputs the sum of the largest two numbers, the difference of the largest and smallest number, and the product of the smallest two numbers.
// done by Nadim Baraky
public class OperationsOnNumbers {
public static void main(String[] args) {
//declare three integer variables
int firstMax, secondMax, min;
//firstMax: largest number; secondMax: the number in between; min: the smallest number.
Scanner input = new Scanner(System.in);
System.out.print("Enter your first number: ");
int firstNumber = input.nextInt();
System.out.print("Enter your second number: ");
int secondNumber = input.nextInt();
System.out.print("Enter your third number: ");
int thirdNumber = input.nextInt();
input.close();
firstMax = Math.max(Math.max(firstNumber, secondNumber),thirdNumber);
if(firstMax == firstNumber) {
secondMax = Math.max(secondNumber, thirdNumber);
}
else if(firstMax == secondNumber) {
secondMax = Math.max(firstNumber, thirdNumber);
}
else {
secondMax = Math.max(firstNumber, secondNumber);
}
min = Math.min(Math.min(firstNumber, secondNumber), thirdNumber);
System.out.println("The sum of the largest two numbers is: " + (firstMax + secondMax));
System.out.println("The difference of the largest and smallest numbers is: " + (firstMax - min));
System.out.println("The product of the smallest two numbers: " + secondMax * min);
}
}

Loop not quite working java

I am new to stackoverflow. First I would like the program to loop with a price, then a question(enter another price?), price, then a question and so on. Below is the output.
Please enter a price:
33
Enter another price?
y
Please enter a price:
66
Please enter a price:
99
Please enter a price:
22
However it will keep looping at the end with "Please enter a price:". I want it to do:
Please enter a price:
33
Enter another price?
y
Please enter a price:
66
Enter another price?
y
Please enter a price:
22
Can anyone help me with this? Also, sometimes the average does not update fully. Thanks :)
import java.util.Scanner;
public class ReadInPrice {
public static void main(String[] args) {
int integer = 0;
int count = 0;
double sum = 0;
double average = 0;
Scanner input = new Scanner(System.in);
String addPrice;
System.out.println("Please enter a price: ");
integer = input.nextInt();
do {
System.out.println("Enter another price? ");
addPrice = input.next();
while (addPrice.equalsIgnoreCase("Y")) { // change this line to while user response = no etc may need a enter another number?
count = count + 1;
sum = sum + integer;
System.out.println("Please enter a price: ");
integer = input.nextInt();
}
}
while (addPrice.equalsIgnoreCase("Y"));
average = sum / count;
System.out.println("Average = " + average);
input.close();
}
}
You need to replace your while with an if
if (addPrice.equalsIgnoreCase("Y")) { // change this line to while user response = no etc may need a enter another number?
count = count + 1;
sum = sum + integer;
System.out.println("Please enter a price: ");
integer = input.nextInt();
}
In fact, addPrice is not modified within your second while loop, and so you have an infinite loop.
In order to do the averaged price, you're in the right way but not in the right place :P
count = count +1 and sum = sum + integer should be done after each integer = input.nextInt(). In your current code, you don't increment the counter and don't add the integer for the last input.
System.out.println("Please enter a price: ");
integer = input.nextInt();
count++ ; // count = count +1
sum += integer ; // sum = sum + integer
do {
System.out.println("Enter another price? ");
addPrice = input.next();
while (addPrice.equalsIgnoreCase("Y")) { // change this line to while user response = no etc may need a enter another number?
System.out.println("Please enter a price: ");
integer = input.nextInt();
count++ ; // count = count +1
sum += integer ; // sum = sum + integer
}
}
while (addPrice.equalsIgnoreCase("Y"));
Finally here is a improved version which avoid the use of if.
int sum = 0;
int integer = 0;
String addPrice = "Y";
while( "Y".equalsIgnoreCase(addPrice) ) {
System.out.println("Please enter a price: ");
integer = input.next();
sum += integer ;
count++;
System.out.println("Enter another price? ");
addPrice = input.next();
}
int avg = sum / count ;
What you should do is change your logic a bit. You need to repeat two actions, entering a price and asking if the user wants to enter another price. Only one loop is required for this.
do {
System.out.println("Please enter a price: ");
integer = input.nextInt();
count = count + 1;
sum = sum + integer;
System.out.println("Enter another price? ");
addPrice = input.next();
} while (addPrice.equalsIgnoreCase("Y"));
i think you want something like this:
import java.util.Scanner;
public class ReadInPrice {
public static void main(String[] args) {
int integer = 0;
int count = 0;
double sum = 0;
double average = 0;
Scanner input = new Scanner(System.in);
String addPrice = "Y";
while (addPrice.equalsIgnoreCase("Y")){
System.out.println("Please enter a price: ");
integer = input.nextInt();
count++;
sum += integer;
System.out.println("Enter another price? ");
addPrice = input.next();
}
average = sum / count;
System.out.println("Average = " + average);
input.close();
}
}
Try this:
EDIT Added min and max.
public class ReadInPrice {
public static void main(String[] args) {
//better to use 2 scanners when dealing with both string and int
//one for string ; one for ints
Scanner strScanner = new Scanner(System.in);
Scanner intScanner = new Scanner(System.in);
boolean enter = true;
int sum = 0;
int count = 0;
int min=Integer.MAX_VALUE;
int max=0;
while (enter) { //while user wants to keep adding numbers
System.out.println("Please enter a price: ");
int price = intScanner.nextInt();
if(price < min)
min=price;
if(price > max)
max=price;
sum += price;
count++;
System.out.println("Enter another price? ");
String answer = strScanner.nextLine();
if (!answer.equalsIgnoreCase("Y"))
enter = false; //user doesn't want to keep adding numbers - exit while loop
}
double average = (double)sum / count;
System.out.println("Average = " + average);
System.out.println("Min = " + min);
System.out.println("Max = " + max);
strScanner.close();
intScanner.close();
System.exit(0);
}
}

Categories