Computer Science Canada Converting an array to a new Array. |
Author: | apurva234 [ 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. |
Author: | ProgrammingFun [ Tue Jun 14, 2011 6:58 pm ] | ||||||||||
Post subject: | Re: Converting an array to a new Array. | ||||||||||
Please use the syntax tags to post your code:
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:
...which would give you two levels to each row:
Using this, you can then read the first name and last name into the 2D array:
After this, you can just output the array:
|
Author: | apurva234 [ Tue Jun 14, 2011 7:36 pm ] |
Post subject: | RE:Converting an array to a new Array. |
Thank You, it helped. |
Author: | ProgrammingFun [ Tue Jun 14, 2011 7:59 pm ] | ||||
Post subject: | Re: RE:Converting an array to a new Array. | ||||
apurva234 @ Tue Jun 14, 2011 7:36 pm wrote: Thank You, it helped.
You're welcome However, I was wrong here: ProgrammingFun wrote: ...which would give you two levels to each row:
It should actually be:
|