Results 1 to 3 of 3
  1. #1
    cupofdoug is offline Member
    Join Date
    Mar 2013
    Rep Power

    Default Java beginner, trouble with "switch"

    Hey thanks for reading. I'll try to make this brief, I'm having trouble using the switch command.

    I'm supposed to write a program that replicates the use of an ATM. There needs to be commands for viewing balance, deposit, withdraw, transfer funds, and exit.

    I'm using the "switch" command to determine what function the user wants to perform, all commands are under a "while" command, and every switch except for the "Press X to exit" switch satisfies the while command, so the loop goes back to the main menu, displays the options and prompts the user for their selection.

    For the deposit and withdrawal commands everything seems to work fine until the loop repeats, and brings the user back to the main menu (back to the switch) for option selection. Where the user would normally put in their selection from the menu options, it doesn't prompt the user for their selection, and instead proceeds to try to read the input the user hasn't even put in. I get an error reading:

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at emubank.EmuBank.main(

    Line 31 is as follows:

    char trans = command.charAt(0);

    The View Balance function works fine. It loops back to the main menu, and properly prompts the user for their next selection.

    I hope I've made this clear enough to understand, if additional information is required I will be checking back frequently. Thanks again.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: Java beginner, trouble with "switch"

    First, a nitpick. Java doesn't have commands; Java has statements.

    Are you using a Scanner? Search this forum using the keywords "consume newline" and you may find a solution.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Jamil37037 is offline Member
    Join Date
    Feb 2013
    Islamabad, Pakistan
    Rep Power

    Default Re: Java beginner, trouble with "switch"

    Please show your code so we can understand it in a better way.


Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  4. "Java Programming for the Absolute Beginner"
    By finalstrike777 in forum New To Java
    Replies: 2
    Last Post: 07-12-2011, 12:41 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts