Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Four arrays 32 line characters from text

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    14
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Four arrays 32 line characters from text

    I am having a problem...I get arrays but when I have to read a line by line text with letters I don't understand how to place the letters in four different array.

    Import java.util*;
    import java.io.*;
     
    public class....
     
    public static void main(String[] args)
    {
      char[] array1 = new char[8];
      char[] array2 = new char[8];
      char[] array3 = new char[8];
      char[] array4 =  new char[8];
     
       Scanner inFile = new Scanner (new File ("filename"));
     
      while() //Been told that I must have a while loop to fill in the four arrays.
      {
      }
    }


    I have 32 lines, each line has a letter. I need to place the first eight line into one array, and the then following eight to the next, and so on and then print out array.

    I have no idea what to do and have read other forums but for some reason I don't understand.

    Please help.
    Last edited by sketch_flygirl; March 27th, 2011 at 11:49 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Four arrays 32 line characters from text

    There are several problems in the code you have posted.
    Import needs to be import and your Class file must have a name.

    I would simplify this as much as possible and take it one step at a time.

    Here is an example of how to populate the array with the first 8 values.
    Obviousally I am not reading in a file but this code can be easily adapted.

    public class CharArray {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */
    	public static void main(String[] args) {
     
    		String readLine = "JavaProgrammingForums.com";
    		String[] myArray = new String[8];
     
    		int counter = 0;	
     
    		// while loop to add values
    		while(counter < 8){
    			//System.out.println(counter);
    			myArray[counter] = readLine;			
    			counter++;
    		}
     
    		// Print content of array
    		for(int i = 0; i < myArray.length; i++){
    			System.out.println(myArray[i]);
    		}
     
    	}
     
    }

    When you understand what is happening above. Move on.
    If the 32 lines are constant, I suggest something like this in the while loop.
    You will obviousally need to expand on it:

    		boolean on = true;
    		int counter = 0;
     
    		// while loop to add values
    		while (on) {
    			if (counter < 8) {
    				// fill first array 
    				System.out.println(counter);
    				counter++;
    			}
    			if(counter < 16){
    				// fill second array
    				System.out.println(counter);
    				counter++;
    			}
    // etc etc ...........
    		}
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    14
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Four arrays 32 line characters from text

    So it will look somethin' like this right?

    PHP Code:
    import java.util.*;
    import java.io.*;

    public class 
    monroylab6
    {
        
    //Read scanner for public class
        
    static Scanner console = new Scanner(System.in);

        
    //METHOD: Main
        
    public static void main(String[] args)
        {

            
    //Variables

            
    String firstName;
            
    int counter 0;
            
    int menuNumber 0;
            
    boolean on true;
            
            
    char[] arrayDNA1 = new char[8];
            
    char[] arrayDNA2 = new char[8];
            
    char[] arrayDNA3 = new char[8];
            
    char[] arrayDNA4 = new char[8];

            
    System.out.println("Please enter first name: ");
            
    firstName console.next();
            
    System.out.println(" ");
        
            
    System.out.println("Hello, " firstName);
            
    System.out.println("This program will be reading a file called seq.txt that contains");
            
    System.out.println("single base letters from multiple DNA sequence fragments, and then");
            
    System.out.println(" saving thoe base letters in multiple 1-D arrays.");
            
    System.out.println("After the arrays are filled, the data will be analyzed in various ways.");

            
    //Read .txt file
            
    File inFile = new File ("seq.txt");
            
    Scanner in = new Scanner(inFile);

            while(
    on)
            {
                
    //Fill out first array
                
    if (counter 8)
                {
                    
    arrayDNA1[counter] = inFile;
                    
    counter++;

                    
    //Print out arrayDNA1
                    
    for(int i 0arrayDNA1.lengthi++)
                    {
                        
    System.out.println(arrayDNA1[i]);
                    }
                }

                
    //Fill out second array
                
    if (counter 16)
                {
                    
    arrayDNA2[counter] = inFile;
                    
    counter++;

                    
    //Print out arrayDNA2
                    
    for(int i 0arrayDNA2.lengthi++)
                    {
                        
    System.out.println(arrayDNA2[i];
                    }
                }

                
    //Fill out third array
                
    if (counter 24)
                {
                    
    arrayDNA3[counter] = inFile;
                    
    counter++;

                    
    //Print out arrayDNA3
                    
    for(int i 0arrayDNA3.lengthi++);
                    {
                        
    System.out.println(arrayDNA3[i]);
                    }
                }

                
    //Fill out fourth array
                
    if (counter 32)
                {
                    
    arrayDNA4[counter] = inFile;
                    
    counter++;

                    
    //Print out arrayDNA4
                    
    for(int i 0arrayDNA4.lengthi++);
                    {
                        
    System.out.println(arrayDNA4[i]);
                    }
                }
            }



    I think I am missing somethin' though....

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Four arrays 32 line characters from text

    Arrrrrgh! Duplicate code, run for your lives.
    char[] arr1 = fillArray();
    char[] arr2 = fillArray();
    char[] arr3 = fillArray();
    char[] arr4 = fillArray();
    The fillArray method will create a new array with a length of 8. Read 8 chars. Insert chars into array. Return array. Note: you will have to create one input stream and keep it open (perhaps pass as a parameter) otherwise you end up reading the same characters each time.

  5. #5
    Junior Member
    Join Date
    Nov 2009
    Posts
    14
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Four arrays 32 line characters from text

    I really don't understand. I for some reason am good at arrays if input but reading from text...I have no idea what the heck I am doing. I still have to analysis the arrays in other methods.

  6. #6
    Junior Member
    Join Date
    Nov 2009
    Posts
    14
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Four arrays 32 line characters from text

    I wonder will this work?
    PHP Code:
            //Read .txt file
            
    File inFile = new File ("seq.txt");
            
    Scanner in = new Scanner(inFile);

            while(
    in.hasNext())
            {
                
    arrayDNA1[counter] = in.nextChar();
                
    arrayDNA2[counter] = in.nextChar();
                
    arrayDNA3[counter] = in.nextChar();
                
    arrayDNA4[counter] = in.nextChar();
                
    counter++;

            } 
    //End of while loop 

  7. #7
    Junior Member
    Join Date
    Nov 2009
    Posts
    14
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Re: Four arrays 32 line characters from text

    Ok, I know the program underneath won't allow me to put the text line by line in order.

    PHP Code:
            //Read .txt file
            
    File inFile = new File ("seq.txt");
            
    Scanner in = new Scanner(inFile);

            while(
    in.hasNext())
            {
                
    arrayDNA1[counter] = in.nextChar();
                
    arrayDNA2[counter] = in.nextChar();
                
    arrayDNA3[counter] = in.nextChar();
                
    arrayDNA4[counter] = in.nextChar();
                
    counter++;

            } 
    //End of while loop 
    Also, I figured how to do the first array.

    PHP Code:
            //Read .txt file
            
    File inFile = new File ("seq.txt");
            
    Scanner in = new Scanner(inFile);

            while(
    in.hasNextLine() && counterDNA1 8)
            {
                
    arrayDNA1[counterDNA1] = in.next().charAt(0);
                
    counterDNA1++;

            } 
    //End of while loop DNA Sequence 1 
    I was told by my TA that I need a separate counter to continue for the rest of the sequence but I really don't understand how to do that. I have tried different ways to get the second arrayDNA2 to work...but is isn't.

    PlEASE! HELP ME! It is due friday and I still have sub methods left to analysis the data.

  8. #8
    Junior Member
    Join Date
    Nov 2009
    Posts
    14
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Four arrays 32 line characters from text

    I got the loop to work!!! Thanks for the help!!!

Similar Threads

  1. Create Multiple Arrays from a text file
    By chris11kgf in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 11th, 2011, 01:40 AM
  2. Writing to a specific line in a text file
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 7th, 2011, 09:11 PM
  3. finding unescaped XML characters
    By diptee in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 18th, 2010, 04:51 AM
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 08:34 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 07:32 AM