Syntax error on token ";" in Java [closed] - java

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.

Related

Dividing Floats in Java [closed]

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

impossible to pass user's input to the array [closed]

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.

Write an application that prints the sum of cubes [closed]

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
3.2: Sum of Cubes
Write an application that prints the sum of cubes. Prompt for and read two integer values and print the sum of each value raised to the third power.
SPECIFICATION OF PROMPTS, LABELS AND OUTPUT :Your code should use these prompts: "integer1: " and "integer2 ".The prompts should not force the user to type the required input on the next line. After all the inputs are read, the output should consist of a single line consisting of the label "the sum of the cubes of these numbers is:" followed by your calculated value . For example:
integer1: 3
integer2: 5
the sum of the cubes of these numbers is: 152
SPECIFICATION OF NAMES: Your application class should be called CubeSum
Error:
Expected Output:
integer1:·↵
integer2:·↵
the·sum·of·the·cubes·of·these·numbers·is:·9
Actual Output:
integer1:integer2:the·sum·of·these·cubes·is:·9↵
My Code:
import java.util.*;
public class CubeSum {
public static void main (String args []) {
Scanner scan = new Scanner(System.in);
int integer1, integer2, cube1, cube2;
System.out.print("integer1: ");
integer1=scan.nextInt();
cube1 = (int)Math.pow(integer1 ,3);
System.out.print("integer2: ");
integer2=scan.nextInt();
cube2 = (int)Math.pow(integer2 ,3);
System.out.println("the sum of these cubes is: " + (cube1 + cube2));
}
}
Change this line:
System.out.println("the sum of these cubes is: " + (cube1 + cube2));
You was missing the + to concat the output.
Also, math is a class, so you should use it like this:
cube1 = (int) Math.pow(integer1, 3);
EDIT
import java.util.*;
public class CubeSum {
public static void main (String args []) {
Scanner scan = new Scanner(System.in);
int integer1, integer2, cube1, cube2;
System.out.print("integer1: ");
integer1=scan.nextInt();
cube1 = (int)Math.pow(integer1 ,3);
System.out.print("integer2: ");
integer2=scan.nextInt();
cube2 = (int)Math.pow(integer2 ,3);
System.out.println("the sum of these cubes is: " + (cube1 + cube2));
}
}
In your line that prints the output, you are indeed missing a '+':
Change the line to:
System.out.println("the sum of these cubes is: " + (cube1 + cube2));
Additionally, there's two more mistakes:
cube1 = math.pow(int1 ,3);
should be
cube1 = (int)Math.pow(integer1 ,3);
and
cub2 = Math.pow(int2, 3);
should be
cube2 = (int)Math.pow(integer2, 3);
since you never actually define 'int1' or 'int2'

illegal start of expression in line 22 [closed]

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

Getting error: ';' expected [closed]

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 9 years ago.
Improve this question
This my code
import java.util.*;
import java.util.Collections;
public class Customer
{
public static void main(String args[]){
Arraylist listcustomer1 = new Arraylist();
Arraylist listcustomer2 = new Arraylist();
Scanner scan = new Scanner(System.in);
String name,city;
int custId,numOfPurchases;
for(i=0;i<30;i++)
{
System.out.println("Enter customer name : ");
name = scan.next();
System.out.prinln("Enter customer id :" );
int custId= scan.nextInt();
System.out.println("Enter number of purchases :");
int numOfPurchases = scan.nextInt();
System.out.println("Enter the city :");
city = scan.next();
Customer a.new Customer(name,custId,numOfPurchases,city);
listcustomer1.add(a);
}
int total =0,avg = 0;
for(int i=0;i<listcustomer1.numOfPurchase;i++)
{
total= total+numOfPurchase;
avg = total/listcustomer1;
if(listcustomer1.numOfPurchase<10){
listcustomer1.remove(i);
Collections.copy(listcustomer2,i);
}
}
System.out.println("Customer Purchase Information ");
System.out.println("Total number of purchases from all cities " +total());
System.out.println("Average number of purchase from all cities " +avg());
}
}
I got this error after i running it :
Customer.java:27: error: ';' expected
Customer a.new Customer(name,custId,numOfPurchases,city);
Is this error for missing semicolon? I already put but the error still there.
Remove . from this line and add =
Customer a.new Customer(name,custId,numOfPurchases,city);
should be:
Customer a = new Customer(name,custId,numOfPurchases,city);
Customer a = new Customer(name,custId,numOfPurchases,city);
replace dot with equals symbol
Customer a.new Customer(name,custId,numOfPurchases,city);

Categories