954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java noob seeking some guidance

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: 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

Unsated
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

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

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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");
    }
}
Unsated
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 
NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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
		}
	}
mrar85
Light Poster
34 posts since Oct 2010
Reputation Points: 10
Solved Threads: 1
 

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..

mrar85
Light Poster
34 posts since Oct 2010
Reputation Points: 10
Solved Threads: 1
 

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

Unsated
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 
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.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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


--------------------Configuration: --------------------
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: 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.

Unsated
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 
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?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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

Unsated
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Glad you were able to fix the compiler problems.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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: 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.

Unsated
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

You need to post the changed code that goes with the new set of error messages.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
/**
 * @(#)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.

Unsated
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

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

switch (getChoice)
NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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

Unsated
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Move line 8 ( the } ) to after line 30

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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");
    }
}
Unsated
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 
: 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.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: