Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Please could someone take a look at my code. It won't compile because it says ; expected here - }elseif (convert == 2){.
I don't understand why, I'm pretty new to this.
Regards.
//user chooses what unit of measurments to convert, inputs number and displays conversion.
class Convertweight
{
public static void main (String [] args)
{
Scanner input = new Scanner(System.in);
//Declare variables
double feet=0.0, temp=0.0, weight=0.0, metres=0.0, f=0.0, stone=0.0, convert=0.0;
//choices of measurment to convert
System.out.println("Please choose and enter either 1,2 or 3 from the measurements below.");
System.out.println("1. Feet to metres");
System.out.println("2. C temp to F temp");
System.out.println("3. KG to stone");
convert=input.nextDouble();
//convert measurment if option 1 is chosen.
if (convert == 1) {
System.out.println("Enter measurement");
feet=input.nextDouble();
metres=feet/3.28;
System.out.println(metres + "m");
//convert temperature if option 2 is chosen.
} elseif (convert == 2){
System.out.println("Enter temperature in celsius");
temp=input.nextDouble();
F=C*1.8+32;
System.out.println(F + "f");
//convert weight if option 3 is chosen.
} elseif (convert == 3){
System.out.println("Enter weight in KG");
weight=input.nextDouble();
stone=weight/6.35029318;
System.out.println(stone+"st");
}
else if is the correct format elseif will cause a compilation error. check the documentation for better understanding.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
First day learning Java and I came across this problem: I need to divide 2 floats(given from command line input). I am getting an error every time I input my 2 floats. I included my code down below
import java.util.Scanner;
public class doubledivision
{
public static void main(String [] args)
{
float num1, num2;
Scanner in = new Scanner(System.in);
System.out.print("Please enter two floating point numbers: ");
num1 = in.nextInt();
num2 = in.nextInt();
float result = num1 / num2;
System.out.println(result);
}
}
If you want to read a number of type float from the user, you need to use :
in.nextFloat()
because this in.nextInt() will return a value of type int.
Also you need to consider some things :
You need to clear the scanner since you want 2 floating numbers , why ? because if the user enters :
>>> 1.0 2.0
The space will make your scanner assigne num1 with 1.0 and num2 with 2.0.
So if you want to ask the user 2 times use :
num1 = in.nextFloat();
in.nextLine(); // to refresh
num2 = in.nextFloat();
If the user enters zero (any number) divided by 0 will blow up​ your app , so add a check :
if(num2==0){
System.out.println("NaN");
}
else {
float result = num1 / num2;
System.out.println(result);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I write a program where I need to take user input (prices) and giving the sum of all prices as output. Naturally prices can't be negative, so my question is: How does the program accept positive numbers only?
You want something like this
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a price: ");
double number = input.nextDouble();
while (number < 0 ) {
System.out.print("Sorry, but your price must be a positive decimal. Enter a price: ");
number = input.nextDouble();
}
System.out.println("Your price is " + number);
}
}
Use a while loop to keep re-checking if the entered price meets your standards.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I've got a strange issue (or at least strange to me). I've got a couple if-else-if statements that are causing strange behavior. For context, I'm essentially taking a char input using the Scanner class and using Scanner.nextInt().charAt(0). This appears to work fine if I check for the required characters and input a totally random character (it returns text to the terminal, breaks out, and exits). However, if I just hit enter at the char prompt, it creates what appears to be an infinite loop (maybe a memory leak?). Attached is the code I'm working on, as well as screenshots of it running in Coderunner2.
Thanks in advance!
import java.util.*;
import java.io.*;
public class phonebill
{
public static void main(String[] arg)
{
Scanner sc = new Scanner(System.in);
System.out.print("Please input the account number: ");
int acct = sc.nextInt(); // reads arbitrary account number
System.out.print("Please input service code: ");
char svc_code = sc.next().charAt(0); // reads service code
character
/* the following blocks check for the appropriate service codes.
* valid codes include p or P for 'Premium' service, and
* r or R for regular service. if no valid code is read,
* the program exits, informing the user to input a valid code.
*/
if (svc_code == 'p' || svc_code == 'P')
{
System.out.print("Please input daytime minutes: ");
int day_min = sc.nextInt();
System.out.print("Please input nighttime minutes: ");
int night_min = sc.nextInt();
System.out.println("Service code is " + svc_code + " and this condition works.");
}
else if (svc_code == 'r' || svc_code == 'R')
{
System.out.print("Please input used minutes: ");
int mins = sc.nextInt();
System.out.println("Service code is " + svc_code + " and this condition works.");
}
else
{
System.out.println("Please input a valid service code.");
}
System.out.print("That's all for now folks");
}
}
Code running normally
Code being weird
UPDATE: I'm just bad at Java and scanners.
It's not an infinite loop, or a memory leak.
It's just waiting for you to enter a non-empty string.
$ java phonebill
Please input the account number: 123
Please input service code:
string
Please input a valid service code.
That's all for now folks
(The blank lines are where I was just hitting enter. I eventually typed "string", and it stopped).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I was required to make a code that accepted a binary number (1's and 0's) and then counted how many ones were in that binary number. My code fulfills this purpose.
The second part of the exercise is this: if the user enters a number that is NOT binary, I must output that there is an error and keep prompting the user until they give a binary number.
Can someone show me how to incorporate this? I have tried several times but cannot make it click. Thanks! Here is my code.
import java.util.Scanner;
public class NewClass
{
public static void main( String [] args )
{
Scanner scan = new Scanner( System.in);
int i = 0, count = 0;
String number;
System.out.println("Please enter a binary number.");
number = scan.next();
String number1 = "1";
while ((i = number.indexOf(number1, i++)) != -1) {
count++;
i += number1.length();
}
System.out.println("There are "+ count + " ones in the binary number.");
}
}
You already know how to find all of the 1's; you should be able to do something similar to find all of the 0's.
If the sum of those two counts is not the length of the string, there must be at least one illegal character in it.
If you use the parseInt method you can do
Integer.parseInt("1100110", 2) returns 102
or in your case
int intVal = Integer.parseInt(number, 2);
as per the javadocs
Throws:
NumberFormatException - if the String does not contain a parsable int.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am a beginner,I'm having problem with my very first code.I just trying to solve coupling shaft alignment.
It says, "illegal start of expression".Can someone help me with this problem? thanks
Here's my code:
import java.util.Scanner;
public class Front {
public static void main(String []args){
Scanner input = new Scanner(System.in);
Double A = input.nextDouble();
Double B = input.nextDouble();
Double C = input.nextDouble();
Double S = input.nextDouble();
Double M = input.nextDouble();
Double F = B / A * (S-M)/2 - S/2;
System.out.println("The front foot is: " + F +);
}
}
You missed an operand after your last + in System.out.println("The front foot is: " + F +);
Another option is to use formatted output:
System.out.printf("The front foot is: %f\n", F.floatValue());