4 Replies - 632 Views - Last Post: 23 June 2011 - 06:48 AM Rate Topic: -----

#1 Reimel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-June 11

Best way to read in text file and use the data with statistics

Posted 22 June 2011 - 10:27 PM

Hello,

I'm trying to figure out the best way to read in my text file that looks like this:

n1,n2,n3,n4,n5,n6
22,38,21,12,41,18
41,43,20,19,38,29
50,16,18,36,27,08
53,37,14,44,45,29

The firs line (n1,n2,n3,n4,n5,n6) is not in my text file its just to help understand what I want to do with the text file. I want to read them into different arrays for each number column( n1, n2, etc.), so array number 1 would contain 22,41,50,53. Then I want to use that array to find the average and most common number.

If anyone could guide me in the right direction that would be great.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Best way to read in text file and use the data with statistics

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Best way to read in text file and use the data with statistics

Posted 22 June 2011 - 11:11 PM

You could read it in as you would for any other file; line by line. Each line could then be broken into tokens using the , as the delimeter. Have a look on the forums for StringTokenzer examples. I think you will find this will get the job done :)
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,668
  • Joined: 27-December 08

Re: Best way to read in text file and use the data with statistics

Posted 23 June 2011 - 06:38 AM

According to the Oracle documentation, StringTokenizer is a legacy class, and the String split() method should be used instead. The reason is b/c StringTokenizer allows for a one-way iteration, whereas split() returns an array, which allows for random access of its elements.

As for the most common element, I would store a Map<Integer, Integer> with the number as the key, and its count as the value.
Was This Post Helpful? 1
  • +
  • -

#4 Rhokk  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 26
  • Joined: 17-June 11

Re: Best way to read in text file and use the data with statistics

Posted 23 June 2011 - 06:47 AM

View PostDaneAU, on 23 June 2011 - 02:11 AM, said:

You could read it in as you would for any other file; line by line. Each line could then be broken into tokens using the , as the delimeter. Have a look on the forums for StringTokenzer examples. I think you will find this will get the job done :)


I thought that StringTokenizer was considered to be depreciated? I know there's a method in String called String.split([delimiter]) that will do the same thing.

So you could do something like this:

String[] myData = dataToSplit.split();

for(int i =0; i < myData.length; i++){
    //Logic to split arrays by columns
}



You'll have to read in the data and place it in dataToSplit before executing this block. Sorry that I couldn't flesh it out more, work calls.

This post has been edited by Rhokk: 23 June 2011 - 06:53 AM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,668
  • Joined: 27-December 08

Re: Best way to read in text file and use the data with statistics

Posted 23 June 2011 - 06:48 AM

Quote

I thought that StringTokenizer was considered to be depreciated?

It isn't formally deprecated b/c the @deprecated annotation isn't used. However, it is considered antiquated, yes. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1