stringindexoutofbounds with currency converter java program
Posted
by
user1795926
on Stack Overflow
See other posts from Stack Overflow
or by user1795926
Published on 2012-11-03T04:36:22Z
Indexed on
2012/11/03
5:00 UTC
Read the original article
Hit count: 142
java
I am have trouble with a summary not showing up. I am supposed to modify a previous Java assignment by by adding an array of objects. Within the loop, instantiate each individual object. Make sure the user cannot keep adding another Foreign conversion beyond your array size. After the user selects quit from the menu, prompt if the user want to display a summary report. If they select ‘Y’ then, using your array of objects, display the following report:
Item Conversion Dollars Amount
1 Japanese Yen 100.00 32,000.00
2 Mexican Peso 400.00 56,000.00
3 Canadian Dollar 100.00 156.00
etc.
Number of Conversions = 3
There are no errors when I compile..but when I run the program it is fine until I hit 0 to end the conversion and have it ask if i want to see a summary. This error displays:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:658) at Lab8.main(Lab8.java:43)
my code:
import java.util.Scanner; import java.text.DecimalFormat; public class Lab8 { public static void main(String[] args) { final int Max = 10; String a; char summary; int c = 0; Foreign[] Exchange = new Foreign[Max]; Scanner Keyboard = new Scanner(System.in); Foreign.opening(); do { Exchange[c] = new Foreign(); Exchange[c].getchoice(); Exchange[c].dollars(); Exchange[c].amount(); Exchange[c].vertical(); System.out.println("\n" + Exchange[c]); c++; System.out.println("\n" + "Please select 1 through 4, or 0 to quit" + >"\n"); c= Keyboard.nextInt(); } while (c != 0); System.out.print("\nWould you like a summary of your conversions? (Y/N): "); a = Keyboard.nextLine(); summary = a.charAt(0); summary = Character.toUpperCase(summary); if (summary == 'Y') { System.out.println("\nCountry\t\tRate\t\tDollars\t\tAmount"); System.out.println("========\t\t=======\t\t=======\t\t========="); for (int i=0; i < Exchange.length; i++) System.out.println(Exchange[i]); Foreign.counter(); } } }
I looked at line 43 and its this line: summary = a.charAt(0);
But I am not sure what's wrong with it, can anyone point it out? Thank you.
© Stack Overflow or respective owner