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());
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 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 2 years ago.
Improve this question
I am trying to pass user's input to the array through a scanner. The goal is to make the average value of the inputs, this is the code:
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
double[] str1 = new int [20] ;
str1 = scanner.next.Double();
System.out.println("Average is: " + Arrays.stream(str1).summaryStatistics().getAverage());
}
}
Below 2 statements are having syntax error and because of this code will not compile:
double[] str1 = new int [20] ;
str1 = scanner.next.Double();
you need to declare double array as double[] str1 = new double[20];
you need to take the double value as input like this scanner.nextDouble(); and as str1 is array we can't store the input directly as str1 = scanner.nextDouble(); in that case your program will give compile time error
Type mismatch: cannot convert from double to double[]
To resolve this error we need to store the value of every input at specific index like str1[0] = scanner.nextDouble();
Below is the working code as per your requirement(as I understand from your question):
Scanner scanner = new Scanner(System.in);
double[] str1 = new double[20] ;
for(int i = 0; i < 20;i++) {
str1[i] = scanner.nextDouble();
}
System.out.println("Average is: " + Arrays.stream(str1).summaryStatistics().getAverage());
I hope it will help you resolve the error you're facing.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm learning Java, and I wanted to make a very basic calculator. But looks like I got a problem right a way!
Here is the code:
import java.util.Scanner;
public class apples{
public static void main(String args[]){
int test = 6;
if(test != 9){
System.out.println("Yes");
}else{
System.out.println("No");
}
}
}
So in Eclipse I tried to run this but the problem is it does not work and show this error:
Exception in thread "main" java.lang.Error: Unresolved compilation
problem:
at apples.main(apples.java:4)
Your second last bracket has some invisible character that you can see a red point(very thin) and if you delete the same your program is perfectly ok.
Incorrect Code having special character in it
import java.util.Scanner;
public class AdvanceCollection {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Enter first number: ");
fnum = scan.nextDouble();
System.out.println("Enter second number: ");
snum = scan.nextDouble();
answer = fnum + snum;
System.out.println(answer);
}
}
Correct code with out that issue
import java.util.Scanner;
public class AdvanceCollection {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Enter first number: ");
fnum = scan.nextDouble();
System.out.println("Enter second number: ");
snum = scan.nextDouble();
answer = fnum + snum;
System.out.println(answer);
}
}
Change your character at the end to }.
There is no compilation error, just noticed last } is having some invisible character with it. delete it and type again }. Hopefully it should work.
It works for me.
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.
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
"Syntax error on token ";", Primitive Type expected after this token" is the message i get rite after the semi colon on this statement "String data = input.next(); " and i don't know why.
import java.util.*;
public class PorterHouse {
public static void main(String[] args){
System.out.println("enter interest rate, loan mount, and number of years: ex. "+
" 7.9-400000-5");
Scanner input = new Scanner(System.in);
String data = input.next();
[] b = data.split("[-]");
double interstRate = Double.parseDouble(b[0]);
double loanPrincipal = Double.parseDouble(b[1]);
double loanLength = Double.parseDouble(b[2]);
double monthlyPayment = (b[1]*b[2] / 1);
double totalPayment = ( monthlyPayment * loanLength * 12);
System.out.println("Monthly Pament: " + monthlyPayment);
System.out.println("totalPayment: " + totalPayment);
}
}
Try to change this:
[] b = data.split("[-]");
with this:
string[] b = data.split("[-]");
Presently [] b has no data type. So do provide a data type to it.
I guess instead of :
[] b = data.split("[-]");
you need something like :
String[] b = data.split("[-]");
also
you might wanna replace :
double monthlyPayment = (b[1]*b[2] / 1);
by :
double monthlyPayment = (loanPrincipal * loanLength / 1); //idk why divide by 1??
cuz, b[1] and b[2] are two Strings.