Java .class is expected [duplicate] - java

This has been giving me error when I try to invoke the addAccount method saying .class is expected on the line where I try to invoke it.
I am trying to do an assignment whereby I am supposed to create a method called addAccount that accepts two parameters; accountName and accountBalance and put the parameters into an accountArray
import java.util.*;
public class bank
{
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
int choice = 0;
int accountNo = 0;
double accountBal = 0;
int[] accountNoArray = new int[20];
int[] accountBalArray = new int[20];
displayMenu();
System.out.print("Please Enter Your Choice: ");
choice = sc.nextInt();
if(choice == 1)
{
System.out.print("Please Enter NRIC number: ");
accountNo = sc.nextInt();
System.out.print("Please Enter Account Balance: ");
accountBal = sc.nextInt();
}
public static void displayMenu()
{
System.out.println("Menu");
System.out.println("1. Add an account");
System.out.println("2.Search an account with the given account number");
System.out.println("3.Display accounts below the given balance");
System.out.println("4.Exit");
}
public static void addAccount(int accountNo,double accountBal)
{
}
}

You are missing a closing curly bracket before the definition of the displayMenu method. The following version is syntactically correct:
public class Bank {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice = 0;
int accountNo = 0;
double accountBal = 0;
int[] accountNoArray = new int[20];
int[] accountBalArray = new int[20];
displayMenu();
System.out.print("Please Enter Your Choice: ");
choice = sc.nextInt();
if (choice == 1) {
System.out.print("Please Enter NRIC number: ");
accountNo = sc.nextInt();
System.out.print("Please Enter Account Balance: ");
accountBal = sc.nextInt();
}
}
public static void displayMenu() {
System.out.println("Menu");
System.out.println("1. Add an account");
System.out.println("2.Search an account with the given account number");
System.out.println("3.Display accounts below the given balance");
System.out.println("4.Exit");
}
public static void addAccount(int accountNo, double accountBal) {
}
}

Do not specify parameter type while calling a method.
Here is an error:
case 1 : addAccount(int accountNo,double accountBal);
It must be:
case 1 : addAccount(accountNo,accountBal); break;

you have declared it static so you need to add the class name before the call
bank.addAccount(int accountNo,double accountBal);
and instead of the types decleration you have to call it with parameters.
bank.addAccount(accountNo,accountBal);

Related

Create account and Login

im having some trouble figuring out how i should approach this task. I would like to create an option to create an account and then have a login section. I am trying to figure out how to pass the information to the login class as i would like to be able to login without creating a new account every time. im not sure if this current code would allow for multiple accounts at the same time. I am open to suggestions if so on how to do that, but im fine with one account.
Here is my code so far:
CreateAccount class
import java.util.Scanner;
public class CreateAccount{
public static void main(){
CreateAccount obj1 = new CreateAccount();
obj1.accountNumberPin();
}
public void accountNumberPin(){
System.out.println("create a six digit account number: ");
Scanner input = new Scanner(System.in);
int accNum = input.nextInt();
System.out.println("create four digit pin: ");
Scanner input2 = new Scanner(System.in);
int accPin = input2.nextInt();
}
public int getAccNum(){
return accNum;
}
public int getAccPin(){
return accPin;
}
}
Login Class
import java.util.Scanner;
public class Login{
public void login(){
CreateAccount test= new CreateAccount();
int theaccNum = test.getAccNum();
int theaccPin = test.getAccPin();
System.out.println("Enter account number: ");
Scanner input1 = new Scanner(System.in);
int accNumberInput= input1.nextInt();
System.out.println("Enter account pin: ");
Scanner input2= new Scanner(System.in);
int accPinInput = input2.nextInt();
if(accNumberInput==theaccNum && accPinInput == theaccPin){
System.out.print("1 2 1 2 this is just a test");
}
}
}
PS, i know there is an issue currently with the get methods
You should start by having an Account class that only contains the data of the account:
class Account {
private final int number;
private final int pin;
public Account(int number, int pin) {
this.number = number;
this.pin = pin;
}
public int getNumber() {
return number;
}
public int getPin() {
return pin;
}
}
Then implement the logic in another class. This class can maintain a list of the created accounts. That list will persist as long as you don't stop the program. (if you need persistence across restarts then you need some kind of storage, that is a file or a database).
Here is an example where I tried to keep as much of your code as possible:
public class Main {
private static final Map<Integer, Account> accounts = new HashMap<>();
public static void main(String[] args) {
while (true) {
System.out.println("Choose an option:");
System.out.println("1 - Create an account");
System.out.println("2 - Login into an account");
System.out.println("3 - List account numbers");
System.out.println("4 - Quit");
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
if (choice == 1) {
createAccount();
} else if (choice == 2) {
login();
} else if (choice == 3) {
listAccountNumbers();
} else if (choice == 4) {
break;
} else {
System.out.println("Invalid choice");
}
}
}
private static void createAccount() {
System.out.println("create a six digit account number: ");
Scanner input = new Scanner(System.in);
int accNum = input.nextInt();
System.out.println("create four digit pin: ");
Scanner input2 = new Scanner(System.in);
int accPin = input2.nextInt();
Account account = new Account(accNum, accPin);
accounts.put(accNum, account);
}
private static void login() {
System.out.println("Enter account number: ");
Scanner input1 = new Scanner(System.in);
int accNumberInput= input1.nextInt();
System.out.println("Enter account pin: ");
Scanner input2= new Scanner(System.in);
int accPinInput = input2.nextInt();
Account account = accounts.get(accNumberInput);
if(account.getPin() == accPinInput) {
System.out.print("Valid credentials for account number " + account.getNumber());
} else {
System.out.print("Invalid credentials for account number " + account.getNumber());
}
}
private static void listAccountNumbers() {
accounts.keySet().forEach(System.out::println);
}
}
public Account deposit(double amount) {
balance += amount;
return new Account(getID(), getCustomer(), getBalance());
}
public Account withdraw(double amount) {
if(balance >= amount) {
balance -= amount;
}
return new Account(getID(), getCustomer(), getBalance());
}

How to add array values

How do I add an array with several methods together
public void inputArray() {
Scanner keyboard = new Scanner (System.in);
System.out.println("Please enter the number of invoices: ");
numInvoices = keyboard.nextInt();
Invoices = new Invoice[numInvoices];
int BillTotal = 0;
for(int i = 0; i < numInvoices; i++){
Invoices[i] = new Invoice();
Invoices[i].setCompanyNameFromUser();
Invoices[i].setBillAmountFromUser();
Invoices[i].SetDateFromUser();
BillTotal = BillTotal + Invoices[i].setBillAmountFromUser;
In this case I want to add up the values input by the user in the setBillAmountFromUser method.
This program block may help you
public void inputArray()
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Please enter the number of invoices: ");
int numInvoices = keyboard.nextInt();
int BillTotal=0;
Invoice Invoices[numInVoices];
for(int i=0;i<numInVoices;i++)
{
Invoices[i]=new Invoices();/*Your Code*/
BillTotal+=Invoices[i].setBillAmountFromUser();
}
System.out.println("BillTotal="+BillTotal);
}
public int setBillAmountFromUser()/*Example Code block/*
{
/*your code */
return 5;//Example Return value
}

I want to make my java program executable like .exe?

This is my java code...I want to make it double click run forever after compiling once..!
Please Guide me How can I make this .. ?
import java.util.Scanner;
public class Functions {
public static int addnumbers(){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("\nMethod Call - Addition");
System.out.print("\nEnter a = ");
a=input.nextInt();
System.out.print("Enter b = ");
b=input.nextInt();
return(a+b);
}
public static int subnumbers(){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("Method Call - Subtraction");
System.out.print("\nEnter a = ");
a=input.nextInt();
System.out.print("Enter b = ");
b=input.nextInt();
return(a-b);
}
public static int mulnumbers(){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("Method Call - Multiply");
System.out.print("\nEnter a = ");
a=input.nextInt();
System.out.print("Enter b = ");
b=input.nextInt();
return(a*b);
}
public static int quonumbers(){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("Method Call - Quotient");
System.out.print("\nEnter a = ");
a=input.nextInt();
System.out.print("Enter b = ");
b=input.nextInt();
return(a/b);
}
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int n;
System.out.println("**Calcultor**\n\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Quotient");
System.out.print("\nEnter no : ");
n=input.nextInt();
if(n==1) {
System.out.println("\nResult = "+addnumbers());
}
if(n==2) {
System.out.println("\nResult = "+subnumbers());
}
if(n==3) {
System.out.println("\nResult = "+mulnumbers());
}
if(n==4) {
System.out.println("\nResult = "+quonumbers());
}
System.out.println("\nControl exit!");
}
}
Tell me any way to do this....
Launch4j
JSmooth
probably a lot more.
These are programs that wrap the Jar file and dependencies to create a native executable for Windows.

Java - Trouble with brackets

I am having some trouble with the placing of brackets. I wanted to write a few methods within the confines of my main method, but I always end up with with a bunch of red lines and errors telling me "Multiple markers at this line
- Syntax error on token "void", # expected
- addVehicleBooking cannot be resolved to a type"
I don't want my methods to return anything, I just want them to execute some code and print some stuff on the screen.
EDIT:
This is the start of the code, no need to worry about unused variables and such. Thanks for everyone's help =].
import java.util.Scanner;
public class FerryMenu {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
public static void addVehicleBooking()
{
String booking_ID = "";
System.out.print("Enter your booking ID");
booking_ID = input.next();
String registration = "";
System.out.print("Enter registration number");
registration = input.next();
String make_model = "";
System.out.print("Enter vehicle make/model");
make_model = input.next();
int number_passengers = 1;
System.out.print("Enter number of passengers");
number_passengers = scan.nextInt();
}
String menu_choice = "";
while(!"X".equals(menu_choice)){
System.out.println("*** Ferry Ticketing System Menu ***");
System.out.println("A - Add Vehicle Booking");
System.out.println("B - Display Booking Info");
System.out.println("C - Update Insurance Status");
System.out.println("D - Display Booking Summary");
System.out.println("X - Exit");
System.out.print("Enter your selection: ");
menu_choice = input.next();
}
}
}
You can't declare methods inside a method.. It's not about brackets.. It's about syntax.
Ok, Dean, here I'll describe it once again..
First thing, throw away the code that you have written.. Lets start fresh..
Follow these steps to approach your problem: -
Create a class say Demo
Add a method to that class, getUserInput()
Add main method also to your class.
Have a constructor (0-arg)
Now, your program starts executing from main().. If you want to take user input.. Call your getUserInput() method from here.. As the first statement..
In your getUserInputMethod(), after reading all the input, invoke your constructor to initialize your instance variables..
After this, your getUserInput() will return control to your main() method.. You can proceed with your code from there..
You can not define a method inside a method. Declare it outside the method and inside the class.
public class FerryMenu {
public static void addVehicleBooking()
{
//...
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
// just call the method here
addVehicleBooking();
//...
}
}
you are writing your addvehicalBooking method inside your main method.
thus those red line :remove that method from main.
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
}
public static void addVehicleBooking()
{
String booking_ID = "";
System.out.print("Enter your booking ID");
booking_ID = input.next();
String registration = "";
System.out.print("Enter registration number");
registration = input.next();
String make_model = "";
System.out.print("Enter vehicle make/model");
make_model = input.next();
int number_passengers = 1;
System.out.print("Enter number of passengers");
number_passengers = scan.nextInt();
}
Try taking out addVehicleBooking() outside main and call it and declare the variables in a constructor
You have a static method inside your main static method which is probably causing the problems. Have you tried moving the addVehicleBooking() method to the space between public class FerryMenu and the main method? (the addVehicleBooking() method should be a member of the class FerryMenu, not the main method)
Why are you making a static method inside public static void main. I dont think that there is a need to make it.
Remove the word static from "static void addVehicleBooking".
I'll edit this answer and add the corrected code asap.
you can not define method in method.
you can define it in class like this:
import java.util.Scanner;
public class FerryMenu {
static Scanner input = new Scanner(System.in);
static Scanner scan = new Scanner(System.in);
public static void addVehicleBooking()
{
String booking_ID = "";
System.out.print("Enter your booking ID");
booking_ID = input.next();
String registration = "";
System.out.print("Enter registration number");
registration = input.next();
String make_model = "";
System.out.print("Enter vehicle make/model");
make_model = input.next();
int number_passengers = 1;
System.out.print("Enter number of passengers");
number_passengers = scan.nextInt();
}
public static void main(String[] args)
{
String menu_choice = "";
while(!"X".equals(menu_choice)){
System.out.println("*** Ferry Ticketing System Menu ***");
System.out.println("A - Add Vehicle Booking");
System.out.println("B - Display Booking Info");
System.out.println("C - Update Insurance Status");
System.out.println("D - Display Booking Summary");
System.out.println("X - Exit");
System.out.print("Enter your selection: ");
menu_choice = input.next();
}
}
}
and fields "input" and "scan" must be defined as static because you are invoking them as static
The addVehicleBooking should be placed outside main. BTW: I hope that you're using somewhere the variables used for user's input because in the code posted are unused.
The refactored code should look like:
import java.util.Scanner;
public class FerryMenu {
public static void main(String[] args) {
addVehicleBooking();
}
public static void addVehicleBooking() {
String menu_choice = "";
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
String booking_ID = "";
System.out.print("Enter your booking ID");
booking_ID = input.next();
String registration = "";
System.out.print("Enter registration number");
registration = input.next();
String make_model = "";
System.out.print("Enter vehicle make/model");
make_model = input.next();
int number_passengers = 1;
System.out.print("Enter number of passengers");
number_passengers = scan.nextInt();
while (!"X".equals(menu_choice)) {
System.out.println("*** Ferry Ticketing System Menu ***");
System.out.println("A - Add Vehicle Booking");
System.out.println("B - Display Booking Info");
System.out.println("C - Update Insurance Status");
System.out.println("D - Display Booking Summary");
System.out.println("X - Exit");
System.out.print("Enter your selection: ");
menu_choice = input.next();
}
}
}

Trying to invoke a method called addAccount for a bank application but it keeps giving me .class is expected error

This has been giving me error when I try to invoke the addAccount method saying .class is expected on the line where I try to invoke it.
I am trying to do an assignment whereby I am supposed to create a method called addAccount that accepts two parameters; accountName and accountBalance and put the parameters into an accountArray
import java.util.*;
public class bank
{
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
int choice = 0;
int accountNo = 0;
double accountBal = 0;
int[] accountNoArray = new int[20];
int[] accountBalArray = new int[20];
displayMenu();
System.out.print("Please Enter Your Choice: ");
choice = sc.nextInt();
if(choice == 1)
{
System.out.print("Please Enter NRIC number: ");
accountNo = sc.nextInt();
System.out.print("Please Enter Account Balance: ");
accountBal = sc.nextInt();
}
public static void displayMenu()
{
System.out.println("Menu");
System.out.println("1. Add an account");
System.out.println("2.Search an account with the given account number");
System.out.println("3.Display accounts below the given balance");
System.out.println("4.Exit");
}
public static void addAccount(int accountNo,double accountBal)
{
}
}
You are missing a closing curly bracket before the definition of the displayMenu method. The following version is syntactically correct:
public class Bank {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice = 0;
int accountNo = 0;
double accountBal = 0;
int[] accountNoArray = new int[20];
int[] accountBalArray = new int[20];
displayMenu();
System.out.print("Please Enter Your Choice: ");
choice = sc.nextInt();
if (choice == 1) {
System.out.print("Please Enter NRIC number: ");
accountNo = sc.nextInt();
System.out.print("Please Enter Account Balance: ");
accountBal = sc.nextInt();
}
}
public static void displayMenu() {
System.out.println("Menu");
System.out.println("1. Add an account");
System.out.println("2.Search an account with the given account number");
System.out.println("3.Display accounts below the given balance");
System.out.println("4.Exit");
}
public static void addAccount(int accountNo, double accountBal) {
}
}
Do not specify parameter type while calling a method.
Here is an error:
case 1 : addAccount(int accountNo,double accountBal);
It must be:
case 1 : addAccount(accountNo,accountBal); break;
you have declared it static so you need to add the class name before the call
bank.addAccount(int accountNo,double accountBal);
and instead of the types decleration you have to call it with parameters.
bank.addAccount(accountNo,accountBal);

Categories