Aligning data

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 634 Views - Last Post: 07 August 2011 - 03:56 PM Rate Topic: -----

#1 sweetpea56mw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 10-July 11

Aligning data

Posted 04 August 2011 - 06:47 PM

The question asked that I write a code that reads the .txt document that he gave us. I did that but the text isn't aligned. How can I fix this?

Here are the classes I used to import the document
package textDocument;

import java.io.IOException;

public class stock {
	
	public static void main (String[]args)throws IOException{
		String file_name="C:\\Users\\Mary\\Documents\\StockData.txt";
		
		try
		{ReadTxt file=new ReadTxt(file_name);
		String[]aryLines=file.OpenFile();
		
		int i;
		for(i=0;i<aryLines.length;i++){
			System.out.println(aryLines[i]);
		}
		}
		catch (IOException e){
			System.out.println(e.getMessage());
		}
	}

}



package textDocument;

import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;

public class ReadTxt {
	
	private String path;
	public ReadTxt(String file_path){
		path=file_path;
	}
	public String[]OpenFile()throws IOException{
			FileReader stock=new FileReader(path);
			BufferedReader textReader=new BufferedReader(stock);
			
			int numberOfLines=readLines();
			String[]textData=new String[numberOfLines];
			
			int i;
			
			for(i=0;i<numberOfLines;i++){
				textData[i]=textReader.readLine();
			}
			textReader.close();
			return textData;
	}
	int readLines()throws IOException{
		FileReader file_to_read=new FileReader(path);
		BufferedReader bf=new BufferedReader(file_to_read);
		
		String aLine;
		int numberOfLines=0;
		
		while((aLine=bf.readLine())!=null){
			numberOfLines++;
		}
		bf.close();
		return numberOfLines;
	}

}


This is what my output looks like.
Symbol OpeningPrice ClosingPrice Volume
GLG 42.46 44.10 1,827,000
FTO 29.00 29.40 1,388,700
CELG 52.10 53.44 12,419,966
WFR 36.45 35.50 4,806,400
AMX 41.25 42.87 6,133,600
ADM 39.25 38.50 6,264,200
GRMN 52.99 53.41 3,478,047
GILD 69.20 68.90 2,789,505
TXU 63.10 63.13 2,953,200
HOC 47.74 47.71 970,000
GYMB 46.82 46.46 631,598
XTO 45.95 46.66 4,968,200
CCJ 34.75 35.13 1,746,900
NITE 18.26 18.65 1,202,794
WNR 23.45 23.56 314,400
TS 38.85 38.59 1,464,400
LVS 74.60 76.20 1,285,300
LIFC 22.75 23.43 1,236,279
AAUK 22.55 22.68 476,948
FORM 37.93 38.18 614,417
STLD 60.47 60.11 1,522,391
HOLX 48.45 48.15 689,105
WCC 65.32 65.27 500,200
CTSH 77.65 75.28 2,067,837
HLX 31.49 32.24 856,200
ICE 85.00 84.42 1,995,400
TTI 26.13 25.90 962,500
BOOM 32.76 32.24 474,225
DRIV 56.70 57.85 1,762,669
COG 52.10 52.91 322,500
GOL 31.50 31.15 1,100,000
GI 80.87 80.98 190,200
FMD 65.52 67.45 597,800
ALY 15.17 15.07 396,700
RRC 26.98 27.15 1,150,900

Is This A Good Question/Topic? 0
  • +

Replies To: Aligning data

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Aligning data

Posted 04 August 2011 - 07:05 PM

when you have a line you can split it based on " " using

String[] token = line.split(" ");

that will give you 4 tokens for each line
you can them use System.out.printf("%-5s %5s %5s %10s\n", token[0], token[1], token[2], token[3]);

or a format like that
Was This Post Helpful? 0
  • +
  • -

#3 sweetpea56mw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 10-July 11

Re: Aligning data

Posted 05 August 2011 - 12:46 PM

View Postpbl, on 04 August 2011 - 07:05 PM, said:

when you have a line you can split it based on " " using

String[] token = line.split(" ");

that will give you 4 tokens for each line
you can them use System.out.printf("%-5s %5s %5s %10s\n", token[0], token[1], token[2], token[3]);

or a format like that


I'm not sure how to put that in my code.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,258
  • Joined: 27-December 08

Re: Aligning data

Posted 05 August 2011 - 01:01 PM

The variable pbl had named "line" should be replaced with whatever variable you use to store a line you read in.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Aligning data

Posted 05 August 2011 - 09:30 PM

   public static void main(String[] args) {
	   String[] line = {"ABC 12.34 56.78 1,234,5678", "DEFGH 1.2 3.4 1,234"};
	   
	   for(int i = 0; i < line.length; ++i) {
		   String[] token = line[i].split(" ");
		   System.out.printf("%-8s %6s %6s %12s\n", token[0], token[1], token[2], token[3]);
	   }
   }


yields
ABC       12.34  56.78   1,234,5678
DEFGH       1.2    3.4        1,234



%-8s in the format means use 8 characters to print a String justify to the left
%12s in the format means use 12 characters to print a String justify to the right
Was This Post Helpful? 0
  • +
  • -

#6 sweetpea56mw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 10-July 11

Re: Aligning data

Posted 07 August 2011 - 07:30 AM

View Postpbl, on 05 August 2011 - 09:30 PM, said:

   public static void main(String[] args) {
	   String[] line = {"ABC 12.34 56.78 1,234,5678", "DEFGH 1.2 3.4 1,234"};
	   
	   for(int i = 0; i < line.length; ++i) {
		   String[] token = line[i].split(" ");
		   System.out.printf("%-8s %6s %6s %12s\n", token[0], token[1], token[2], token[3]);
	   }
   }


yields
ABC       12.34  56.78   1,234,5678
DEFGH       1.2    3.4        1,234



%-8s in the format means use 8 characters to print a String justify to the left
%12s in the format means use 12 characters to print a String justify to the right

I see how this works if your typing the text in but what about when its being read in? I thought it could be as simple as adding the documents address in but that just creates an error.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,258
  • Joined: 27-December 08

Re: Aligning data

Posted 07 August 2011 - 08:28 AM

At this point, without seeing your revised code and the errors the compiler produces, we are the blind leading the blind.
Was This Post Helpful? 0
  • +
  • -

#8 sweetpea56mw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 10-July 11

Re: Aligning data

Posted 07 August 2011 - 08:56 AM

View Postmacosxnerd101, on 07 August 2011 - 08:28 AM, said:

At this point, without seeing your revised code and the errors the compiler produces, we are the blind leading the blind.

package textDocument;

import java.io.IOException;
import java.util.StringTokenizer;

public class stock {
	
	public static void main (String[]args)throws IOException{
		String file_name="C:\\Users\\Mary\\Documents\\StockData.txt";
		
		try
		{ReadTxt file=new ReadTxt(file_name);
		String[]aryLines=file.OpenFile();
		
		int i;
		for(i=0;i<aryLines.length;i++){
			System.out.println(aryLines[i]);
		}
		}
		catch (IOException e){
			System.out.println(e.getMessage());
		}
				  
			String[] line = {"C:\\Users\\Mary\\Documents\\StockData.txt"};  
	     	     for(int i = 0; i < line.length; ++i) {  
			     String[] token = line[i].split(" ");  
			     System.out.printf("%-8s %6s %6s %12s\n", token[0], token[1], token[2], token[3]);  

			    
	}
	     }
		

}


package textDocument;

import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.StringTokenizer;

public class ReadTxt {
	
	private String path;
	public ReadTxt(String file_path){
		path=file_path;
	}
	public String[]OpenFile()throws IOException{
			FileReader stock=new FileReader(path);
			BufferedReader textReader=new BufferedReader(stock);
			
			int numberOfLines=readLines();
			String[]textData=new String[numberOfLines];
			
			int i;
			
			for(i=0;i<numberOfLines;i++){
				textData[i]=textReader.readLine();
			}
			textReader.close();
			return textData;
	}
	int readLines()throws IOException{
		FileReader file_to_read=new FileReader(path);
		BufferedReader bf=new BufferedReader(file_to_read);
		
		String aLine;
		int numberOfLines=0;
		
		while((aLine=bf.readLine())!=null){
			numberOfLines++;
		}
		bf.close();
		return numberOfLines;
	 	
	}




}



Symbol OpeningPrice ClosingPrice Volume
GLG 42.46 44.10 1,827,000
FTO 29.00 29.40 1,388,700
CELG 52.10 53.44 12,419,966
WFR 36.45 35.50 4,806,400
AMX 41.25 42.87 6,133,600
ADM 39.25 38.50 6,264,200
GRMN 52.99 53.41 3,478,047
GILD 69.20 68.90 2,789,505
TXU 63.10 63.13 2,953,200
HOC 47.74 47.71 970,000
GYMB 46.82 46.46 631,598
XTO 45.95 46.66 4,968,200
CCJ 34.75 35.13 1,746,900
NITE 18.26 18.65 1,202,794
WNR 23.45 23.56 314,400
TS 38.85 38.59 1,464,400
LVS 74.60 76.20 1,285,300
LIFC 22.75 23.43 1,236,279
AAUK 22.55 22.68 476,948
FORM 37.93 38.18 614,417
STLD 60.47 60.11 1,522,391
HOLX 48.45 48.15 689,105
WCC 65.32 65.27 500,200
CTSH 77.65 75.28 2,067,837
HLX 31.49 32.24 856,200
ICE 85.00 84.42 1,995,400
TTI 26.13 25.90 962,500
BOOM 32.76 32.24 474,225
DRIV 56.70 57.85 1,762,669
COG 52.10 52.91 322,500
GOL 31.50 31.15 1,100,000
GI 80.87 80.98 190,200
FMD 65.52 67.45 597,800
ALY 15.17 15.07 396,700
RRC 26.98 27.15 1,150,900
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at textdocument.stock.main(stock.java:27)
Was This Post Helpful? 0
  • +
  • -

#9 sweetpea56mw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 10-July 11

Re: Aligning data

Posted 07 August 2011 - 09:13 AM

The output doesnt look like it does in eclipse, There is space between each coulum header.

This post has been edited by sweetpea56mw: 07 August 2011 - 09:15 AM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Aligning data

Posted 07 August 2011 - 09:35 AM

You are trying to use the printf() or the String containint the filename :)

			String[] line = {"C:\\Users\\Mary\\Documents\\StockData.txt"};  
	     	     for(int i = 0; i < line.length; ++i) {  
			     String[] token = line[i].split(" ");  
			     System.out.printf("%-8s %6s %6s %12s\n", token[0], token[1], token[2], token[3]);  


should be on the lines you read
	public static void main (String[]args)throws IOException{
		String file_name="C:\\Users\\Mary\\Documents\\StockData.txt";
		
		try
		{  ReadTxt file=new ReadTxt(file_name);
		String[]aryLines=file.OpenFile();
		
		int i;
		for(i=0;i<aryLines.length;i++){
			     String[] token = aryLines[i].split(" ");  
			     System.out.printf("%-8s %6s %6s %12s\n", token[0], token[1], token[2], token[3]);  
		}
		}
		catch (IOException e){
			System.out.println(e.getMessage());
		}
				  
			    
	}
	     }
		

}



Was This Post Helpful? 1
  • +
  • -

#11 sweetpea56mw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 10-July 11

Re: Aligning data

Posted 07 August 2011 - 10:34 AM

This is the error I get when I put in what you just gave me, am I missing something? Do I need to do something to make it to where it can be edited because there's a lot more I have to do with this and none of it seems to be working. Does something need to be added to the other class to make what I just added work?

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at textdocument.stock.main(stock.java:18)

package textDocument;

import java.io.IOException;


public class stock {
	
	public static void main (String[]args)throws IOException{
		String file_name="C:\\Users\\Mary\\Documents\\StockData.txt";
		
		try
		{ReadTxt file=new ReadTxt(file_name);
		String[]aryLines=file.OpenFile();
		
		int i;
		for(i=0;i<aryLines.length;i++){
			String[]token=aryLines[i].split(" ");
			System.out.printf("%-8s %6s %6s %12s\n", token[0], token[1], token[2], token[3]);   
		}
		}
		catch (IOException e){
			System.out.println(e.getMessage());
		}
				  
	 
	     }
		

}

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Aligning data

Posted 07 August 2011 - 12:27 PM

I'll bet that one (probably the last one) lines have a problem
Do:

	public static void main (String[]args)throws IOException{
		String file_name="C:\\Users\\Mary\\Documents\\StockData.txt";

		try {
			ReadTxt file=new ReadTxt(file_name);
			String[]aryLines=file.OpenFile();

			int i;
			for(i=0;i<aryLines.length;i++){
				String[]token=aryLines[i].split(" ");
				if(token.length == 4)
					System.out.printf("%-8s %6s %6s %12s\n", token[0], token[1], token[2], token[3]); 
				else
					System.out.println("Line " + i + " has a problem: >" + aryLines[i] + "<");
			}
		}
		catch (IOException e){
			System.out.println(e.getMessage());
		}
	}


Was This Post Helpful? 0
  • +
  • -

#13 sweetpea56mw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 10-July 11

Re: Aligning data

Posted 07 August 2011 - 01:08 PM

View Postpbl, on 07 August 2011 - 12:27 PM, said:

I'll bet that one (probably the last one) lines have a problem
Do:

	public static void main (String[]args)throws IOException{
		String file_name="C:\\Users\\Mary\\Documents\\StockData.txt";

		try {
			ReadTxt file=new ReadTxt(file_name);
			String[]aryLines=file.OpenFile();

			int i;
			for(i=0;i<aryLines.length;i++){
				String[]token=aryLines[i].split(" ");
				if(token.length == 4)
					System.out.printf("%-8s %6s %6s %12s\n", token[0], token[1], token[2], token[3]); 
				else
					System.out.println("Line " + i + " has a problem: >" + aryLines[i] + "<");
			}
		}
		catch (IOException e){
			System.out.println(e.getMessage());
		}
	}



They all come out as having a problem

Line 0 has a problem: >Symbol OpeningPrice ClosingPrice Volume<
Line 1 has a problem: >GLG 42.46 44.10 1,827,000<
Line 2 has a problem: >FTO 29.00 29.40 1,388,700<
Line 3 has a problem: >CELG 52.10 53.44 12,419,966<
Line 4 has a problem: >WFR 36.45 35.50 4,806,400<
Line 5 has a problem: >AMX 41.25 42.87 6,133,600<
Line 6 has a problem: >ADM 39.25 38.50 6,264,200<
Line 7 has a problem: >GRMN 52.99 53.41 3,478,047<
Line 8 has a problem: >GILD 69.20 68.90 2,789,505<
Line 9 has a problem: >TXU 63.10 63.13 2,953,200<
Line 10 has a problem: >HOC 47.74 47.71 970,000<
Line 11 has a problem: >GYMB 46.82 46.46 631,598<
Line 12 has a problem: >XTO 45.95 46.66 4,968,200<
Line 13 has a problem: >CCJ 34.75 35.13 1,746,900<
Line 14 has a problem: >NITE 18.26 18.65 1,202,794<
Line 15 has a problem: >WNR 23.45 23.56 314,400<
Line 16 has a problem: >TS 38.85 38.59 1,464,400<
Line 17 has a problem: >LVS 74.60 76.20 1,285,300<
Line 18 has a problem: >LIFC 22.75 23.43 1,236,279<
Line 19 has a problem: >AAUK 22.55 22.68 476,948<
Line 20 has a problem: >FORM 37.93 38.18 614,417<
Line 21 has a problem: >STLD 60.47 60.11 1,522,391<
Line 22 has a problem: >HOLX 48.45 48.15 689,105<
Line 23 has a problem: >WCC 65.32 65.27 500,200<
Line 24 has a problem: >CTSH 77.65 75.28 2,067,837<
Line 25 has a problem: >HLX 31.49 32.24 856,200<
Line 26 has a problem: >ICE 85.00 84.42 1,995,400<
Line 27 has a problem: >TTI 26.13 25.90 962,500<
Line 28 has a problem: >BOOM 32.76 32.24 474,225<
Line 29 has a problem: >DRIV 56.70 57.85 1,762,669<
Line 30 has a problem: >COG 52.10 52.91 322,500<
Line 31 has a problem: >GOL 31.50 31.15 1,100,000<
Line 32 has a problem: >GI 80.87 80.98 190,200<
Line 33 has a problem: >FMD 65.52 67.45 597,800<
Line 34 has a problem: >ALY 15.17 15.07 396,700<
Line 35 has a problem: >RRC 26.98 27.15 1,150,900<
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Aligning data

Posted 07 August 2011 - 01:44 PM

I can't beleive it :) What do you have in your file ?

Try that, we'll see
	public static void main (String[]args)throws IOException{
		String file_name="C:\\Users\\Mary\\Documents\\StockData.txt";

		try {
			ReadTxt file=new ReadTxt(file_name);
			String[]aryLines=file.OpenFile();

			int i;
			for(i=0;i<aryLines.length;i++){
				String[]token=aryLines[i].split(" ");
				if(token.length == 4)
					System.out.printf("%-8s %6s %6s %12s\n", token[0], token[1], token[2], token[3]); 
				else
					System.out.println("Line " + i + " has a problem: >" + aryLines[i] + "< it has " + token.length + " tokens");
			}
		}
		catch (IOException e){
			System.out.println(e.getMessage());
		}
	}


Was This Post Helpful? 0
  • +
  • -

#15 sweetpea56mw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 10-July 11

Re: Aligning data

Posted 07 August 2011 - 01:59 PM

Thank god I did well in the first half of this class, it's not looking to good for this code.

Line 0 has a problem: >Symbol OpeningPrice ClosingPrice Volume<it has2tokens
Line 1 has a problem: >GLG 42.46 44.10 1,827,000<it has1tokens
Line 2 has a problem: >FTO 29.00 29.40 1,388,700<it has1tokens
Line 3 has a problem: >CELG 52.10 53.44 12,419,966<it has1tokens
Line 4 has a problem: >WFR 36.45 35.50 4,806,400<it has1tokens
Line 5 has a problem: >AMX 41.25 42.87 6,133,600<it has1tokens
Line 6 has a problem: >ADM 39.25 38.50 6,264,200<it has1tokens
Line 7 has a problem: >GRMN 52.99 53.41 3,478,047<it has1tokens
Line 8 has a problem: >GILD 69.20 68.90 2,789,505<it has1tokens
Line 9 has a problem: >TXU 63.10 63.13 2,953,200<it has1tokens
Line 10 has a problem: >HOC 47.74 47.71 970,000<it has1tokens
Line 11 has a problem: >GYMB 46.82 46.46 631,598<it has1tokens
Line 12 has a problem: >XTO 45.95 46.66 4,968,200<it has1tokens
Line 13 has a problem: >CCJ 34.75 35.13 1,746,900<it has1tokens
Line 14 has a problem: >NITE 18.26 18.65 1,202,794<it has1tokens
Line 15 has a problem: >WNR 23.45 23.56 314,400<it has1tokens
Line 16 has a problem: >TS 38.85 38.59 1,464,400<it has1tokens
Line 17 has a problem: >LVS 74.60 76.20 1,285,300<it has1tokens
Line 18 has a problem: >LIFC 22.75 23.43 1,236,279<it has1tokens
Line 19 has a problem: >AAUK 22.55 22.68 476,948<it has1tokens
Line 20 has a problem: >FORM 37.93 38.18 614,417<it has1tokens
Line 21 has a problem: >STLD 60.47 60.11 1,522,391<it has1tokens
Line 22 has a problem: >HOLX 48.45 48.15 689,105<it has1tokens
Line 23 has a problem: >WCC 65.32 65.27 500,200<it has1tokens
Line 24 has a problem: >CTSH 77.65 75.28 2,067,837<it has1tokens
Line 25 has a problem: >HLX 31.49 32.24 856,200<it has1tokens
Line 26 has a problem: >ICE 85.00 84.42 1,995,400<it has1tokens
Line 27 has a problem: >TTI 26.13 25.90 962,500<it has1tokens
Line 28 has a problem: >BOOM 32.76 32.24 474,225<it has1tokens
Line 29 has a problem: >DRIV 56.70 57.85 1,762,669<it has1tokens
Line 30 has a problem: >COG 52.10 52.91 322,500<it has1tokens
Line 31 has a problem: >GOL 31.50 31.15 1,100,000<it has1tokens
Line 32 has a problem: >GI 80.87 80.98 190,200<it has1tokens
Line 33 has a problem: >FMD 65.52 67.45 597,800<it has1tokens
Line 34 has a problem: >ALY 15.17 15.07 396,700<it has1tokens
Line 35 has a problem: >RRC 26.98 27.15 1,150,900<it has1tokens

This post has been edited by sweetpea56mw: 07 August 2011 - 02:00 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2