Hi guys, I'm currently making a GUI which displays a graph. At the moment it reads coordinates within an array in the code:

public int datax[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
public int datay[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; 

I need it to take these coordinates from a txt file, which I'm having trouble with.

So far I've made it read in a txt file and output to a text area like this:

public void readFile() {
         // Disable read button
          readFile.setEnabled(false);

    // Dimension data structure
getNumberOfLines();
data = new String[numLines];

// Read file
readTheFile();

// Output to text area  
textArea.setText(data[0] + "\n");
for(int index=1; index < data.length;index++)
    textArea.append(data[index] + "\n");

// Rnable open button
openFile.setEnabled(true);
 }

My code to plot the coordinates using datasets is this:

//Configure dataset
int n = 10;
     Dataset dataset = new Dataset (1, 2, n);

        for (int k = 0; k < n; ++k)
     dataset.set(0, 0, k, datax[k]);

        for (int k = 0; k < n; ++k)
     dataset.set(0, 1, k, datay[k]); 

I need datax and datay to be read in from the txt file though which is formatted simply x val, y val [per line]

3, 4
2, 6
5, 9 etc..

Even if it read the file into an array, then outputted this to the graph, that would work! I know this should be straightforward, but I just can't see how to do it. Some areas of Java I get, others I definately don't :cry: Can anyone possibly suggest a solution?

Thanks
Lev

Recommended Answers

All 11 Replies

Member Avatar for iamthwee

It seems like all you need to do is read a basic tutorial about file I/O and how to parse that infomation into an array or vector?

There's lot to choose from, consult google.

I've looked through whats on Google, all I'm looking for is something that will feed (parse) the output of the file reader into the datax and datay arrays. My Java is terrible!

Member Avatar for iamthwee

>My Java is terrible!

So do something about it. Come on kiddo, how hard can it be, read in a file, use the whitespace or comma as a delimiter, push them into a vector then plot them.

Simple as pie.

Can anyone other than this guy give me some advice for the coding? When you've been thrown in at the deep end with Java it isn't 'simple as pie'.... I think it must be something like datax.add(data.toIntArray()); :-S

Member Avatar for iamthwee

Can anyone other than this guy give me some advice for the coding? When you've been thrown in at the deep end with Java it isn't 'simple as pie'.... I think it must be something like datax.add(data.toIntArray()); :-S

Like I said, now is the time to take responsibility for yourself and stop being spoon fed. Grow up and do it yourself.

If you don't know how, then you should have been paying more attention in class instead of looking for nekid ladeeze.

ThanQ

Hi guys, I'm currently making a GUI which displays a graph. At the moment it reads coordinates within an array in the code:


public int datax[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
public int datay[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

I need it to take these coordinates from a txt file, which I'm having trouble with.

So far I've made it read in a txt file and output to a text area like this:


public void readFile() {
// Disable read button
readFile.setEnabled(false);

// Dimension data structure
getNumberOfLines();
data = new String[numLines];

// Read file
readTheFile();

// Output to text area
textArea.setText(data[0] + "\n");
for(int index=1; index < data.length;index++)
textArea.append(data[index] + "\n");

// Rnable open button
openFile.setEnabled(true);
}

My code to plot the coordinates using datasets is this:


//Configure dataset
int n = 10;
Dataset dataset = new Dataset (1, 2, n);

for (int k = 0; k < n; ++k)
dataset.set(0, 0, k, datax[k]);

for (int k = 0; k < n; ++k)
dataset.set(0, 1, k, datay[k]);

I need datax and datay to be read in from the txt file though which is formatted simply x val, y val [per line]
3, 4
2, 6
5, 9 etc..

Even if it read the file into an array, then outputted this to the graph, that would work! I know this should be straightforward, but I just can't see how to do it. Some areas of Java I get, others I definately don't :cry: Can anyone possibly suggest a solution?


Thanks
Lev

Here are a few steps:

Start with a buffered reader:

BufferedReader br = new BufferedReader(new FileReader(file));

once you have that, then you can read from the file:

String in = "";
while ( (in = br.readLine()) != null)
{  
   //parse the code
  //add the arraylist
}

The parsing is the only part that might cause you trouble, but if you use a StringTokenizer then you should have no problem at all.

Like I said, now is the time to take responsibility for yourself and stop being spoon fed. Grow up and do it yourself.

If you don't know how, then you should have been paying more attention in class instead of looking for nekid ladeeze.

ThanQ

What gives you the right to be so harsh? You need to pay more attention in English class.

Member Avatar for iamthwee

>What gives you the right to be so harsh? You need to pay more attention in English class.

-Ummm ok? Thanx for that mom?

You gotta be joking right? It's simple stuff, I don't see what da problem is? Do your own homework kiddo:eek:

>What gives you the right to be so harsh? You need to pay more attention in English class.

-Ummm ok? Thanx for that mom?

Don't be a hypocrite. You made a comment worse than I did except in a different context.

You gotta be joking right? It's simple stuff, I don't see what da problem is?

It's simple stuff just like the stuff you post.

Do your own homework kiddo:eek:

It's not my homework and I'm not a 'kiddo'.

Member Avatar for iamthwee

>Don't be a hypocrite. You made a comment worse than I did except in a different context.

Huh? What are you going on about?

>It's simple stuff just like the stuff you post.

Aaaaaha ha, thanx for helping me tho, I appreciate that? ;)

>It's not my homework and I'm not a 'kiddo'.

I wasn't referring to you :rolleyes:, and I use the word kiddo in a derogatory sense.

ThanQ

P.S can't we just kiss and make up.... prettie please. I much prefer it when we're not fighting. Plus I bet it annoys Dani ;)

>Don't be a hypocrite. You made a comment worse than I did except in a different context.

Huh? What are you going on about?

>It's simple stuff just like the stuff you post.

Aaaaaha ha, thanx for helping me tho, I appreciate that? ;)

>It's not my homework and I'm not a 'kiddo'.

I wasn't referring to you :rolleyes:, and I use the word kiddo in a derogatory sense.

ThanQ

P.S can't we just kiss and make up.... prettie please. I much prefer it when we're not fighting. Plus I bet it annoys Dani ;)

server_crash: Thanks for the tip- I'll get down to it tomorrow, just got back and was looking forward to bed until I read the contributions from seizureandy.com


Andy- look mate, just because you find it easy yourself doesn't mean others will. I should grow up and take responsibility for myself- oh really? You obviously know me better than I know myself, but thanks for the dad advice anyway..

I posted on here because I really don't understand this part of what I have to do. The Java is only a small part of my final uni project, and I haven't been taught it in 'class'....I see your ready to ask others for help about changing cursors but when it comes to vectors and delimiters I should grow up and do it myself :-| . With vast skills like yours I can see how you must be easily disgusted at kiddos like myself.

Wasn't planning on sinking to your level...but I think your website probably says enough about the kind of guy you are :rolleyes:

God Bless

Member Avatar for iamthwee

>Look andy,

That's not my website, or me, perhaps I should change my signature so people don't get confused. :sad:

>I should grow up and take responsibility for myself- oh really? You obviously know me better than I know myself, but thanks for the dad advice anyway..

The only reason I was being so harsh was because of that rather sarcastic private mail you sent me, of course no-one else knows of that but that's ok.

However, regardless I still think my response was an informative one. I told you that you need to check out basic file IO for java. It isn't difficult to search for that using google and find some useful links. Invariably, you should also get some information about reading the data as well.

Perhaps, in your defense, if you really do need to use a string tokeniser and vectors, that would justify your need for help.

>I see your ready to ask others for help about changing cursors

Yes, I am new to this too. I come here to learn what a can about programming...there is much to learn and I find the answers here more informative than the ones I get from my silly teachers. ;) Sorrie if I offended you kiddo.

P.S may I ask what your final uni-project is?

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.