Getting error: ';' expected [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 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);

Related

Keep getting error that says "error: variable service is already defined in method getData(SpaService)" [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 days ago.
Improve this question
Here's my line of code, I'm trying to follow this book but I can't figure out what I'm doing wrong.
import java.util.Scanner;
public class CreateSpaServicesPt2ish {
public static void main(String[] args) {
SpaService firstService = new SpaService();
SpaService secondService = new SpaService();
firstService = getData(firstService);
secondService = getData(secondService);
System.out.println("First service details:");
System.out.println(firstService.getServiceDescription() +
" $ " + firstService.getPrice());
System.out.println("Second service details:");
System.out.println(secondService.getServiceDescription()
+ " $ " + secondService.getPrice());
}
public static SpaService getData(SpaService service) {
String service;
double price;
Scanner keyboard = newScanner(System.in);
System.out.print("Enter service >> ");
service = keyboard.nextLine();
System.out.print("Enter price >> ");
price = keyboard.nextDouble();
keyboard.nextLine();
service.setServiceDescription(service);
service.setPrice(price);
return service; }
}
The outcome should look like the one from the last version i made, which was this

Why I can't add elements to HashMap (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 2 years ago.
Improve this question
I don't know what I'm doing wrong... I have the next code:
public class Administrator {
private static Map<Integer, Professor> professors = new HashMap<Integer, Professor>();
private static void addProfessor(){
Scanner scanner = new Scanner(System.in);
System.out.println("\nADD PROFESSOR");
System.out.print("\tId: ");
Integer id = scanner.nextInt();
System.out.print("\tName: ");
String name = scanner.next();
System.out.print("\tLast name: ");
String lastname = scanner.next();
System.out.print("\tInit date (AAAA/MM/DD) (including slashes): ");
LocalDate date = LocalDate.parse(scanner.next());
if(professors.put(id, new Professor(id, name, lastname, date)) != null) {
System.out.println("Professor added successfully.");
} else {
System.err.println("We can't add the professor.");
}
}
}
And when I call the addProfessor method, the error "We can't add the professor." is printed. I don't know why the element is not added to the HashMap. There isn't any exception on my console, so I don't have any way to know whats wrong.
When calling Map.put(), the previous entry for the given key in the map is returned. If put() returns null, then the entry is added, but it didn't overwrite anything.

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.

read from two separate text files and write into one [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 4 years ago.
Improve this question
need to combine two text files of girl names and boy names into one text file. the new file has to have the boy names and girl names separated into two lists of each gender, and we dont know how many names each file will have. the program runs but gets stuck in an infinite loop
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;
public class NameTester
{
public static void main(String args[]) throws FileNotFoundException
{
Scanner userIn = new Scanner(System.in);
System.out.println("Enter file name for boys name file: ");
String boyFile = userIn.next();
System.out.println("Enter file name for girls name file: ");
String girlFile = userIn.next();
userIn.close();
Scanner boyIn = new Scanner(new FileReader(boyFile));
Scanner girlIn = new Scanner(new FileReader(girlFile));
PrintWriter out = new PrintWriter("names.txt");
out.print("Boys Names: Girls Names: ");
int count = 1;
while(boyIn.hasNextLine() || girlIn.hasNextLine());
{
String b = boyIn.next();
String g = girlIn.next();
out.print(count + " " + b + " " + count + " " + g);
count++;
}
boyIn.close();
girlIn.close();
out.close();
}
}
This line is an empty while loop that will run forever:
while(boyIn.hasNextLine() || girlIn.hasNextLine());
Get rid of the semicolon at the end:
while(boyIn.hasNextLine() || girlIn.hasNextLine()) // <- NO SEMICOLON
{
....
}
I haven't checked for other logic errors in your program, but this should get rid of the infinite loop.

Syntax error on token ";" 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 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.

Categories