Scanner class validation - java

I am having a bit difficulty with my code. After depositing an amount $30, it is suppose to bring back the Saving Menu
A- Deposit
B - Withdraw
C - Report
D-Return Main menu
Somehow it keeps scanning $30 instead of asking a new input.
Thanks to anyone who will try helping me out.
package Menu;
import java.util.Scanner;
enum Options {
A, B, C, D
}
public class Menu {
public Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
Menu menu = new Menu();
menu.mainMenu();
}
public void mainMenu() {
String userChoice;
boolean quit = false;
do {
System.out.println("Bank Menu" + "\nA: Savings" + "\nB: Checking"
+ "\nC: Exit");
userChoice = sc.next().toUpperCase();
switch (userChoice) {
case "A":
savingMenu savMen = new savingMenu();
break;
case "B":
break;
case "C":
quit = true;
break;
default:
System.out.println("Wrong entry.");
break;
}
System.out.println();
} while (!quit);
sc.close();
System.exit(0);
}
}
package Menu;
import Savings.Savings;
public class savingMenu extends Menu {
Savings sav = new Savings();
public savingMenu() {
String userChoice;
boolean quit = false;
/**
* A - Deposit B - Withdraw C - Report D - Return Menu
*/
do {
System.out.println("Savings Menu" + "\nA: Deposit "
+ "\nB: Withdraw" + "\nC: Report"
+ "\nD: Return to Bank Menu");
userChoice = sc.nextLine().toUpperCase();
switch (userChoice) {
case "A":
sav.makeDeposit();
break;
case "B":
sav.makeWithdraw();
break;
case "C":
sav.doMonthlyReport();
case "D":
quit = true;
super.mainMenu();
default:
System.out.println("Wrong choice.");
break;
}
System.out.println();
} while (!quit);
}
}

With Scanner check there is a next line with hasNextLine()

Ok I found the error or I should say mis-understanding.
One of my class that I deleted in the post was actually causing the havoc.
public void validateDepositAmount() {
boolean isDoubleDigit = false;
System.out.println("How much would you like to deposit?");
//try (Scanner sc = new Scanner(System.in)) {
Scanner sc = new Scanner(System.in);
do {
if (sc.hasNextDouble()) {
totalDeposit = sc.nextDouble();
if(totalDeposit >= 0) {
isDoubleDigit = true;
} else {
System.out.println("Your input must higher than 0.");
sc.nextLine();
}
} else {
System.out.println("Please put an amount.");
sc.nextLine();
}
} while (isDoubleDigit == false);
//} catch (Exception e) {
//e.printStackTrace();
//}
}
So basically I was using Try with resources and it just kept going in a loop.

Related

I need validation method in java

I need to make atm machine for school project.
I finished all and all is working fine, and i make the validation for the pin because it is string. So my problem is how to make validation for all other methods to check if anything else but numbers is entered to say the user that is wrong and to return him on the start of that method. All variables are stored into array as integers.
So here is my code please help i tried so many things and i cant make it work..
public class Banka {
static Scanner skener = new Scanner(System.in);
static String pin[] = {"1234","2345","3456","4567","5678"};
static String username[] = {"Mate","Nathan","John","Michelle","Angelina"};
static int balance[] = {200,100,250,150,300};
static boolean overdraft[] = {true,true,false,true,false};
static int index;
public static void main(String[] args) {
login();
}
public static void login() {
Date datum = new Date();
System.out.println("" +datum);
System.out.println("------------------------------");
System.out.println("Welcome to the Illuminati Bank \n Please log in with your PIN");
String login = skener.next();
checkpin(login);
for (int i = 0; i< pin.length; i++) {
if (login.matches(pin[i])) {
index = i;
System.out.println("\nWelcome " + username[index]);
Menu();
}
}
System.out.println("Wrong PIN entered, please login again \n");
login();
}
public static void Menu() {
System.out.println("Please select an option");
System.out.println("\n 1.View Bank Statement \n 2.Deposit \n 3.Withdraw \n 4.Change Pin \n 5.Exit \n");
int choice = skener.nextInt();
switch (choice) {
case 1: statement();
break;
case 2: deposit();
break;
case 3: withdraw();
break;
case 4: change();
break;
case 5: exit();
break;
default: System.out.println("Incorrect Choice ");
Menu();
}
}
public static void statement() {
switch(index) {
case 0: case 1: case 2: case 3: case 4:
System.out.println("" +username[index]+ ", your balance is: " +balance[index]+ "€");
if (overdraft[index] == true) {
System.out.println("You are entitled to overdraft");
}
else {
System.out.println("You are NOT entitled to overdraft");
}
Menu();
}
}
public static void deposit() {
System.out.println("" +username[index]+ ", how much you wish to deposit?");
int deposit = skener.nextInt();
balance[index] = balance[index] + deposit;
System.out.println("Thank you, you deposited " +deposit+ "€, now you have " +balance[index]+ "€ total");
depositm();
}
public static void depositm(){
System.out.println("\n 1.Deposit more \n 2.Exit to menu");
int more = skener.nextInt();
switch (more) {
case 1: deposit();
break;
case 2: Menu();
default: System.out.println("Wrong choice, please choose again");
depositm();
}
}
public static void withdraw() {
System.out.println("" +username[index]+ ", how much you wish to withdraw?");
int withdraw = skener.nextInt();
if (overdraft[index] == true) {
balance[index] = balance[index] - withdraw;
System.out.println("Thank you, you withdrawed the money, now you have " +balance[index]+ "€");
Menu();
}
if(overdraft[index] == false && balance[index] >= withdraw)
{balance[index] = balance[index] - withdraw;
System.out.println("Thank you, you withdrawed the money, now you have " +balance[index]+ "€");
Menu();
}
else {
System.out.println("You have insufficient funds \nPlease try again");
withdraw();
}
}
public static void change() {
System.out.println("" +username[index]+ ", do you want to change your pin?");
System.out.println("Press 1 to change or 2 to exit to menu");
int change = skener.nextInt();
switch (change) {
case 1: System.out.println("Please enter new PIN");
pin[index] = skener.next();
System.out.println("You successfully changed your PIN");
Menu();
case 2: System.out.println("Your PIN remains unchanged");
Menu();
default: System.out.println("Wrong choice, please choose again");
change();
}
}
public static void exit(){
System.out.println("Goodbye " +username[index]+ ", Illuminati Bank wish you all the best \n");
login();
}
public static int checkpin(String x){
while(!x.matches("\\d{4}")){
System.err.println("\n Error.\n Please enter 4 digit pin.");
login();
}
return 0;
}
}
so if any one can help me how to validate all other methods with user inputs where is INTs that would be great.
Try this
String input = "xxxx";
int pin;
try{
pin = Integer.parseInt(input);
}catch(NumberFormatException e){
// input contains letters or symbols.
}
Or here's another one using the Character class.
String input = "xxxx";
boolean allDigits = true;
for(char ch: input.toCharArray()){
if(!Character.isDigit(ch)) {
allDigits = false;
break;
}
}
if(allDigits){
// input contains only digits.
}
Edit: Answering this comment.
You can modify your method like this,
public static void checkpin(String x) {
if (x.length() == 4) {
try {
Integer.parseInt(x);
login();
} catch (NumberFormatException e) {
System.err.println("\n Error.\n Invalid pin.");
}
} else {
System.err.println("\n Error.\n Please enter 4 digit pin.");
}
}
this way the method login() is called only if the pin has 4 digits and all the digits are numbers.

Exception Handling not working

I made an ATM program. I have a try catch that will ask for the user to type in their pin number. The pin number must be 5 digits. So the exception will check if it is 5 digits or not but the exception handling is not working. No matter what number I type in, it always says invalid number.
Here is my code the try catch is at the top of the program and the exception handling checkNumber is at the bottom of the program..
import java.util.ArrayList;
import java.util.Scanner;
public class BankMain
{
private double availableBal = 80;
private double totalBal = 100;
private double availableBal2 = 480;
private double totalBal2 = 500;
private double availableBal3 = 80;
private double totalBal3 = 100;
ArrayList<Integer> cardNum = new ArrayList<Integer>();
static Scanner input = new Scanner(System.in);
private String error; // String the error from the exception
{
error = "error";
}
public void cardNumbers()
{
Scanner cards = new Scanner(System.in);
Scanner input = new Scanner(System.in);
Scanner keyboard = new Scanner(System.in);
try
{
System.out.println("Please select a 5 digit card number");
cardNum.add(input.nextInt());
checkNumber();
}
catch (invalidNumber err)
{
System.out.println("Caught Error: " + err.getError());
}
System.out.println("Thank you! You're card number is " + cardNum);
System.out.println("Type 'c' to go back to main menu.");
String value = keyboard.next();
if (value.equalsIgnoreCase("c"))
{
menu();
}
else if (!keyboard.equals('c'))
{
System.out.println("Invalid Entry!");
}
}
public void menu()
{
System.out.println("ATM Menu:");
System.out.println();
System.out.println("1 = Create Account");
System.out.println("2 = Account Login");
System.out.println("3 = Exit ATM");
query();
}
public void startAtm()
{
menu();
}
public void drawMainMenu()
{
AccountMain main3 = new AccountMain();
int selection;
System.out.println("\nATM main menu:");
System.out.println("1 - View account balance");
System.out.println("2 - Withdraw funds");
System.out.println("3 - Add funds");
System.out.println("4 - Back to Account Menu");
System.out.println("5 - Terminate transaction");
System.out.print("Choice: ");
selection = input.nextInt();
switch (selection)
{
case 1:
viewAccountInfo();
break;
case 2:
withdraw();
break;
case 3:
addFunds();
break;
case 4:
AccountMain.selectAccount();
break;
case 5:
System.out.println("Thank you for using this ATM!!! goodbye");
}
}
public void viewAccountInfo()
{
System.out.println("Account Information:");
System.out.println("\t--Total balance: $" + totalBal);
System.out.println("\t--Available balance: $" + availableBal);
drawMainMenu();
}
public void viewAccountInfo2()
{
System.out.println("Account Information:");
System.out.println("\t--Total balance: $" + totalBal2);
System.out.println("\t--Available balance: $" + availableBal2);
drawMainMenu();
}
public void deposit(int depAmount)
{
System.out.println("\n***Please insert your money now...***");
totalBal = totalBal + depAmount;
availableBal = availableBal + depAmount;
}
public void checkNsf(int withdrawAmount)
{
if (totalBal - withdrawAmount < 0)
System.out.println("\n***ERROR!!! Insufficient funds in you accout***");
else
{
totalBal = totalBal - withdrawAmount;
availableBal = availableBal - withdrawAmount;
System.out.println("\n***Please take your money now...***");
}
}
public void addFunds()
{
int addSelection;
System.out.println("Deposit funds:");
System.out.println("1 - $20");
System.out.println("2 - $40");
System.out.println("3 - $60");
System.out.println("4 - $100");
System.out.println("5 - Back to main menu");
System.out.print("Choice: ");
addSelection = input.nextInt();
switch (addSelection)
{
case 1:
deposit(20);
drawMainMenu();
break;
case 2:
deposit(40);
drawMainMenu();
break;
case 3:
deposit(60);
drawMainMenu();
break;
case 4:
deposit(100);
drawMainMenu();
break;
case 5:
drawMainMenu();
break;
}
}
public void withdraw()
{
int withdrawSelection;
System.out.println("Withdraw money:");
System.out.println("1 - $20");
System.out.println("2 - $40");
System.out.println("3 - $60");
System.out.println("4 - $100");
System.out.println("5 - Back to main menu");
System.out.print("Choice: ");
withdrawSelection = input.nextInt();
switch (withdrawSelection)
{
case 1:
checkNsf(20);
drawMainMenu();
break;
case 2:
checkNsf(40);
drawMainMenu();
break;
case 3:
checkNsf(60);
drawMainMenu();
break;
case 4:
checkNsf(100);
drawMainMenu();
break;
case 5:
drawMainMenu();
break;
}
}
public void query()
{
Scanner keyboard = new Scanner(System.in);
double input = keyboard.nextInt();
if (input == 2)
{
BankMainPart2 main2 = new BankMainPart2();
System.out.println("Please enter your 5 digit card number.");
BankMainPart2.loginCard(cardNum);
}
else if (input == 1)
{
cardNumbers();
}
else if (input == 3)
{
System.out.println("Thank you, have a nice day!");
System.exit(0);
}
}
public void checkingMenu()
{
AccountMain main3 = new AccountMain();
int selection;
System.out.println("\nATM main menu:");
System.out.println("1 - View account balance");
System.out.println("2 - Withdraw funds");
System.out.println("3 - Add funds");
System.out.println("4 - Back to Account Menu");
System.out.println("5 - Terminate transaction");
System.out.print("Choice: ");
selection = input.nextInt();
switch (selection)
{
case 1:
viewAccountInfo2();
break;
case 2:
withdraw();
break;
case 3:
addFunds();
break;
case 4:
AccountMain.selectAccount();
break;
case 5:
System.out.println("Thank you for using this ATM!!! goodbye");
}
}
private static void checkNumber() throws invalidNumber // run the check activation exception
{
if (String.valueOf(input).length() != 5)
{
throw new invalidNumber("invalid number");
}
else
{
System.out.println("Works!");
}
}
public static void main(String args[])
{
BankMain myAtm = new BankMain();
myAtm.startAtm();
}
}
This code snippet looks fine:
if (String.valueOf(input).length() != 5)
{
throw new invalidNumber("invalid number");
}
else
{
System.out.println("Works!");
}
As long as you don't realize that input is not the double variable declared locally somewhere:
double input = keyboard.nextInt()
instead it's an instance of java.util.Scanner (!)
static Scanner input = new Scanner(System.in)
And Scanner.toString() is certainly not the PIN you want.
Why you are having three different instances of Scanner class.. That is what making the program confusing..
In the compareNumber() method, you are actually checking the value of input, which is an instance of Scanner.. It's better to use it like this: -
checkNumber(input.nextInt())
And add the number to the list in your checkNumber(int num) method..
Of course I am not saying that it is a good way of coding.. But it will solve your problem for the time being..
Else, there are so many issues with your code..
This is your try-catch block: -
try {
System.out.println("Please select a 5 digit card number");
cardNum.add(input.nextInt());
checkNumber();
} catch (invalidNumber err) {
System.out.println("Caught Error: " + err.getError());
}
And this is your checkNumber() method : -
private static void checkNumber() throws invalidNumber
{
if (String.valueOf(input).length() != 5) {
throw new invalidNumber("invalid number");
}
else {
System.out.println("Works!");
}
}
Now you must see that you are using input as a parameter to String.valueOf(input).
But you have declared 'input` as an instance of Scanner before your try-catch block..
Scanner cards = new Scanner(System.in);
Scanner input = new Scanner(System.in);
Scanner keyboard = new Scanner(System.in);
This code is in your codeNumbers() method..
So, clearly your input can never actually contain a user input, rather a hashcode representing the object new Scanner(System.in).
So, its better that you pass the integer input from user to checkNumber() method..
************ MODIFICATION Needed in Code..
So, your checkNumber()` will be modified as: -
private static void checkNumber(int number) throws invalidNumber
{
if (String.valueOf(number).length() != 5) {
throw new invalidNumber("invalid number");
}
else {
System.out.println("Works!");
}
}
And your call to this method in try-catch block will change to this: -
try {
System.out.println("Please select a 5 digit card number");
int number = input.nextInt();
cardNum.add(number);
checkNumber(number);
} catch (invalidNumber err) {
System.out.println("Caught Error: " + err.getError());
}
Your code checks that String.valueOf(input) has a length of 5 characters. But input is not the number entered by the user. It's the object of type Scanner that is used to parse what the user enters. So the result of String.valueOf(input) is probably something like java.util.Scanner#B09876.

Exception Handling giving me trouble

I have a small program I am working on. I Just put a try catch in my code and everything seems to be working except for 1 thing. I will post my code below... As you can see my try catch statement in my code it tells the program to go down to the checkNumber method for exception handling. I keep getting an error on this part...
if (input == 5){
}
The input keeps underlining red and saying "Incompatible operand types scanner and int.
Not sure what the problem is... or how to fix it.. here is my code
import java.util.ArrayList;
import java.util.Scanner;
public class BankMain
{
private double availableBal =80;
private double totalBal =100;
private double availableBal2 =480;
private double totalBal2 =500;
private double availableBal3 =80;
private double totalBal3 =100;
ArrayList<Integer> cardNum = new ArrayList<Integer>();
static Scanner input = new Scanner(System.in);
private String error; //String the error from the exception
{
error = "error";
}
public void cardNumbers(){
Scanner cards = new Scanner(System.in);
Scanner input = new Scanner(System.in);
Scanner keyboard = new Scanner(System.in);
try{
System.out.println("Please select a 5 digit card number");
cardNum.add(input.nextInt());
checkNumber();
}
catch(invalidNumber err){
System.out.println("Caught Error: " + err.getError());
}
System.out.println("Thank you! You're card number is " +cardNum);
System.out.println("Type 'c' to go back to main menu.");
String value = keyboard.next();
if(value.equalsIgnoreCase("c")){
menu();
}
else if (!keyboard.equals('c')){
System.out.println("Invalid Entry!");
}
}
public void menu(){
System.out.println("ATM Menu:");
System.out.println();
System.out.println("1 = Create Account");
System.out.println("2 = Account Login");
System.out.println("3 = Exit ATM");
query();
}
public void startAtm()
{
menu();
}
public void drawMainMenu()
{
AccountMain main3 = new AccountMain();
int selection;
System.out.println("\nATM main menu:");
System.out.println("1 - View account balance");
System.out.println("2 - Withdraw funds");
System.out.println("3 - Add funds");
System.out.println("4 - Back to Account Menu");
System.out.println("5 - Terminate transaction");
System.out.print("Choice: ");
selection =input.nextInt();
switch(selection)
{
case 1:
viewAccountInfo();
break;
case 2:
withdraw();
break;
case 3:
addFunds();
break;
case 4:
AccountMain.selectAccount();
break;
case 5:
System.out.println("Thank you for using this ATM!!! goodbye");
}
}
public void viewAccountInfo()
{
System.out.println("Account Information:");
System.out.println("\t--Total balance: $"+totalBal);
System.out.println("\t--Available balance: $"+availableBal);
drawMainMenu();
}
public void viewAccountInfo2()
{
System.out.println("Account Information:");
System.out.println("\t--Total balance: $"+totalBal2);
System.out.println("\t--Available balance: $"+availableBal2);
drawMainMenu();
}
public void deposit(int depAmount)
{
System.out.println("\n***Please insert your money now...***");
totalBal =totalBal +depAmount;
availableBal =availableBal +depAmount;
}
public void checkNsf(int withdrawAmount)
{
if(totalBal -withdrawAmount < 0)
System.out.println("\n***ERROR!!! Insufficient funds in you accout***");
else
{
totalBal =totalBal -withdrawAmount;
availableBal =availableBal -withdrawAmount;
System.out.println("\n***Please take your money now...***");
}
}
public void addFunds()
{
int addSelection;
System.out.println("Deposit funds:");
System.out.println("1 - $20");
System.out.println("2 - $40");
System.out.println("3 - $60");
System.out.println("4 - $100");
System.out.println("5 - Back to main menu");
System.out.print("Choice: ");
addSelection =input.nextInt();
switch(addSelection)
{
case 1:
deposit(20);
drawMainMenu();
break;
case 2:
deposit(40);
drawMainMenu();
break;
case 3:
deposit(60);
drawMainMenu();
break;
case 4:
deposit(100);
drawMainMenu();
break;
case 5:
drawMainMenu();
break;
}
}
public void withdraw()
{
int withdrawSelection;
System.out.println("Withdraw money:");
System.out.println("1 - $20");
System.out.println("2 - $40");
System.out.println("3 - $60");
System.out.println("4 - $100");
System.out.println("5 - Back to main menu");
System.out.print("Choice: ");
withdrawSelection =input.nextInt();
switch(withdrawSelection)
{
case 1:
checkNsf(20);
drawMainMenu();
break;
case 2:
checkNsf(40);
drawMainMenu();
break;
case 3:
checkNsf(60);
drawMainMenu();
break;
case 4:
checkNsf(100);
drawMainMenu();
break;
case 5:
drawMainMenu();
break;
}
}
public void query(){
Scanner keyboard = new Scanner(System.in);
double input = keyboard.nextInt();
if (input == 2){
BankMainPart2 main2 = new BankMainPart2();
System.out.println("Please enter your 5 digit card number.");
BankMainPart2.loginCard(cardNum);
}
else if (input == 1){
cardNumbers();
}
else if (input == 3){
System.out.println("Thank you, have a nice day!");
System.exit(0);
}
}
public void checkingMenu()
{
AccountMain main3 = new AccountMain();
int selection;
System.out.println("\nATM main menu:");
System.out.println("1 - View account balance");
System.out.println("2 - Withdraw funds");
System.out.println("3 - Add funds");
System.out.println("4 - Back to Account Menu");
System.out.println("5 - Terminate transaction");
System.out.print("Choice: ");
selection =input.nextInt();
switch(selection)
{
case 1:
viewAccountInfo2();
break;
case 2:
withdraw();
break;
case 3:
addFunds();
break;
case 4:
AccountMain.selectAccount();
break;
case 5:
System.out.println("Thank you for using this ATM!!! goodbye");
}
}
private static void checkNumber() throws invalidNumber //run the check activation exception
{
if (input == 5)
{
System.out.println("");
}
else
throw new invalidNumber("invalid number");
}
public static void main(String args[])
{
BankMain myAtm = new BankMain();
myAtm.startAtm();
}
}
Class Bank Main 2
import java.util.ArrayList;
import java.util.Scanner;
public class BankMainPart2 {
public static void loginCard(ArrayList<Integer> cardNum){
BankMain main = new BankMain();
AccountMain main3 = new AccountMain();
Scanner logNum = new Scanner(System.in);
int loginInput = logNum.nextInt();
if (cardNum.contains(loginInput)) {
main3.selectAccount();
}
else {
System.out.println("Sorry, that pin number is incorrect!");
}
}
}
Your variable input is an object of Scanner which is declared globally and will be used for the method checknumber() and 5 is an int. Hence they are incompatible types.
The same statement works for method query() because for query you have locally defined a variable input which is of the type double.
What you should actually write is
if(input.nextInt() == 5){}

Java help for restart program

import java.util.Scanner ;
public class Mal {
public static void main (String []args) {
System.out.println("Welcome") ;
Scanner myinput=new Scanner (System.in) ;
System.out.println("Make your choice. \n 1.Check a card number \n 2.Quit.");
int choise=myinput.nextInt();
switch(choise) {
case 1:System.out.println("Enter your credit card number: ");
break;
case 2:System.out.println("Are you sure?") ;
String answer=myinput.next();
if(answer.equals("yes")){
System.out.println("Byee :)") ;
System.exit(0);
}
break;
default: System.out.println("Idiot!") ;
break;
}
}
}
i want a program which starts again if user types something different than "yes"
Here's what you can do
have a flag like this
boolean quit = false;
and a while enclosing your options
while(!quit){
...
...
}
and
set quit = true if the user wants to quit.
EDIT:
Something like this
public static void main(String a[]) {
System.out.println("Welcome");
Scanner myinput = new Scanner(System.in);
boolean quit = false;
while (!quit) {
System.out
.println("Make your choise. \n 1.Check a card number \n 2.Quit.");
int choise = myinput.nextInt();
switch (choise) {
case 1:
System.out.println("Enter your credit card number: ");
break;
case 2:
System.out.println("Are you sure?");
String answer = myinput.next();
if (answer.equals("yes")) {
System.out.println("Byee :)");
quit= true;
}
break;
default:
System.out.println("Idiot!");
break;
}
}

Default continue and if else statements

I'm using try on my code and it says illegal start of type. I'm using switch statements but default: continue; do not agree with each other I keep getting continue outside of loop. With the else statement it says illegal start type. So what can I do about try, continue, and the else statement.
public class Menu {
private Inventory database;
private char menuItem;
private Scanner input;
private char mode;
int code;
public Menu(Inventory database)
{
this.database = database;
menuItem = 'N';
input = new Scanner(System.in);
}
public Menu(MyArrayList database)
{
this.database = database;
menuItem = 'A';
input = new Scanner(System.in);
}
private void showMenu()
{
if(code == 'A'){
System.out.println();
System.out.println("------------------");
System.out.println("Display Movies : D");
System.out.println("Add Movie : A");
System.out.println("Delete Movie : X");
System.out.println("Select Mode : M");
System.out.println("Exit : E");
System.out.println("------------------");
System.out.println();
System.out.print("Please make your selection: ");
}
else
{
System.out.println();
System.out.println("------------------");
System.out.println("Display Movies : D");
System.out.println("Rent a Movie : R");
System.out.println("Reserve a Movie: S");
System.out.println("Select Mode : M");
System.out.println("Exit : E");
System.out.println("------------------");
System.out.println();
System.out.print("Please make your selection: ");
}
}
private void rentMovie(int productID)
{
int index = database.getIndex(productID);
if( index == -1)
{
System.out.println("There is not such a code.");
}
else
{
if( database.getMovie(index).getIsRented())
{
System.out.println("You cannot rent " + database.getMovie(index).getTitle() + ". It is already rented.");
}
else
{
database.getMovie(index).setIsRented(true);
System.out.println("Please take your movie.");
}
}
}
private void reserveMovie(int productID)
{
int index = database.getIndex(productID);
if( index == -1)
{
System.out.println("There is not such a code.");
}
else
{
if( database.getMovie(index).getIsReserved() )
{
System.out.println("You cannot reserve " + database.getMovie(index).getTitle() + ". It is already reserved.");
}
else
{
if( database.getMovie(index).getIsRented())
{
database.getMovie(index).setIsReserved(true);
System.out.println( database.getMovie(index).getTitle() + " is reserved for you." );
}
else
{
System.out.println( database.getMovie(index).getTitle() + " is available. You can rent it if you like.");
}
}
}
}
try{
if(mode == 'A'){
switch(menuItem){
case 'N':
break;
case 'D':
database.print();
showMenu();
menuItem = input.next().charAt(0);
break;
case 'A':
String title;
System.out.println("Enter movie title, then press enter");//movie title,
title= input.nextLine();
System.out.println("Enter movie code, then press enter");//enter movie code,then press enter
code = Integer.parseInt(input.nextLine());
addMovie(title,code);
menuItem ='N';
break;
case 'X':
System.out.println("");
deleteMovie(code);
menuItem ='N';
break;
case 'M':
selectMode();
menuItem = 'N';
case 'E':
System.out.print("Program terminated.");
System.exit(0);
break;
default:
continue;
}
}
}
else
{
public void run(){
int code;
while(true)
{
switch(menuItem)
{
case 'N':
break;
case 'D':
database.print();
showMenu();
menuItem = input.next().charAt(0);
break;
case 'R':
System.out.print("Please enter product code:");
rentMovie( input.nextInt() );
showMenu();
menuItem = input.next().charAt(0);
break;
case 'S':
System.out.print("Please enter product code:");
reserveMovie( input.nextInt() );
showMenu();
menuItem = input.next().charAt(0);
break;
case 'E':
System.out.print("Program terminated.");
System.exit(0);
break;
default :
showMenu();
menuItem = input.next().charAt(0);
}
}
}
}
You've got else following your try block, and that makes no sense. no, just bad indentation.
Where's your catch block, or finally block? It makes no sense to just have try. Also, you can't just declare a function in the middle of that else block.
Basically, I recommend you re-study the "Java Syntax" chapter of whatever guide you're using, because this code is just all kinds of wrong.
A.. Few pointers..
First of all, your Try doesn't have a catch.
Your case statements lack {} blocks.
And you can't create a method within an else block.
Further to answer your Question: Illegal start of type means you haven't initiated the variable. For example "menuItem"
Edit: To further that, default should be break; not continue;
Edit2: And further your second switch contains a boolean as argument...
With your edit, now showing the issue.
Your Try starts outside of a method body.
reserveMovie is closed just before try starts, and as such is not valid.

Categories