• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please help, using 2D arrays  RSS feed

 
Gus Parker
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heres my method, explaination and question below




Pretty much what Im trying to do here is, go through each line of the text file with values delimited by :
there are 11 lines, with 13 values in each line all seperated by a ":"

I want to put these into this 2D array and then take them out for use as needed by my program.

Im trying to use two for loops, one for the line to go and then one for the values.

I seem to be doing something wrong, it seems to be happening around the stage when I try to put the delimited value in
one by one.

Am I going about this the right way, or should I be heading down a different path?

Or is there something small I'm just not doing right,

say around this line "allstudentObjArray[i3][i2] = new stuAll(sa1.next());"
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this compile?

new stuAll(sa1.next());

stuAll is a variable name in your code. But having "new" before it, implies that it is a type.
 
Gus Parker
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Does this compile?

new stuAll(sa1.next());

stuAll is a variable name in your code. But having "new" before it, implies that it is a type.


Sorry I dont have a compiler handy for a few hours, thanks i'll try this out tonight, although I'm fairly sure I tryed this (but cant be sure).

I originally had a much longer code to do all of this, but wanted to trim it down a lot, (also makes it easier for other parts of the program as well)....

Do you think I'm going about this the right way, or is there something else that would be easier you could suggest?
 
clara irma
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

in your line allstudentObjArray[i3][i2] = new stuAll(sal.next())
isn't it supposed to be allstudentObjArray[i3][i2] = new Student(sal.next()), with Student constructor taking a String parameter.
That depends on what 'stuAll' is in your code..

There are several other things:
For one thing, you put a 'i2++' and 'i3++' inside both of your for-loop. This will make the values incremented twice.

And in your code, for each line found in 'allStu', you run the nested for-loops, taking values from the 'sal' object.
But you never re-assign 'sal' to the next new line, so 'sal' here only has the value of the first line in allStu (which, after all token has been retrieved using .next(), should cause an exception).

You might also wants to review your logic, with the nested for-loops inside the while loop. Because the while loop (when used properly) should already serves as iterator of your lines, so you shouldn't need double loop there, depending on what you are trying to achieve..

 
Gus Parker
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
clara irma wrote:Hello,

in your line allstudentObjArray[i3][i2] = new stuAll(sal.next())
isn't it supposed to be allstudentObjArray[i3][i2] = new Student(sal.next()), with Student constructor taking a String parameter.
That depends on what 'stuAll' is in your code..

There are several other things:
For one thing, you put a 'i2++' and 'i3++' inside both of your for-loop. This will make the values incremented twice.

And in your code, for each line found in 'allStu', you run the nested for-loops, taking values from the 'sal' object.
But you never re-assign 'sal' to the next new line, so 'sal' here only has the value of the first line in allStu (which, after all token has been retrieved using .next(), should cause an exception).

You might also wants to review your logic, with the nested for-loops inside the while loop. Because the while loop (when used properly) should already serves as iterator of your lines, so you shouldn't need double loop there, depending on what you are trying to achieve..



Hi Clara, thanks for your reply.

I'm not sure if it's good and going to work or not, but with the i2++ and i3++ , i have those together in seperate loops, but still in the while loop,

so ive got

while loop = each line
first for loop = the go through and increment the first value part of multi array- [this bit][] - ....
second for loop = for each of the first values of the multi array, add each value as it delimits from the line current on in [] [this bit]

i hoping to pull the above off, would be great if i could....

but...

i just cant get this line right

"allstudentObjArray[i3][i2] = new stuAll(sal.next()) "...

but ass you mentioned I think i need to try Student there.

but do you think the Sal bit will work to go through each delimit and add it, im wondering

I'll have to try later.
 
clara irma
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm...well, from what I see, 'allStu' in your code here contains the whole text of your 'allstudents2.txt', and 'sal' contains a single line (from allStu).
That's why I was wondering about your triple loop.

But like you said, you'd better try it out first


 
Gus Parker
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dam still cant get it working
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gus Parker wrote:dam still cant get it working
Tell people what is going wrong. And it's spelt "damn."
 
Gus Parker
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Gus Parker wrote:dam still cant get it working
Tell people what is going wrong. And it's spelt "damn."


If you read my first post, you'd notice a specific question not related to looked for a quick answer. I asked a question that was looking for a serious answer. Am I was heading down the right path with it all? I came here for help, if you can't help in the way I originally asked, move on and stop being a smart ass.......

Sentences don't start with "and". Now we're even on the spelling/punctuality. Can you help or not?

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dam still cant get it working


Well, there are a few reasons why you would make such a post....

1. you tried something new and it still didn't work.

2. you are frustrated that no-one answered the question to your needs and wants an answer.

And in both of these cases....

Can you help or not?


You stand a better chance of getting a better answer if you provide more details.... Basically, Campbell, is *not* accusing you of "looking for the quick answer". He is asking you for elaboration so he can help you.

Henry
 
Gus Parker
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
dam still cant get it working


Well, there are a few reasons why you would make such a post....

1. you tried something new and it still didn't work.

2. you are frustrated that no-one answered the question to your needs and wants an answer.

And in both of these cases....

Can you help or not?


You stand a better chance of getting a better answer if you provide more details.... Basically, Campbell, is *not* accusing you of "looking for the quick answer". He is asking you for elaboration so he can help you.

Henry


Hi Henry,

Really I want to get this code working without having to scrap it and try something new. I find it an interesting challenge - as someone new to Java and programming - to want to finish something I started to help my education. The reason I posted that was...

3. To try and get some help to my original question - Am I going about this the right way, or should I be heading down a different path?

I'd like to know if there is a better way to go about this, an easier way. I feel like I might be stumbling around with a silly solution to this, when there might be something more worthwhile for this situation that I can go and look at and study, to find out more about it.

Do you think I'm heading down the right path for this?
Am I making a slight error?
Will my 3 loops in one work if I go about it the right way?
Is this good practice in the world of programming?


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anyway, as a quick glance -- since it is not possible for me to compile and run, because not all the code is available... I can see a few things wrong...

- You check to see if there are any lines left in file *after* you read the first one.
- You only read one line from the file.
- You have two indexes, i2 and i3, which are supposed to be the two indexes, but the "for" loops range them both for the first index.
- And of course, you try to get everything from the one line that you read.

Henry


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Do you think I'm heading down the right path for this?


Design issues aside -- this example is a bit small to be worrying about "right path". Assignments also generally define the "only way", regardless of whether they are right or not.

Am I making a slight error?


Yes, the errors are "slight". You just have a lot of them.

Will my 3 loops in one work if I go about it the right way?


I don't see why not.

Is this good practice in the world of programming?


Anything that you can learn from is "good practice".

Henry
 
Gus Parker
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry Your 2 posts there have been helpful, I'll give this all some thought

-Gus
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Henry. I was about to say there was no point in my helping because there were already lots of useful suggestions, but "doesn't work" doesn't tell people what help is needed next.
You have explained it better than I could have.

And ( ) Gus, you need to be careful about spelling; to those of us who grew up with English the difference in context between "dam" and "damn" is obvious. But to the readers (maybe only a few) who don't speak English well and use software to help translate that difference might cause a lot of confusion. Remember the story about the translation software which translated "out of sight, out of mind" into Russian and back, and returned "blind and mad!"

Pleased Henry has been able to help you
 
Gus Parker
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for replies.

After trying a few things, the below code I have works fine.



Thanks
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code might work, but it is very "brittle"; if you don't have exactly 30 lines with 30 names in each, you will get Exceptions or problems with nulls later on.

Were you told to use StringTokenizer? Have you read its documentation and seen you are not supposed to use it?

Have you been told about 2-D arrays? In which case (to go into pedantic mode) I shall tell you there is no such thing. See Java Language Specification §10.2 and notice what it says about [][]. So your students[][] is an array of arrays. So you can iterate through your array and say students[i] = new Student[j];

Where do you get j from? Well, remember what it said in StringTokenizer to use instead, a certain method of a well-known class? If you use that method you get an array, and that array has a length field and the new Student array will have exactly the same length . . .
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!