Results 1 to 3 of 3
- 03-11-2013, 05:15 AM #1Member
- Join Date
- Mar 2013
- Rep Power
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)
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.
- 03-11-2013, 05:40 AM #2
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.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 03-11-2013, 11:50 AM #3Member
- Join Date
- Feb 2013
- Islamabad, Pakistan
- Rep Power
- By klspepper in forum New To JavaReplies: 0Last Post: 12-07-2012, 09:29 AM
- By Googol in forum New To JavaReplies: 3Last Post: 10-30-2012, 04:06 PM
- By Vermont in forum New To JavaReplies: 5Last Post: 12-21-2011, 07:44 PM
- By finalstrike777 in forum New To JavaReplies: 2Last Post: 07-12-2011, 01:41 PM
- By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 08:35 AM