• Post Reply Bookmark Topic Watch Topic
  • New Topic

how can i make this shorter?  RSS feed

 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we had a little exam today asking to print out a table [product name, sku, size, color, quantity] using 5-6 arrays, and creating 2 methods: 1 - method to call for the table to print and 2 - method to call for the 5th product line. i did it, but it was really long and drawn out, so i'm just wondering what a faster or more efficient method of coding this while getting the same result.

here is my code:


thanks in advance!
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
btw this is what i'm working with thus far trying to shorten this, but if there's a better way i'm open to that as well...

 
Michael Jefferson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Yes! I have done this too.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First. Splitting data to separate arrays is just bad idea (but if this was in assignment, I can live with that ).
Better way is to create Product class with all those fields and make Product[] array.

Secondly,
replace this
with this

Next,

this is just pointless. You create variable i with value 0 and then check if i is equal to 0. You could drop entire if statement.

If you had Product class defined with toString method overriden you coud write:
(where products is an array of any collection of type Product)


Also, please don't write questions in commends (inside your code).
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaptain Javathor wrote:

Yes! I have done this too.


Wrong topic?
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the replies guys! i got it like this:



and you're right about the if ( i == 0 ), i realize that's a redundant way to do it but i couldn't get anything else to work. i will definitely look at how you did it! thanks again
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have the same code in two places (for printing). That is generally bad idea.
My suggestion is. Make one method that prints a single element. It could look like this:

Then you could make method like this to print fifth element:


and this to print all elements:


You could make another method printTitles to print your headers.

I'll repeat myself. This way of solving this task is bad. you should never split data into separate arrays like this. This is asking for bugs.
Again, I understand that this is an assignment... A bad one... Where did you find this?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For what it's worth, I don't think you should have a goal of making anything shorter.

You should strive to making it BETTER. That means code that is easy to follow and understand. If that ends up with it being shorter, so be it. But I'd rather have code that is 200 lines and easy to understand than code that is 100 lines and confusing as heck to read or follow.
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:You have the same code in two places (for printing). That is generally bad idea.
My suggestion is. Make one method that prints a single element. It could look like this:

Then you could make method like this to print fifth element:


and this to print all elements:


You could make another method printTitles to print your headers.

I'll repeat myself. This way of solving this task is bad. You should never split data into separate arrays like this. This is asking for bugs.
Again, I understand that this is an assignment... A bad one... Where did you find this?


hah, i said the same thing. it's just something our teacher asked for out of the blue. said write it out on paper then type it in and compile.

so if i wanted to do this differently in the same format i'm assigned to work within, how would i do that?

 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Delete this and forget you ever had this

You posted this several times. I can't look at it anymore

David Borchgrevink wrote:so if i wanted to do this differently in the same format i'm assigned to work within, how would i do that?

I don't understand. Are you asking how to do this assignment without using separate arrays?
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:Delete this and forget you ever had this

You posted this several times. I can't look at it anymore

David Borchgrevink wrote:so if i wanted to do this differently in the same format i'm assigned to work within, how would i do that?

I don't understand. Are you asking how to do this assignment without using separate arrays?


haha sorry i meant how could i have a method that prints the 5th product without having that useless if statement
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Borchgrevink wrote:haha sorry i meant how could i have a method that prints the 5th product without having that useless if statement


Just don't write the if statement.
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
David Borchgrevink wrote:haha sorry i meant how could i have a method that prints the 5th product without having that useless if statement


Just don't write the if statement.


hmm that's weird. that's what i originally tried... maybe my syntax was off or something
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Borchgrevink wrote:hmm that's weird. that's what i originally tried... maybe my syntax was off or something

Maybe too many or too little curly braces?
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
David Borchgrevink wrote:hmm that's weird. that's what i originally tried... maybe my syntax was off or something

Maybe too many or too little curly braces?


entirely possible

i'm just glad that

a: i was able to get the long version working to pass that test (total bs by the way, we've spent 1 day on arrays...2 weeks ago...and we were supposed to know this with closed computers and all? half our class had nothing to hand in at the end)

and b: that i was able to figure out a more efficient authoring but i definitely take into account the theory that the long version while more tedious is easier to read, thus easier debugging
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!