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.
Code Java:
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.
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.
Code Java:
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:
Code Java:
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 ...........
}
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 = 0; i < arrayDNA1.length; i++)
{
System.out.println(arrayDNA1[i]);
}
}
//Fill out second array
if (counter < 16)
{
arrayDNA2[counter] = inFile;
counter++;
//Print out arrayDNA2
for(int i = 0; i < arrayDNA2.length; i++)
{
System.out.println(arrayDNA2[i];
}
}
//Fill out third array
if (counter < 24)
{
arrayDNA3[counter] = inFile;
counter++;
//Print out arrayDNA3
for(int i = 0; i < arrayDNA3.length; i++);
{
System.out.println(arrayDNA3[i]);
}
}
//Fill out fourth array
if (counter < 32)
{
arrayDNA4[counter] = inFile;
counter++;
//Print out arrayDNA4
for(int i = 0; i < arrayDNA4.length; i++);
{
System.out.println(arrayDNA4[i]);
}
}
}
}
I think I am missing somethin' though....
Re: Four arrays 32 line characters from text
Arrrrrgh! Duplicate code, run for your lives.
Code java:
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.
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.
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
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.
Re: Four arrays 32 line characters from text
I got the loop to work!!! Thanks for the help!!!