Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Converting an array to a new Array.
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
apurva234




PostPosted: 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. Very Happy
Sponsor
Sponsor
Sponsor
sponsor
ProgrammingFun




PostPosted: 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:
Java:

   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


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:
Java:

String name[][] = new String [Stnames][Stnames];

...which would give you two levels to each row:
Java:

name[0] and name[1]


Using this, you can then read the first name and last name into the 2D array:
Java:

String reading = input.readLine();
name [i] = reading.split(",");

//then, name[0] = lastname and name[1] = firstname


After this, you can just output the array:
Java:

for (int c=0; c < varname; c++)
{
    for (int d=0; d < 2; d++)
    {
        System.out.print (name[c][d] + " ");
    }
    System.out.println ();
}
apurva234




PostPosted: Tue Jun 14, 2011 7:36 pm   Post subject: RE:Converting an array to a new Array.

Thank You, it helped.
ProgrammingFun




PostPosted: 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:
Java:

name[0] and name[1]



It should actually be:
Java:

name[i][0] and name[i][1] with i being your for variable
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: