package Myth;
import java.util.Scanner;
public class Practice
{
public static void heightEstimator(double momH, double dadH)
{
Scanner kb= new Scanner(System.in);
System.out.println("Enter your Mothers Height in Feet: ");
momH= kb.nextInt();
System.out.println("Enter your Fathers Height in Feet: ");
dadH= kb.nextInt();
double w= (dadH+momH)/2;
System.out.println("You will be "+ w+ "Ft.");
}
public static int shoeSize(double x)
{
Scanner z= new Scanner(System.in);
System.out.println("Enter your Fathers Heigh in Inches: ");
x= z.nextInt();
double y= x/6;
System.out.println("Your shoe size will be: "+ y);
return 0;
}
public static void main(String[] args)
{
heightEstimator(0, 0);
shoeSize(0);
}
}
I'm not sure as to why I keep getting this error in my code. What's wrong with my code?
momH= kb.nextInt(); is wrong because datatytpe "momH" and "dadH" and "x" is a double datatype, and not integer.
*
You should change nextInt() to nextDouble();
*
Related
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()
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();
public class maathclass {
public void someNumbers() {
int z = 4;
System.out.println(z);
}
}
//Trying to take variable z to add with answer from maathclass.java
import java.util.Scanner;
public class input {
public static void main (String args[]) {
double fnum, snum, answer;
Scanner userInput= new Scanner(System.in);
maathclass mathObject = new maathclass();
mathObject.someNumbers();
System.out.println("Enter first number:");
fnum = userInput.nextDouble();
System.out.println("Enter second number:");
snum = userInput.nextDouble();
// i want to add the variable z to the answer
answer = fnum + snum;
System.out.println("Answer:" + answer);
}
}
Well you can do this
maathclass.java
public class maathclass {
private int z = 4
public void setZ(int z) {
this.z = z;
}
public int getZ(){
return z;
}
}
input.java
import java.util.Scanner;
public class input {
public static void main (String args[]) {
double fnum, snum, answer;
Scanner userInput= new Scanner(System.in);
maathclass mathObject = new maathclass();
mathObject.setZ(4);
System.out.println("Enter first number:");
fnum = userInput.nextDouble();
System.out.println("Enter second number:");
snum = userInput.nextDouble();
// i want to add the variable z to the answer
answer = fnum + snum + maathclass.getZ();
System.out.println("Answer:" + answer);
}
}
or else if you desire to set the value of z from maathclass then just initialize it with the value in maathclass constructor.
Hopes this helps.
Implement a method that is passed by two double type numbers, prints out their total and doesn't return anything. Guys how does this look now???
import java.util.Scanner;
public class Tutorial1 {
public static void main1(double num1, double num2)
{
double numb1=3.5;
double numb2=3.4;
double total = numb1 + numb2;
Scanner reader = new Scanner (System.in);
System.out.println(total);
numb1 = reader.nextInt();
numb2 = reader.nextInt();
}
I didn't understand what u want well try this hope helps to u
import java.util.Scanner;
public class MyFirstJavaProgram {
public static void main(String []args) {
main1(3.65,2.85);
}
public static double main1 (double numb12, double numb21){
int numb1 = 0, numb2 = 0;
int total = numb1 + numb2;
Scanner reader = new Scanner(System.in);
System.out.println("Enter two integers: ");
numb1 = reader.nextInt();
numb2 = reader.nextInt();
System.out.println(total+numb1+numb2);
return total+numb1+numb2;
}
}
When I execute my program the console is completely empty. I have no idea what is causing this to happen. I am not receiving any error messages except for the "scanner not closed" warning. What is causing this issue?
public class BMIprogram {
public static void main(String[] args) {
double BMI;
double weight;
double height;
Scanner bodymassScan = new Scanner(System.in);
weight = bodymassScan.nextInt();
height = bodymassScan.nextInt();
System.out.println("Enter weight in pounds: ");
System.out.println("Enter height in inches: ");
BMI = ((weight/Math.pow(height, 2)) * 703);
System.out.println(BMI + " is the BMI");
}
}
Just rearrange your code! The Scanner is waiting for you to make two inputs before printing out the statements.
public class BMIprogram {
public static void main(String[] args) {
double BMI;
double weight;
double height;
Scanner bodymassScan = new Scanner(System.in);
System.out.println("Enter weight in pounds: ");
weight = bodymassScan.nextInt();
System.out.println("Enter height in inches: ");
height = bodymassScan.nextInt();
BMI = ((weight/Math.pow(height, 2)) * 703);
System.out.println(BMI + " is the BMI");
}
}
You can change the code line order:
System.out.println("Enter weight in pounds: ");
weight = bodymassScan.nextInt();
System.out.println("Enter height in inches: ");
height = bodymassScan.nextInt();
As the bodymassScan is waiting for your input before printing in System.out.
Console is empty as it is waiting for your input. When you are executing your code its creating an scanner object and when you are using nextint() method its taking the input using scanner object. After that you are printing "Enter weight in pounds" on console.
Case you want it in proper way you need to rearrange the code like:
System.out.println("Enter weight in pounds: ");
weight = bodymassScan.nextInt();
System.out.println("Enter height in inches: ");
height = bodymassScan.nextInt();