Posted: Tue Jun 14, 2011 6:22 pm Post subject: Converting an array to a new Array.
Hello,
I have a been given an assignment where id have to read in a text file into an array, and format the text file and print it out. In my case, the file contains names of the students of our class, so i swapped the lastname with the first name to show the firstname before the lastname. (Sorry its a bit confusing). Here is the code:
import java.io.*;
public class FirstName_LastName
{
public static void main (String[] args) throws IOException
{ //main method
/* Class used for reading data from files, allowing for an InputStreamReader to be
constructed on a FileInputStream. */
FileReader file = new FileReader ("LastNameList.txt");
BufferedReader input = new BufferedReader (file);
final int Stnames = 28;
String name[] = new String [Stnames];
for (int i = 0 ; i < name.length ; i++)
{//start of i for
//Reads in data from file into "name" array.
name [i] = (input.readLine ());
for (int j = 0 ; j < name [i].length () ; j++)
{//start of j for
if (name [i].charAt (j) == ',')
{//start of if
// int j is used to track the comma within the name.
int LName = j;
int FName = j + 2; // First name is printed after the comma and space.
for (int k = FName ; k < name [i].length();k++)
{//start of k for
System.out.print (name [i].charAt (k));
} //end of if
System.out.print (", ");
for (int l = 0 ; l < LName ; l++)
{//start of l for
System.out.print (name [i].charAt (l));
}
}
}
System.out.println (); // ensures the proper displayment of names.
}
}
}//main method
what i want to know is, how would i convert the name[i].charAt (l)); to a brand new array so i can store the final output so i can bubble sort?
and by the way, i'm a beginner to java so please be kind as to what some of the comments may be.
Sponsor Sponsor
ProgrammingFun
Posted: Tue Jun 14, 2011 6:58 pm Post subject: Re: Converting an array to a new Array.
publicstaticvoid main (String[] args)throwsIOException {//main method
/* Class used for reading data from files, allowing for an InputStreamReader to be
constructed on a FileInputStream. */ FileReader file = newFileReader("LastNameList.txt");
BufferedReader input = newBufferedReader(file);
for(int i = 0 ; i < name.length ; i++) {//start of i for //Reads in data from file into "name" array.
name [i] = (input.readLine());
for(int j = 0 ; j < name [i].length() ; j++) {//start of j for if(name [i].charAt(j) == ',') {//start of if // int j is used to track the comma within the name. int LName = j;
int FName = j + 2; // First name is printed after the comma and space. for(int k = FName ; k < name [i].length();k++) {//start of k for System.out.print(name [i].charAt(k));
}//end of if System.out.print(", ");
for(int l = 0 ; l < LName ; l++) {//start of l for
} } } System.out.println(); // ensures the proper displayment of names. } } }//main method
I do not completely understand your question.
If you want to store the first names and last names in two different arrays, you can create a two dimensional array: