Working on a simple banking program and it doesn't compile. Worked perfectly when everything was in main, then i was told to divide into methods( where everything went wrong ).
The errors im getting are:

C:\Users\DJ3\Desktop\School\Minilab5.java:30: illegal start of expression
}
^
C:\Users\DJ3\Desktop\School\Minilab5.java:67: illegal start of type
switch (userChoice)
^
C:\Users\DJ3\Desktop\School\Minilab5.java:67: <identifier> expected
switch (userChoice)
^
C:\Users\DJ3\Desktop\School\Minilab5.java:69: orphaned case
case 1:
^
4 errors

Process completed.

import java.util.Scanner;

public class Minilab5
{

public static void main (String[] args)
{

  Minilab5 banker = new Minilab5 ();
  banker.dispatcher ();
}

void dispatcher ()
{

  Scanner in = new Scanner(System.in);
  int userChoice;
  boolean quit = false;
  float balance = 0f;
  do
}
{
  	 System.out.println("1. Deposit money");
   	 System.out.println("2. Withdraw money");
   	 System.out.println("3. Check balance");
  	 System.out.print("0. To quit");
   	 userChoice = in.nextInt();

}
  public static void deposit()
{
  	float amount;
            System.out.print("Amount to deposit: ");
            amount = in.nextFloat();

            if (amount <= 0)
       System.out.println("Cannot deposit negative amounts.");

         else
       {
         balance += amount;
        System.out.println("$" + amount + " has been deposited.");
       }
}

public static void withdraw()
{
System.out.print("Amount to withdraw: ");
                    amount = in.nextFloat();
        if (amount <= 0 || amount > balance)
             System.out.println("Insufficient Funds.");
        else
         {
          balance -= amount;
          System.out.println("$" + amount + " has been withdrawn.");
         }
}
   switch (userChoice)
{
            case 1:
        deposit();
            break;

            case 2:
        withdraw();
            break;

            case 3:
            System.out.println("Your balance: $" + balance);
            break;

            case 0:
            quit = true;
            break;

            default:
            System.out.println("Incorrect choice.");
            break;
}
	{

        while (!quit);
      System.out.println("Have a nice day");
    }
}

Im sure the issue is with the braces but i cant seem to find it

Your { and } are messed up. Format your code properly, with correct indenting of all the {} and it should be clearer.
The line numbers in the error messages don't match the code you posted, so it's hard to understand, but

do
}

is probably a mistake

oh sorry about the lines i didn't add the top part

/**
 * @(#)Minilab5.java
 *
 * Revised Bank application
 *
 * @author 
 * @version 1.00 2011/11/16
 */

import java.util.Scanner;

public class Minilab5
{

public static void main (String[] args)
{

  Minilab5 banker = new Minilab5 ();
  banker.dispatcher ();
}

void dispatcher ()
{

  Scanner in = new Scanner(System.in);
  int userChoice;
  boolean quit = false;
  float balance = 0f;
  do
{
  	 System.out.println("1. Deposit money");
   	 System.out.println("2. Withdraw money");
   	 System.out.println("3. Check balance");
  	 System.out.print("0. To quit");
   	 userChoice = in.nextInt();

}
  public static void deposit()
{
  	float amount;
            System.out.print("Amount to deposit: ");
            amount = in.nextFloat();

            if (amount <= 0)
       System.out.println("Cannot deposit negative amounts.");

         else
       {
         balance += amount;
        System.out.println("$" + amount + " has been deposited.");
       }
}

public static void withdraw()
{
System.out.print("Amount to withdraw: ");
                    amount = in.nextFloat();
        if (amount <= 0 || amount > balance)
             System.out.println("Insufficient Funds.");
        else
         {
          balance -= amount;
          System.out.println("$" + amount + " has been withdrawn.");
         }
}
   switch (userChoice)
{
            case 1:
        deposit();
            break;

            case 2:
        withdraw();
            break;

            case 3:
            System.out.println("Your balance: $" + balance);
            break;

            case 0:
            quit = true;
            break;

            default:
            System.out.println("Incorrect choice.");
            break;
}
	{

        while (!quit);
      System.out.println("Have a nice day");
    }
}

see this tmeplate.....it helps you to see which is the main method, method and which is the class....

public class Minilab5 
{
//main
public static void main (String[] args){
	...code goes here
}
	
//method
void dispatcher (){
...code goes here

}
//another method
public static void deposit(){
	...code goes here
	}

//another method
public static void withdraw(){
...code goes here
		}
	}

Edited 5 Years Ago by mrar85: n/a

and it seems to me you are trying to define method within a method:

public static void main (String[] args){

}
void dispatcher(){

do{
    public static void deposit(){}
    public static void deposit(){}
}while()
}

which you can't..

Edited 5 Years Ago by mrar85: n/a

Thank you for your reply but im still confused =(
Someone on another forum helped me with this and i have

/**
 * @(#)Minilab5.java
 *
 * Revised Bank application
 *
 * @author
 * @version 1.00 2011/11/16
 */

import javax.swing.*;

public class Minilab5
{

	public static void main (String[] args)
	{

  Minilab5 banker = new Minilab5 ();
  banker.dispatcher ();
	}

void dispatcher ()
{
  int userChoice;
  boolean quit = false;
  float balance = 0f;
}
public static void getChoice ()
{
	String response;

  	 response = JOptionPane.showUnput Dialog ( "1 = Deposit, 2 = Withdraw, 3 = Balance" );
   	 userChoice = in.nextInt();

}
  public static void deposit()
{
  	float amount;
            JOptionPane.showInputDialog("Amount to deposit: ");
            amount = in.nextFloat();

            if (amount <= 0)
       		JOptionPane.showMessageDialog("Cannot deposit negative amounts.");

         else
       {
         balance += amount;
        	JOptionPane.showMessageDialog("$" + amount + " has been deposited.");
       }
}

public static void withdraw()
{
			JOptionPane.showInputDialog("Amount to withdraw: ");
                    amount = in.nextFloat();
        if (amount <= 0 || amount > balance)
             JOptionPane.showMessageDialog("Insufficient Funds.");
        else
         {
          balance -= amount;
          	JOptionPane.showMessageDialog("$" + amount + " has been withdrawn.");
         }
}
   switch (userChoice)
{
            case 1:
        deposit();
            break;

            case 2:
        withdraw();
            break;

            case 3:
            JOptionPane.showMessageDialog("Your balance: $" + balance);
            break;

            case 0:
            quit = true;
            break;

            default:
            JOptionPane.showMessageDialog("Incorrect choice.");
            break;
}
	{

        while (!quit);
      JOptionPane.showMessageDialog("Have a nice day");
    }
}

but this isnt compiling either, sorry im sure its hard explaining things to someone who doesnt really know the terms but this is like a foriegn language for me, only been coding for like a month

this isnt compiling either

You'll speed things up if you post the full text of the error messages you get.
Or you can wait for some one to ask you to post them.

One problem I see in your posted code is the location of the {}s.
Every new set of {} within an existing set of {} should be indented 3-4 spaces so you can scan the code and see what code is inside of what pairs of {}. The code within those inner {} should again be indented 3-4 spaces.

This saves a lot of time when reading code if the {} are properly aligned. With sloppily aligned {}s you have to continually scan back and forth to see what code is related to what.

Edited 5 Years Ago by NormR1: n/a

im sorry about not posting the errors that was stupid of me:


--------------------Configuration: <Default>--------------------
C:\Users\DJ3\Desktop\School\Minilab5.java:32: ';' expected
response = JOptionPane.showUnput Dialog ( "1 = Deposit, 2 = Withdraw, 3 = Balance" );
^
C:\Users\DJ3\Desktop\School\Minilab5.java:64: illegal start of type
switch (userChoice)
^
C:\Users\DJ3\Desktop\School\Minilab5.java:64: <identifier> expected
switch (userChoice)
^
C:\Users\DJ3\Desktop\School\Minilab5.java:66: orphaned case
case 1:
^
4 errors

If these errors are because of the curly braces then im at a loss cause i barely know where they go.

Edited 5 Years Ago by Unsated: {}s

JOptionPane.showUnput Dialog

What is this supposed to be?

C:\Users\DJ3\Desktop\School\Minilab5.java:64: illegal start of type
switch (userChoice)
^

What method is this statement in?

Edited 5 Years Ago by NormR1: n/a

Thanks for pointing that out its suppose to be showInputDialog, and that statement must have been from the beginning version of the code and i probably took it out.

oh sorry that userChoice was changed to getChoice

Edited 5 Years Ago by Unsated: n/a

lol all thanks to you =P i still have 3 more though but your help is greatly appreciated.


C:\Users\DJ3\Desktop\School\Minilab5.java:64: illegal start of type
switch (getChoice)
^
C:\Users\DJ3\Desktop\School\Minilab5.java:64: <identifier> expected
switch (getChoice)
^
C:\Users\DJ3\Desktop\School\Minilab5.java:66: orphaned case
case 1:
^
3 errors

JCreator is telling me that

switch (getChoice)

is an illegal start of type and that an identifier is expected??

Also

case 1:

is an orphaned case??

i just tried running the program and got

java.lang.NoClassDefFoundError: Minilab5
Exception in thread "main"
Process completed.

Edited 5 Years Ago by Unsated: n/a

/**
 * @(#)Minilab5.java
 *
 * Revised Bank application
 *
 * @author
 * @version 1.00 2011/11/16
 */

import javax.swing.*;

public class Minilab5
{

	public static void main (String[] args)
	{

  Minilab5 banker = new Minilab5 ();
  banker.dispatcher ();
	}

void dispatcher ()
{
  int userChoice;
  boolean quit = false;
  float balance = 0f;
}
public static void getChoice ()
{
	String response;

  	 response = JOptionPane.showInputDialog ( "1 = Deposit, 2 = Withdraw, 3 = Balance" );
   	 userChoice = in.nextInt();

}
  public static void deposit()
{
  	float amount;
            JOptionPane.showInputDialog("Amount to deposit: ");
            amount = in.nextFloat();

            if (amount <= 0)
       		JOptionPane.showMessageDialog("Cannot deposit negative amounts.");

         else
       {
         balance += amount;
        	JOptionPane.showMessageDialog("$" + amount + " has been deposited.");
       }
}

public static void withdraw()
{
			JOptionPane.showInputDialog("Amount to withdraw: ");
                    amount = in.nextFloat();
        if (amount <= 0 || amount > balance)
             JOptionPane.showMessageDialog("Insufficient Funds.");
        else
         {
          balance -= amount;
          	JOptionPane.showMessageDialog("$" + amount + " has been withdrawn.");
         }
}
   switch (getChoice)
{
            case 1:
        deposit();
            break;

            case 2:
        withdraw();
            break;

            case 3:
            JOptionPane.showMessageDialog("Your balance: $" + balance);
            break;

            case 0:
            quit = true;
            break;

            default:
            JOptionPane.showMessageDialog("Incorrect choice.");
            break;
}
	{

        while (!quit);
      JOptionPane.showMessageDialog("Have a nice day");
    }
}

Nothing has really changed its just that before i was just compiling it and this time i just tried running it.

As I asked before and you missed or ignored,
What method is this code in? This statment must be in a method.

switch (getChoice)

im sorry i guess i missed that but i just realized what you said and slapped myself when i saw that... is there a way to put the switch statement into the getchoice statement like this?

public static void getChoice ()
{
	String response;

  	 response = JOptionPane.showInputDialog ( "1 = Deposit, 2 = Withdraw, 3 = Balance" );
   	 userChoice = in.nextInt();

}
  switch (getChoice)
{
            case 1:
        deposit();
            break;

            case 2:
        withdraw();
            break;

            case 3:
            JOptionPane.showMessageDialog("Your balance: $" + balance);
            break;

            case 0:
            quit = true;
            break;

            default:
            JOptionPane.showMessageDialog("Incorrect choice.");
            break;
}

also i just tried running it ignoring the errors and instead of joptionpane windows it just wrote it all in println

ok i did that and got 24 errors


C:\Users\DJ3\Documents\MiniLab5.java:12: class Minilab5 is public, should be declared in a file named Minilab5.java
public class Minilab5
^
C:\Users\DJ3\Documents\MiniLab5.java:33: cannot find symbol
symbol : variable in
location: class Minilab5
amount = in.nextFloat();
^
C:\Users\DJ3\Documents\MiniLab5.java:36: cannot find symbol
symbol : method showMessageDialog(java.lang.String)
location: class javax.swing.JOptionPane
JOptionPane.showMessageDialog("Cannot deposit negative amounts.");
^
C:\Users\DJ3\Documents\MiniLab5.java:40: cannot find symbol
symbol : variable balance
location: class Minilab5
balance += amount;
^
C:\Users\DJ3\Documents\MiniLab5.java:41: cannot find symbol
symbol : method showMessageDialog(java.lang.String)
location: class javax.swing.JOptionPane
JOptionPane.showMessageDialog("$" + amount + " has been deposited.");
^
C:\Users\DJ3\Documents\MiniLab5.java:48: cannot find symbol
symbol : variable amount
location: class Minilab5
amount = in.nextFloat();
^
C:\Users\DJ3\Documents\MiniLab5.java:48: cannot find symbol
symbol : variable in
location: class Minilab5
amount = in.nextFloat();
^
C:\Users\DJ3\Documents\MiniLab5.java:49: cannot find symbol
symbol : variable amount
location: class Minilab5
if (amount <= 0 || amount > balance)
^
C:\Users\DJ3\Documents\MiniLab5.java:49: cannot find symbol
symbol : variable amount
location: class Minilab5
if (amount <= 0 || amount > balance)
^
C:\Users\DJ3\Documents\MiniLab5.java:49: cannot find symbol
symbol : variable balance
location: class Minilab5
if (amount <= 0 || amount > balance)
^
C:\Users\DJ3\Documents\MiniLab5.java:50: cannot find symbol
symbol : method showMessageDialog(java.lang.String)
location: class javax.swing.JOptionPane
JOptionPane.showMessageDialog("Insufficient Funds.");
^
C:\Users\DJ3\Documents\MiniLab5.java:53: cannot find symbol
symbol : variable balance
location: class Minilab5
balance -= amount;
^
C:\Users\DJ3\Documents\MiniLab5.java:53: cannot find symbol
symbol : variable amount
location: class Minilab5
balance -= amount;
^
C:\Users\DJ3\Documents\MiniLab5.java:54: cannot find symbol
symbol : variable amount
location: class Minilab5
JOptionPane.showMessageDialog("$" + amount + " has been withdrawn.");
^
C:\Users\DJ3\Documents\MiniLab5.java:54: cannot find symbol
symbol : method showMessageDialog(java.lang.String)
location: class javax.swing.JOptionPane
JOptionPane.showMessageDialog("$" + amount + " has been withdrawn.");
^
C:\Users\DJ3\Documents\MiniLab5.java:62: cannot find symbol
symbol : variable userChoice
location: class Minilab5
userChoice = in.nextInt();
^
C:\Users\DJ3\Documents\MiniLab5.java:62: cannot find symbol
symbol : variable in
location: class Minilab5
userChoice = in.nextInt();
^
C:\Users\DJ3\Documents\MiniLab5.java:65: cannot find symbol
symbol : variable getChoice
location: class Minilab5
switch (getChoice)
^
C:\Users\DJ3\Documents\MiniLab5.java:76: cannot find symbol
symbol : variable balance
location: class Minilab5
JOptionPane.showMessageDialog("Your balance: $" + balance);
^
C:\Users\DJ3\Documents\MiniLab5.java:76: cannot find symbol
symbol : method showMessageDialog(java.lang.String)
location: class javax.swing.JOptionPane
JOptionPane.showMessageDialog("Your balance: $" + balance);
^
C:\Users\DJ3\Documents\MiniLab5.java:80: cannot find symbol
symbol : variable quit
location: class Minilab5
quit = true;
^
C:\Users\DJ3\Documents\MiniLab5.java:84: cannot find symbol
symbol : method showMessageDialog(java.lang.String)
location: class javax.swing.JOptionPane
JOptionPane.showMessageDialog("Incorrect choice.");
^
C:\Users\DJ3\Documents\MiniLab5.java:90: cannot find symbol
symbol : variable quit
location: class Minilab5
while (!quit);
^
C:\Users\DJ3\Documents\MiniLab5.java:91: cannot find symbol
symbol : method showMessageDialog(java.lang.String)
location: class javax.swing.JOptionPane
JOptionPane.showMessageDialog("Have a nice day");
^
24 errors

Process completed.

The new code looks like this

/**
 * @(#)Minilab5.java
 *
 * Revised Bank application
 *
 * @author
 * @version 1.00 2011/11/16
 */

import javax.swing.*;

public class Minilab5
{

	public static void main (String[] args)
	{

  Minilab5 banker = new Minilab5 ();
  banker.dispatcher ();
	}

void dispatcher ()
{
  int userChoice;
  boolean quit = false;
  float balance = 0f;
}

  public static void deposit()
{
  	float amount;
            JOptionPane.showInputDialog("Amount to deposit: ");
            amount = in.nextFloat();

            if (amount <= 0)
       		JOptionPane.showMessageDialog("Cannot deposit negative amounts.");

         else
       {
         balance += amount;
        	JOptionPane.showMessageDialog("$" + amount + " has been deposited.");
       }
}

public static void withdraw()
{
			JOptionPane.showInputDialog("Amount to withdraw: ");
                    amount = in.nextFloat();
        if (amount <= 0 || amount > balance)
             JOptionPane.showMessageDialog("Insufficient Funds.");
        else
         {
          balance -= amount;
          	JOptionPane.showMessageDialog("$" + amount + " has been withdrawn.");
         }
}
public static void getChoice ()
{
	String response;

  	 response = JOptionPane.showInputDialog ( "1 = Deposit, 2 = Withdraw, 3 = Balance" );
   	 userChoice = in.nextInt();


  switch (getChoice)
{
            case 1:
        deposit();
            break;

            case 2:
        withdraw();
            break;

            case 3:
            JOptionPane.showMessageDialog("Your balance: $" + balance);
            break;

            case 0:
            quit = true;
            break;

            default:
            JOptionPane.showMessageDialog("Incorrect choice.");
            break;
}
}
	{

        while (!quit);
      JOptionPane.showMessageDialog("Have a nice day");
    }
}

: cannot find symbol

This error means the the compiler can not find the definition for a variable you have used in the statement where the error occurred.
You need to write a definition for that variable.

it sounded basic as i said it to myself >.> but ive only been coding for about a month, trying to catch up with the class but not being exempt from the work its hard try to learn all this stuff so fast.

I suggest that you delete most of what you have and start over. Add a few lines to the new version and compile it. Fix the errors and compile it. When there are no more errors, add a few more lines and do it again.

Start with this and add to it:

import javax.swing.*;

public class Minilab5
{

	public static void main (String[] args)
	{

          Minilab5 banker = new Minilab5 ();

	}  // end main()
} // end class

Like to thank everyone who helped me in this thread, after listening to your advice and readin material given to me ive successfully completed the assignment.

import javax.swing.JOptionPane;
class Practice6_1
{
public static double newBalance=0;
public static double balance=0;
public static double adjustment=0;
public static String response;
public static String moreBankingBusiness;

public static void main(String[] args)
{
try
{
moreBankingBusiness = JOptionPane.showInputDialog("do you want to do some banking?");
moreBankingBusiness=moreBankingBusiness.toUpperCase();
}catch (Exception e)
{
// Print out the exception that occurred
System.out.println("Invalid Button Press - "+e.getMessage());
System.exit(0);
}

while(moreBankingBusiness.equals("YES"))
{
try
{
response=JOptionPane.showInputDialog
("What would you want to do ?\n1.Deposit\n2.Withdraw\n3.GetBalance");
if(response.equals(""))
{
JOptionPane.showMessageDialog
(null,"You nust make an entry in the InputBox.");
System.exit(0);
}

if(response==null)
{
JOptionPane.showMessageDialog(null,"You clicled on the Cancel button.");
System.exit(0);
}
int ch = Integer.parseInt(response);
switch (ch)
{
case 1:
deposit();
break;
case 2:
withdrawal();
break;
case 3:
balance();
break;

default :
JOptionPane.showMessageDialog(null,response+"is not vaild banking function");
break;
}
balance=newBalance;
moreBankingBusiness=JOptionPane.showInputDialog("Do you have more banking business?");
moreBankingBusiness=moreBankingBusiness.toUpperCase();
}catch (Exception e)
{
// Print out the exception that occurred
System.out.println("Invalid type of Input or Button Pressed - "+e.getMessage());
System.exit(0);
}
}//end of while

}

public static void deposit()
{
try
{
adjustment=Double.parseDouble(JOptionPane.showInputDialog("Enter the Deposit Amount"));
newBalance=balance+adjustment;
JOptionPane.showMessageDialog(null,"***SMILEY NATIONAL BANK***\n\n"+
"Old Balance is:"+balance+"\n"+"Adjustment is :"+adjustment+"\n"+
"New Balance is:"+newBalance+"\n");
}catch (Exception e)
{
// Print out the exception that occurred
System.out.println("Invalid operation - "+e.getMessage());
System.exit(0);
}
}

public static void withdrawal()
{
try
{
adjustment=Double.parseDouble(JOptionPane.showInputDialog("Enter the Withdrawal Amount"));
if (balance-adjustment>=0)
{
newBalance=balance-adjustment;
JOptionPane.showMessageDialog
(null,"***SMILEY NATIONAL BANK***\n\n"+
"Old Balance is:"+balance+"\n"+
"Adjustment is :"+adjustment+"\n"+
"New Balance is:"+newBalance+"\n");
}
else
JOptionPane.showMessageDialog
(null,"***SMILEY NATIONAL BANK***\n\n"+
"Insufficient funds to withdraw.");
}catch (Exception e)
{
// Print out the exception that occurred
System.out.println("Invalid operation - "+e.getMessage());
System.exit(0);
}

}

public static void balance()
{
JOptionPane.showMessageDialog(null,"***SMILEY NATIONAL BANK***\n\n"+"Your Current Banace is:"+balance);
}
}
This question has already been answered. Start a new discussion instead.