Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Read tab separated file and store on two dimensional arraylist

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!  I have tried to read a txt file . Then , I should store it on Arraylist[i][j]

For example
COL1        COL2     COL3
Obj1         Obj2      Obj3



   
I get the results but not as I expect.

Column
Obj1
Obj2
Obj3
Column
Obj1


Thanks a lot in advance!
 
Sheriff
Posts: 21920
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

I'm assuming that inputStream is a Scanner, because that's the only class I can think of that has all of hasNext, next and close. For a Scanner, hasNext and next read tokens. It uses a delimiter to separate tokens; the default delimiter is white space. In other words, because you're using hasNext and next, the Scanner is already splitting at the tabs for you. Check out hasNextLine and nextLine instead, these only split at line breaks.
 
Eleni Pap
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help.But the problem is remaining


I get
col1
col2
col3

row2
row2
row2
 
Rob Spoor
Sheriff
Posts: 21920
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're still using next and not nextLine.
 
Eleni Pap
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really appreciate your help and thanks for your time.



In this way, I can see the file as I want. But I don't store the data on ArrayList.
When I try to save the data on ArrayList, I get either an array as a column with all values or an array as a row.




 
Eleni Pap
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is another way to store the data . But on debug, I don't see  array[][] but an array[]

 
Rob Spoor
Sheriff
Posts: 21920
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nextLine still needs to be split. You had that part before, so why did you remove it?

Also, there's no need to turn a List (or Set, or any Collection) into an array before iterating over it. You can simply replace tempsArray with temps in your loop.
 
Eleni Pap
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I remove it to understand better the steps.





This create:
[column1, column2, .., ...]



And this:


This create this:
[[Ljava.lang.String;@238e0d81]
[[Ljava.lang.String;@238e0d81, [Ljava.lang.String;@5489eee81]

 
Rob Spoor
Sheriff
Posts: 21920
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That latter is expected, because (unfortunately) arrays inherit their toString (and equals and hashCode) implementations from Object. That means they get something that looks very cryptic. In this case, the [ indicates it's an array; the Ljava.lang.String; is the array's component type (so it's a String[]), and then come the @ and the system hash code in HEX.
 
Eleni Pap
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but it prints:

[Colum1]
[Column1, Row1]
[Column1, Row1, Row2]

It doesn't create the
[column1] [column2] [column3]
[row1] [ro2] [row3]

Why???

[[[Ljava.lang.String;@4565@626c]]
[[[Ljava.lang.String;@45656@26c, [Ljava.lang.String;@15689@41c]]
[[[Ljava.lang.String;@45656@26c, [Ljava.lang.String;@15689@41c, [Ljava.lang.String;@5197848c]]
[[[Ljava.lang.String;@45656@26c, [Ljava.lang.String;@1568c941, [Ljava.lang.String;@5197848c, [Ljava.lang.String;@17f052a3]]


I mean that I waited at least something like
[Ljava.lang.String;@4565@626c] [Ljava.lang.String;@4565@626c] [Ljava.lang.String;@4565@626c]
[Ljava.lang.String;@4565@626c] [Ljava.lang.String;@4565@626c] [Ljava.lang.String;@4565@626c]
etc....
 
Rob Spoor
Sheriff
Posts: 21920
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're seeing that output, then you have a List<List<String[]>>. I don't see that in the code you've provided, so it's somewhere in the code you haven't shown us.
 
Saloon Keeper
Posts: 3895
154
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It IS in the code that Eleni shows. It is the line:

That line (with missing parentheses) is within the body of the loop, so that is why you see what you see.

Another thing to improve is that now all the arrays are printed on one line, instead of each array on its own line.
 
Eleni Pap
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your time. My code has parenthesis.
So, I will try later, to complete it and show the answer!

Thanks for your help!!
 
Piet Souris
Saloon Keeper
Posts: 3895
154
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the credits to Rob, but you're welcome!
 
Rob Spoor
Sheriff
Posts: 21920
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Eleni, if it still does not work, can you show the results of adding the following inside your loop?
 
Eleni Pap
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Nothing it works.
Thank you again!!!
OUTPUT

data: FNAME LNAME    PHONE
--------------------------
values: [FNAME, LNAME,  PHONE]
--------------------------
temps to String[[Ljava.lang.String;@1a6c5a9e, [Ljava.lang.String;@37bba400, [Ljava.lang.String;@179d3b25, [Ljava.lang.String;@20ad9418]
-------------------------------------------------------
temps to Array[Ljava.lang.Object;@31cefde0
---------------------------------------------------
temps as List[[[Ljava.lang.String;@1a6c5a9e, [Ljava.lang.String;@37bba400, [Ljava.lang.String;@179d3b25...]]
 
Eleni Pap
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I use next instead nextLine, I get the first line of my text file, as many fields of an array. For example:
[FirstName] [LastName]

But it gets only the first line, I try to get all the text in 2D array.
When I use nextLine than next I get the first line as one field.
[FirstName, LastName]


Now it reads only the first line of my text file(sc.nextLine) or only the first column(sc.next)....
It does not read all the content of my file.
 
Marshal
Posts: 68909
275
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you start by going through the documentation for Scanner and read the whole of the introduction, as well as those two methods, and these two methods:-
  • hasNext()
  • hasNextLine()
  • You won't understand what is happening until you understand abut delimiters and aboiut those methods.
    Also please tell us what you have been taught the nextLine() method does.
     
    Rob Spoor
    Sheriff
    Posts: 21920
    106
    Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Line 8 is also interesting. Since you do the splitting on tabs yourself, what does it now do?
     
    Campbell Ritchie
    Marshal
    Posts: 68909
    275
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Rob Spoor wrote:. . . splitting on tabs . . . what does it now do?

    Not what you think, I expect.

    Hadn't noticed that delimiter before.
     
    Eleni Pap
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks all for your directions.

    I realized that my code runs OK. But I cannot understand it.

    I tried to get a field on ArrayList in this way //System.out.println(temps.get(3)[1]);
    and program return the value that I want.

    It confuses me the program, when appear the fields with a comma. I thought that I have one String for each line. But if I had one String , comma won't be on my ArrayList but only a blank field.
    I mean [ELENI PAP] and not [ELENI, PAP]

    On debug, I noticed only the
    line = FNAME\t LNAME\t   \tPHONE and not the
    values, which are all the fields  0\FIRSTNAME 1\LASTNAME ETC.

    So, I try with a delimiter, with split, with ArrayList, LinkedList in order to manage, what I have already done.

       


    nextLine(): It reads until to find the character ā€˜\nā€™
    next(): it reads until to find next String


    Thanks a lot all of you for your time.

     
    If you believe you can tell me what to think, I believe I can tell you where to go. Go read this tiny ad!
    Two software engineers solve most of the world's problems in one K&R sized book
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic