I have been asked to do the following:
Alter introductory problem 1 to display the entered names in alphabetical order by last name. Problem 1: Write a program that creates a 5 x 2 array. The first column represents first names and the second column will store last names. Have the user enter 5 full(first and last) names to be stored in this 2D array. After all input is taken, display the names, but show them as LastName, first initial.
I now realize that the method outlined below is not the proper/most efficient way to attack this program, but I'm still baffled as to why the program fails in the way in does. When I run the program in eclipse, it prints the line of question marks once and then throws the following exception:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(Unknown Source) at TwoD6.main(TwoD6.java:36)
It seems to be failing the second time it runs, and I can't understand why this is. I've tried changing it between variable types, fearing that the the numeral value of the char wasn't accepted (the charAt command was originally in the if statement), as well as filling the array before the user does (thinking that perhaps the error came from java not accepting that the strings would have characters at position 0).
import java.util.Scanner;
public class TwoD6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Input 5 full names (first and last).");
String [][] d2 = { { " "," " },
{" "," "},
{" "," "},
{" "," "},
{" "," "} };
//new String[5][2]; initial string declaration, which I previously thought could be the source of the issues
for(int i = 0; i < d2.length; i++)
{
d2 [i][0] = sc.next();
d2 [i][1] = sc.next();
}
for(int i = 0; i < d2.length-1; i++)
{
char c = d2[i][1].charAt(0);
char d = d2[i+1][1].charAt(0);
int e = c; int f = d;
if(e < f)
{
System.out.println("????????????????"); //implemented to see how many times the loop runs; currently prints once
String a = ""; String b = ""; //switchers
d2[i][0] = a; d2[i][1] = b;
d2[i][0] = d2[i+1][0]; d2[i+1][0] = a;
d2[i][1] = d2[i+1][1]; d2[i+1][1] = b;
}
}
for(int i = 0; i < d2.length; i++) //printing the array
{
char firstChar = d2[i][0].charAt(0);
System.out.println(d2[i][1] + ", " + firstChar + ".");
}
}
}
I would appreciate any explanations you could give me. I'm not really interested in fixing this in terms of making it a viable solution to the problem set, as I've already coded a better program, but I'd like to know what went wrong here with the charAt() commands and how to fix it for next time. Thanks!
The code in "if-block" in second "for" loop will store a zero length string at next index in your 2 -d array, this will cause exception in very next iteration
The real problem is following set of lines (in second for loop's if block )
Most precisely lines
since a="" ,b ="" The value of d2[i+1][0] and d2[i+1][1] is nothing but a zero length String . Which doesn't have any character at next index in loop
You can test it your self by adding following line in beginning and end of second for loop in your code and see the output
Here is what i got
[EDIT] Also this code doesn't solve the problem you are trying to solve even after you have fixed this bug. As there are lot of logical errors with this code