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

    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(EmuBank.java:31)

    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
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    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.

    db
    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
    Location
    Islamabad, Pakistan
    Posts
    25
    Rep Power
    0

    Default Re: Java beginner, trouble with "switch"

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

    Regards
    Jamil

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
  •