• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Parsing a text file to an arraylist

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I need help on an assignment where I have to read a text file then split each line of the text file into three sets of numbers then store these numbers in an arraylist so I can use them for creating an object in opengl. This is what the text file that I am reading looks like:

0.468750 0.242188 0.757812 0.437500 0.164062 0.765625 0.500000 0.093750 0.687500 0.562500 0.242188 0.671875
-0.500000 0.093750 0.687500 -0.437500 0.164062 0.765625 -0.468750 0.242188

This is what I have so far for reading the text file:

import java.util.Scanner;
import java.io.*;


class FileReader
{
public static void main(String[] args) throws IOException
{

File file = new File("data.txt");
try {

Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();

Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter(" ");
while (lineScanner.hasNext()) {

String part = lineScanner.next();
System.out.println(part + " " + part + " " + part);
}
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If given the same problem i would parse the entire file as a String and then use StringTokenizer and 'for' loop.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark

I would use the Scanner as you have. But what exactly are you stuck on?

If it is how to store them in an array list, I would create my own Object that held the 3 doubles in a way that represents what they are, then I would make the List a generic of that type of object and store the object in there.

As it is OpenGL im going to guess that the 3 doubles are X,Y,Z?

HTH
Ror

EDIT:
I would also change part of your current code to

[ November 04, 2008: Message edited by: Rory Marquis ]
 
Mark Daniel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rory!

That is exactly what I am stuck on. I'm still new to java and don't actually know how to create the arraylist or store these data in the arraylist. Yes the three doubles are X,Y,Z. Can you please show me how to create the arraylist to store these doubles?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JavaDocs would be a good place to start...

http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html

Henry
 
Mark Daniel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I created an arraylist by using:

ArrayList<String> myAL = new ArrayList();

But I don't know how to create an object which will hold the three doubles so I can add that to my arraylist.
 
Rory Marquis
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark

So you need to create a new class, and put in 3 private String attributes (instance variables) and then you need to put 3 getter methods.

Then I would say put in the three values into the constructor of that class so that it becomes immutable.

Finally, create the ArrayList as you have, but instead of <String> put your object in.

List<MyObject> myAL = new ArrayList<MyObject>();



Be sure to put in the generic after the "new ArrayList" too otherwise you will get a warning from the compiler.

Then in your loop you get your three doubles out of the file (changing them from String into double - see Double.parseDouble())

Then construct your object with the x,y,z and add it to your List.

HTH
Ror
[ November 04, 2008: Message edited by: Rory Marquis ]
 
Mark Daniel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way of doing it without having to create another class then set the doubles as an object, perhaps using a string buffer. Will I still need to create another class for the objects?

Thanks in advance.
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Daniel:
Is there a way of doing it without having to create another class then set the doubles as an object, perhaps using a string buffer. Will I still need to create another class for the objects?

Thanks in advance.



Sure there are other ways, the StringBuffer you mentioned is one of them though it increases the code's complexity and it's reuselessness. That word will be in the next Oxford edition.

Anyway, you've already created a class, the FileReader.
Just make another one called whatever you like to call it, and define your 3 attributes so you can loop through your collection again later and ask for each one by name, instead of doing the nasty with the StringBuffer.

Try to follow Rory's suggestion, if you get stuck again just ask.

You could also, since you asked for an alternative, use an array to hold the 3 values but that's not nearly as nice as an Object with attributes. This is OO, make the most of it.

Happy coding.
[ November 04, 2008: Message edited by: Taariq San ]
 
Rory Marquis
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Taariq I completely agree.
 
Mark Daniel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the advice Rory, you were right about the string buffer being too complex. I created another class and put the three private strings with their getter methods but I am not sure if I put in the values right, this is the new class:



What would I now put in the arraylist instead of <String>? Would I put in the name of the class in this case <Objects> ?

Thanks
[edit]Add code tags. CR[/edit[
[ November 04, 2008: Message edited by: Campbell Ritchie ]
 
Mark Daniel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a look at the Double.parseDouble method but I'm not sure how to do it. Can you please help me?

Thanks
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,

I think you're on the right way

What you should put in the list is exactly the class Objects you created (although another name would probably be better to not confuse it with class Object). Each entry in the list then represents one 3-dimensional vector with the x, y and z components contained in one object of type Objects.

The method parseDouble() is easy to use. Just give it a String as parameter which represents a double value and it will return a real numeric double. Because it's a static method you can directly call Double.parseDouble() without creating a Double object before.

Marco
 
Mark Daniel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Thanks for confirming that its correct I was worried I had done it wrong. I created the arraylist but I'm not sure if I added the objects in correctly, this is it:

ArrayList<MyObjects> myAL = new ArrayList<MyObjects>();
myAL.add("data1");
myAL.add("data2");
myAL.add("data3");

Would I put this arraylist in my Objects class or on my FileReader class and where on my code will I put it. I'm using eclipse and it doesn't like <MyObjects> on the arraylist, its asking me to create a class for it. Also this is how i've done the Double parse method and not sure if its correct:

String s;
Double.parseDouble(s);

Sorry for all the questions but like I said before i'm still new to java.

Thanks guys for all your help
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,

I'm going to try to clarify a few things for you...

An ArrayList is a container for "something". "something" is exactly the type you give it in those brackets like in <MyObject>. This means the list should only contain objects which are of type (=class) MyObject (or subclasses of it). What you've called "MyObject" here should be the same as "Objects" in your example above, so this is perhaps your problem. Anyway, regardless of its name this is a class you have to create yourself because it's otherwise unknown to the rest of your code. The reason for eclipse to complain will most probably be that you didn't create this class at all or you created it with a wrong name?!?

Adding things to the ArrayList with method add() is correct. BUT if you declare the list with ArrayList<MyObject> you effectively allow only things of type MyObject to be in the list. But what you do here is put some Strings in it. You should create objects of type MyObject instead, assign it the X/Y/Z values from your input data and then put these objects in the list.

Where you should put the ArrayList and all this really depends on your application. You could just put in the main() method of the main class of your application. At least for testing purposes. If you have a serious need for this program I guess it would be a good idea to learn some more Java basics and re-make the application with a better design then. If you aren't familiar with object oriented concepts in general it would probably be the best to start with a completely different application which is easier to understand.


Marco
 
Mark Daniel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marco,

Your right I changed the Arraylist from <MyObjects> to the class name <Object> which I have created and eclipse seems happy with it. I though I had already given the objects a string in my "Objects" class:

public class Objects {
private String data1 = " ";
private String data2 = " ";
private String data3 = " ";
public void setData1(String data1) {
this.data1 = data1;
}
public String getData1() {
return data1;
}
public void setData2(String data2) {
this.data2 = data2;
}
public String getData2() {
return data2;
}
public void setData3(String data3) {
this.data3 = data3;
}
public String getData3() {
return data3;
}

public Objects(String objectsData1, String objectsData2, String objectsData3) {
data1 = objectsData1;
data2 = objectsData2;
data3 = objectsData3;
}

}

How would I assign the objects with the X/Y/Z values? Right now running my FileReader class will only return one set of numbers per line for example:
-0.640625
-0.007812
-0.429688
-0.593750
-0.125000
-0.164062
-0.773438
-0.140625
-0.125000

Thanks Marco and sorry to trouble you with all these questions.
 
Rory Marquis
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again Mark,

Marco is right, you would be well advised to read though the Java tutorial and one on Object Orientated Design. You will be a better developer for it.

I strongly agree with Marco and you should take his advice on that, his description on generics was good.

However, lets see if we can help you here too...


Your right I changed the Arraylist from <MyObjects> to the class name <Object> which I have created and eclipse seems happy with it. I though I had already given the objects a string in my "Objects" class:



The above is very close.

What you should do is change it to ArrayList<Objects> which is why Marco pointed out you should change the name of your class.

However, as Marco said, a couple of small changes would be better, here is an example of what I might do


Because your OpenGL coordinates will need to be double values and not strings, therefore your object for these should store double values as above.

Then you use Double.parseDouble(String) before you put the values in. See the first part of your program reads in the file, the second part validates the contents and formats it to the correct values, i.e. double and the last part stores these in an Object Orientated way (in the OpenGLCoordinates object) and puts it into an ArrayList<OpenGLCoordinates> ready for use.

Does that help a little?
Ror

[ November 05, 2008: Message edited by: Rory Marquis ]

[ November 05, 2008: Message edited by: Rory Marquis ]

[ November 05, 2008: Message edited by: Rory Marquis ]
[ November 05, 2008: Message edited by: Rory Marquis ]
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use the code button, Mark Daniel; the code looks much better if you do.
 
Mark Daniel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I lost you guys but I might be wrong. I changed the class name of Object to OpenGLCoordinates and used the code that Rory posted. I then changed the name on my arraylist from <Objects> to <OpenGLCoordinates>. I then used the Double.parseDouble(part); method just before the System.out.println(part + " "); to convert the String part into a double is that correct? All I should do now is add these doubles to my Arraylist.
 
Rory Marquis
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark

You are close. Now all you have to do is add the OpenGLCoordinates object you made with the the 3 doubles in it into to the ArrayList.

example:


Where x,y,z, are the doubles you just read in from the file.

Post the code that you have now for the Scanner part if that helps (use the code button though).
 
Mark Daniel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rory for all your help, this is the code for my FileReader class I hope I got the objects right on my Arraylist:

[edit]Move code tags. CR[/edit]

[ November 05, 2008: Message edited by: Campbell Ritchie ]
[ November 11, 2008: Message edited by: Mark Daniel ]
 
Mark Daniel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Rory I didn't realise I put the code outside the code box.
 
Mark Daniel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Again that was a Helpfile that I was working on to test my data this is the FileReader class although there is not difference between them:
 
Rory Marquis
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark, np on the code, but you can edit a post

ok, I can see that you aren't grasping the object orientated approach, you should really go and read up about it.

In the Scanner there is a hasNextDouble() and nextDouble().

What you are trying to do is to read in the file and get out the coordinates x,y,z for as many times as they appear, then store these so that they can be used later for an OpenGL project right?

In the code below I create the ArrayList that you need to return from your method (this should be a method that parses the file into the List and then returns the List for use else where).
Then I get the Scanner, and I ask it for the next 3 doubles which I store in x y z double primitives.

Then I create a new OpenGLCoordinates object passing in the x y and z into the constructor.

Then I add this object to the List.



Let me know how you get on with that. I haven't compiled it, but it's probably pretty close, have a look though the Scanner API to make sure the nextDouble is correctly used if it doesn't work.

Does that all make better sense?
Ror

[ November 05, 2008: Message edited by: Rory Marquis ]
[ November 05, 2008: Message edited by: Rory Marquis ]
 
Campbell Ritchie
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Daniel:
Sorry Rory I didn't realise I put the code outside the code box.



But I've moved the CODE tags for you.
 
Mark Daniel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Sorry I've been away for a while I had to take care of some stuff. Thanks for all your advice I totally understand how this works now. The only two problems now are that I need to add the generics after the Arraylist which I don't know how to do. Do I just use the method:



Also my compiler is giving me a warning on "part" on the following code:



Thanks again guys I wouldn't of got this far without your help.
 
Campbell Ritchie
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have nearly got it working without generics so far, get it working then add the generics later. Go through the Java Tutorial for generics, but don't expect to read it all in 5 minutes. Even the basics take time to digest.

As for printing part: what is part? Every variable can be printed to screen with System.out.println because if you look in the API for println() you find it has been overloaded for every conceivable type of argument. But you might get peculiar results.
 
Mark Daniel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys,

I took out the line:



And all the warnings disappeared even the generics from the Arraylist. It compiles fine put does not print out anything does this mean its working fine and is sending the objects to the OpenGLCoordinates class so I can use them for my OpenGL?

This is the complete class for FileReader:


Thanks
 
Taariq San
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Daniel:
It compiles fine put does not print out anything does this mean its working fine and is sending the objects to the OpenGLCoordinates class so I can use them for my OpenGL?



It might, but why not try looping through your list and printing the values?
Or even easier, you could override the method toString of Object in your class OpenGLCoordinates, have it return a String representation of itself.
Something like "OpenGLCoordinates(x = 5.00, y, 6.55, z = 4.34)".

Then when you've added all the items to your ArrayList, just before your code exits, you can print the ArrayList and all its items will be printed to console one by one. You can compare the data with what's in the file and see if it's correct.
 
Mark Daniel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys!
Great news, I think its working but I just want you to check it. I made some changes to the println code to return the x, y, z coordinates and it printed them out exactly like I want them to be. Can you please check it and see if its correct and just give me an idea as to how I will call these objects into my openGL class. This is the code:



Thanks again everyone for all your help, I wouldn't of made it without your help.
[ November 06, 2008: Message edited by: Mark Daniel ]
 
Rory Marquis
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark

In your OpenGL class (I assume this is your own custom class) pass it the List of OpenGLCoordinates.

Then loop though the List and handle the x,y,x when you need them.

HTH
Ror
 
Mark Daniel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

I'm using an example file which already has a couple of shapes drawn using OpenGL. I can edit it to draw my QUADS but I'm not sure how to pass it my Arraylist. Do I just add the line:

to my OpenGL class?

Will I use float to read the values from the arraylist?
 
Rory Marquis
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

If your OpenGL class is in Java, then you need to create it as an Object and create a function that will take the List in as a parameter.

Inside that function you then need to loop though the List and get the values out as a double as that is what they have been stored as.

HTH
Ror
 
Mark Daniel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
So you mean return an object from the arraylist which I will then be able to call in my OpenGL class. Can you please show me how to create an object.

Thanks
 
Rory Marquis
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark

I mean, pass the List of coordinates directly to your OpenGL class (what is it called, the class?).

When you create a new class for your OpenGL (assuming you are still working in Java) you are creating a new Object, an "instance" of that class.

So in your OpenGL class put in a function which accepts the List as an argument and there you have your parsed and ready List of coordinates in your OpenGL class to do whatever you need with them.

HTH
Ror
 
Mark Daniel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rory,

My openGL class is call GLDrawing.

This is the class:


Right now it displays a red square. I basically need a loop to replace the vertex coordinates with the values from my arraylist so that it can draw the QUADS from the values of my arraylist but i'm not sure how to do it.

Thanks
[ November 11, 2008: Message edited by: Mark Daniel ]
 
Rory Marquis
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

It looks to me as in this example the GLDrawing class is the main class, so what I have done to show you is to put all that we did into this class. Normally I would say don't do this as the drawing and the parsing of the file should be separate purely for cohesive reasons.

I have also embedded the OpenGLCoordinates class into this one, this is something that you can choose to do, in this example it is fine.

However, try this and see how you get on. I haven't compiled it, but you should be able to get the rough idea of how this should all fit together, i hope.

 
Mark Daniel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rory that works perfectly. Thank you so much, I really appreciate all your trouble and hard work.
 
Rory Marquis
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark



No problem, glad it worked.

Have a play around with the classes we created, and the objects, and see how they interact. It will give you a better insight into Java Object Orientated Programming.

Good Luck
Ror
 
Mark Daniel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yea i'm already playing around with them, I'm loading some texture into the drawings now as we speak. It seems easy now that its finished but I know it wasn't without your help. I honestly can't thank you enough for all you hard work.

Thanks guys for everything.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic