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

Recommended Answers

All 13 Replies

instead of

System.out.print("");

use

System.out.println("");

that will make your pointer go to a new line for each print

if you want to go and use excell files in java, you may want to look into JExcelapi or poi, a library by apache to use ms office files.

Thanks stultuske for the advice!

I've already been exploring the JExcel api, and have made a few test programs to start learning it. Also, I've used both .println() and .print(). But those just print to the console in one huge long line either vertically or horizontal, respectively. I am trying to learn how to print the file line by line. For example, the textfile would print to the console just as it appears in the textfile I posted above.

Another way to have text be printed on more than one line is to insert a newline character ('\n') in the text at the point you want the following text to start on the next line. "Line 1\nLines 2\nLine 3" will print on 3 lines
The println() method automatically adds a newline character at the end of the String you are printing.

Thanks for the reply NormR1,

I understand the \n and what the println() does.

But I am trying to get my array of strings to print like this in the console:

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

If I use the println() method it prints every word one line at a time, versus a whole line of data at a time, which is what I am trying to accomplish.

I understand the \n and what the println() does.

To print this on one line put one lineend at the end of the String:
"Squad Rank Name SSN BloodType RifleType RifleSN AcogSN PeqSN\n"
If the String is in parts, use print() on all of the parts except the last one. Use println() on the last part to put the next thing printed on the next line.

when you read it in from a file, as you plan to do it eventually, you'll be reading it line by line, not word by word, in which case both println and adding "\n" will do what you want it to do.

Ah, ok I get it. I had to change my code slightly to:

while (kb.hasNextLine() && index < array.length)
        {
           array[index] = kb.nextLine();
           System.out.print(array[index] + "\n");
           index++;
        }

I had to use hasNextLine() and nextLine() instead of hasNext() and next(). And then like you guys were saying add the \n in my print statement. Thanks.

Ok now I have a new question pertaining to printing it. Is there a way to manipulate the data after it is read in so that I can equally space the elements in the array so that it appears better? For example, instead of displaying like this:

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

It would display like this:

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

Sorry, it's not displaying how I am trying to get it to, but I what I mean is everything is spaced and lined up evenly.

Couple of ways:
Pad each string with spaces so Strings in a column have the same number of characters. By padding on the right for words and on the left for numbers you can have normal alignment for those types.
Insert tab \t characters between each column.

When I add \t or \n in the text file it prints "\t" and/or "\n" with the rest of the strings. Eventually when I implement this into my program, the user will have an option to enter new data through the GUI to be stored in the textfile, so I do not think I can pad it, because that would create future formatting errors(not actual code errors but how it looks when it displays). But I guess \t and \n would potentially create display issues as well? Is there a way to do this through manipulating the array?

The \n and \t characters are for the compiler when it reads your source. It converts them to the correct binary values. If you put them in a text file, the compiler is not involved in converting them to their binary values. When you read those two characters: "\n" (note this is a 2 char String) you will have to convert them to '\n' (note a 1 char value).

You would pad or use tabbing when you format the data for output to the screen.

Ok, I will work on it for a bit on my own, I have to leave internet connection now. Thanks for the help so far! I'll be back ;)

you don't need to have it in the file.
just have your application run a bit like:
-read a line from your file into StringLine
while ( StringLine != eof )
- print that line
- print "\n" (or use println, for that matter)
- read a line from your file
end while

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.