Java - Trouble with brackets - java

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

Related

Is it possible detect single enterkey with java.util.Scanner?

I am trying to write a terminal-based toy app, which allows user to input product category and inventory.
Is it possible to implement a feature of pressing enter key to input the default inventory.
Here is the procedure/steps
app print "product category:"
user input a category, such as shoe
app print "Inventory(press enter key for 999):"
user press enterkey or input another number
app print product_category + product_inventory
here is my code
import java.util.Scanner;
public class ProductScanner {
public static void main(String[] args) {
System.out.print("product category: ");
Scanner scanner = new Scanner(System.in);
String product_category = scanner.next();
System.out.print("Inventory(press enter key for 999): ");
int product_inventory = scanner.nextInt();
scanner.close();
System.out.println(String.format("%s, %d", product_category, product_inventory));
}
}
this code does not support "enterkey for default" feature.
quesion
is it possible detect single enterkey with java.util.Scanner to implement the default input?
I also tried this code, even worse
import java.util.Scanner;
public class ProductScanner {
public static void main(String[] args) {
System.out.print("product category: ");
Scanner scanner = new Scanner(System.in);
String product_category = scanner.next();
scanner.close();
System.out.print("Inventory(press enter key for 999): ");
scanner = new Scanner(System.in);
String product_inventory_str = "999";
if(scanner.hasNext()){
System.out.println("hasNext");
product_inventory_str = scanner.nextLine();
}
else{
System.out.println("does not have Next");
}
int product_inventory = 999;
if(product_inventory_str.isEmpty()){
System.out.println("isEmpty");
}
else{
product_inventory = Integer.parseInt(product_inventory_str);
}
scanner.close();
System.out.println(String.format("%s, %d", product_category, product_inventory));
}
}
You could always read an entire line (because user will have to press Enter anyway) and then decide what to do with it, something like this:
public static void main(String[] args) {
System.out.print("product category: ");
Scanner scanner = new Scanner(System.in);
String product_category = scanner.nextLine();
System.out.print("Inventory(press enter key for 999): ");
String pi_string = scanner.nextLine();
int product_inventory = pi_string.isEmpty()?
999:Integer.parseInt(pi_string);
scanner.close();
System.out.println(String.format("%s, %d",
product_category, product_inventory));
}

Why are we not able to give input to the string s1 in the following code?

import java.util.*;
public class MovieInfo
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the name of the film");
String name = in.nextLine();
System.out.println("Enter the film budget");
int bud = in.nextInt();
System.out.println("Enter the running time");
double time = in.nextDouble();
System.out.println("Enter the film production house");
String s1 = in.nextLine();
System.out.println("Film being screened : "+name);
System.out.println("Budget "+bud+" cores");
System.out.println("Running time "+time+" hours");
System.out.println("Production : "+s1);
}
}
In the output of the above program, it does not take input for String s1.
How do I fix it?
Any help would be appreciated.
Try to put the following additional line:
in.nextLine();
after that line:
double time = in.nextDouble();
to consume new line character (it's created when you accept input double with Enter key) that is not being consumed with .nextDouble() call.
Please refer to link to that question, provided by LenosV, to get detailed information on what's the reason of that behavior of Scanner.
This will work
import java.util.*;
public class MovieInfo
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the name of the film");
String name = in.nextLine();
System.out.println("Enter the film budget");
int bud = in.nextInt();
System.out.println("Enter the running time");
double time = in.nextDouble();
System.out.println("Enter the film production house");
String s1 = in.next();
System.out.println("Film being screened : "+name);
System.out.println("Budget "+bud+" cores");
System.out.println("Running time "+time+" hours");
System.out.println("Production : "+s1);
}
}

How do I loop my main method in a way?

Well for instance, the program will ask the user for input, then it will produce some output then it's done, but how can I make it loop again if the user wants to through the main method?
Here's my code:
public static void main(String[] args) {
Scanner sc = new Scanner(System. in );
System.out.print("Please enter the first name of the person you would love to know about : ");
String hisName = sc.next();
printSomeInfoAbout(hisName);
}
How will I make it run again if the user decides to again?
public static void main(String[] args) {
Scanner sc = new Scanner(System. in );
System.out.print("Please enter the first name of the person you would love to know about : ");
String hisName = sc.next();
printSomeInfoAbout(hisName);
System.out.print("AGAIN (Y/N) : "); // ask the input from user
String var= sc.next();
if(var.equalsIgnoreCase("Y")){// Matches "Y" or "y"
main(null); // if input is Y then call main again.
}
}
String x=null;
Scanner sc = new Scanner(System. in );
String hisName=null;
do{
System.out.print("Please enter the first name of the person you would love to know about : ");
hisName = sc.next();
printSomeInfoAbout(hisName);
System.out.print("y/n");
x=sc.next();
}while(x.equals("y"));
boolean exit = false;
while(!exit){
Scanner sc = new Scanner(System. in );
System.out.print("Please enter 'exit' to exit or, the first name of the person you would love to know about : ");
String hisName = sc.next();
if(!"exit".equals(hisName)) {
printSomeInfoAbout(hisName);
}else{
exit = true;
}
}
create a method for getting the input and call on it if the user chooses to input another name
A simple loop that will check whatever the user put an empty string as the first name it will exit.
public static void main(String[] args) {
Scanner sc = new Scanner(System. in );
do {
System.out.print("Please enter the first name of the person you would love to know about : ");
String hisName = sc.next();
if (hisName.equals("")) {
printSomeInfoAbout(hisName);
}
} while (!hisName.equals(""));
}
Please find below code segment, It might help you..
public static void main(String[] args) {
String name;
Scanner scn = new Scanner(System.in);
boolean flag = false;
do {
System.out.println("Please enter the first name of the person you would love to know about : ");
name = scn.next();
System.out.println("Your friend " +name+ " is a great guy..!");
System.out.println();
System.out.println("Enter 1 to continue giving other" +
" names or Enter 2. to quit");
System.out.println();
int choice = scn.nextInt();
if( choice == 1 ) {
flag = true;
} else {
System.out.println("ThankU.. Bye");
flag = false;
}
} while(flag);
}

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

Java .class is expected [duplicate]

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