Hi All,
If I read in from a file and populate an array with its data(all strings), how can I control the way it prints(to the console for example).Right now it just prints a huge long line in the console of all its contents. I figure I will have to implement for loops to get it to print line by line from the text file. My main goal is eventual learning how to print it to an excel file but I am taking baby steps there to learn how to manipulate an array. Also, would an arrayList be a better option than an array for this? Thanks!
This is my test class for the array:
import java.io.*;
import java.util.*;
public class ArrayTest {
final static int ARRAY_SIZE = 500;
static String[] array = new String[ARRAY_SIZE];
static int index = 0;
public static void main(String[] args) throws FileNotFoundException {
File f = new File("MasterData.txt");
Scanner kb = new Scanner(f);
while (kb.hasNext() && index < array.length)
{
array[index] = kb.next();
System.out.print(array[index]);
index++;
}
kb.close();
}
}
This is the contents of a text file:
Squad Rank Name SSN BloodType RifleType RifleSN AcogSN PeqSN
1ST Sgt Dog 1234 OPos M4 1234567 1234 5678
1ST Sgt Dog 1234 ONeg M16A4 1234567 1234 5678
1ST Lcpl Dog 1234 APos M16A4 1234567 1234 5678
1ST Lcpl Dog 1234 ANeg M16A2 1234567 1234 5678
1ST Lcpl Dog 1234 OPos M249SAW 1234567 1234 5678
1ST Pfc Dog 1234 BPos M240B 1234567 1234 5678
2ND Cpl Dog 1234 OPos M4 1234567 1234 5678
2ND Cpl Dog 1234 ONeg M16A4 1234567 1234 5678
2ND Lcpl Dog 1234 APos M16A4 1234567 1234 5678
2ND Lcpl Dog 1234 ANeg M16A2 1234567 1234 5678
2ND Lcpl Cat 1234 OPos M249SAW 1234567 1234 5678
2ND Pfc Cat 1234 BPos M240B 1234567 1234 5678
3RD Cpl Cat 1234 OPos M4 1234567 1234 5678
3RD Cpl Cat 1234 ONeg M16A4 1234567 1234 5678
3RD Lcpl Cat 1234 APos M16A4 1234567 1234 5678
3RD Lcpl Cat 1234 ANeg M16A2 1234567 1234 5678
3RD Lcpl Dog 1234 OPos M249SAW 1234567 1234 5678
3RD Pfc Dog 1234 BPos M240B 1234567 1234 5678
4TH Cpl Dog 1234 OPos M4 1234567 1234 5678
4TH Cpl Dog 1234 ONeg M16A4 1234567 1234 5678
4TH Lcpl Dog 1234 APos M16A4 1234567 1234 5678
4TH Lcpl Dog 1234 ANeg M16A2 1234567 1234 5678
4TH Lcpl Dog 1234 OPos M249SAW 1234567 1234 5678
4TH Pfc Dog 1234 BPos M240B 1234567 1234 5678