Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to display a particular array?

 
Sujath Mohammed
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you guys help with this so basically this program would ask the user to search for a team and then the program should display that team and the years they won the competition.
Here's what i have done:
 
Winston Gutkowski
Bartender
Pie
Posts: 10084
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sujath Mohammed wrote:Could you guys help with this

Sujath: Please DontWriteLongLines (←click);
it makes your thread incredibly hard to read.

You can edit your post with the 'Edit' button.
(I'd do it myself, but your lines are massive).

Thanks.

Winston
 
Sujath Mohammed
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much sorry for that i just joined
 
Winston Gutkowski
Bartender
Pie
Posts: 10084
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sujath Mohammed wrote:Thank you very much sorry for that i just joined

Fraid it's still too long (at least on my screen). 80 characters. That's about 10 years, or 4-5 strings, per line.

Thanks.

Winston
 
Sujath Mohammed
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what about now
 
Kenneth Van Gysegem
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

maybe I can help.
Every String in the winners-Array has an index, so I think you can try to write a for loop like:


so that for every index i that has as value "South" in the winners array, you print the value for index i in the years array.

I hope this helped.
Kenneth
 
Winston Gutkowski
Bartender
Pie
Posts: 10084
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sujath Mohammed wrote:what about now

You missed out the comments. I've done it for you this time. See how much better it looks?

Winston
 
Sujath Mohammed
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But the thing is the user is gonna enter a team i can't just put a default team
i basically want a code where if a user enters "Souths" it should display the team souths and the years they won
 
Sujath Mohammed
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Sujath Mohammed wrote:what about now

You missed out the comments. I've done it for you this time. See how much better it looks?

Winston


Thank you
 
Winston Gutkowski
Bartender
Pie
Posts: 10084
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sujath Mohammed wrote:But the thing is the user is gonna enter a team i can't just put a default team
i basically want a code where if a user enters "Souths" it should display the team souths and the years they won

OK, well just substitiute the name that the user entered (which you've presumably stored in a field) every place you see "South".

Winston
 
Kenneth Van Gysegem
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
OK, then you can use the import java.util.Scanner;

you just give the input a name like:

then you change my previous code."South" changed into team.
You then however should write a test for the input. Because you want it to be one of the choices in the array winners.
If the user would, t for instance, input a digit, you'll get an error.

I hope it clarifies things for you,
Kenneth
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sujath,
You mentioned that both the arrays are parallel to each other ? I am thinking you meant that both contain data for each other at the parallel index ? Like for the team at winner[3] the year of win is infact years[3] . So instead of using 2 arrays and managing them , why not make a map with your key as the years and value as the winners as below

 
Sujath Mohammed
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saif Asif wrote:Hello Sujath,
You mentioned that both the arrays are parallel to each other ? I am thinking you meant that both contain data for each other at the parallel index ? Like for the team at winner[3] the year of win is infact years[3] . So instead of using 2 arrays and managing them , why not make a map with your key as the years and value as the winners as below



Thats gonna leave me with heaps of map key
 
Winston Gutkowski
Bartender
Pie
Posts: 10084
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sujath Mohammed wrote:Thats gonna leave me with heaps of map key

So? It's only one Map, which is way better then using multiple arrays. Furthermore, you can load it with a loop like Kenneth suggested, viz:Winston
 
Sujath Mohammed
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it like this
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sujath Mohammed wrote:
Saif Asif wrote:Hello Sujath,
You mentioned that both the arrays are parallel to each other ? I am thinking you meant that both contain data for each other at the parallel index ? Like for the team at winner[3] the year of win is infact years[3] . So instead of using 2 arrays and managing them , why not make a map with your key as the years and value as the winners as below



Thats gonna leave me with heaps of map key


Its going to be far better to use one map as compared to handling multiple arrays and looping through them. And whats wrong with having heaps of map keys ? They are useful for retrieving the values from the map rather than looping and comparing each value individualy.
 
Kenneth Van Gysegem
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
That has the outcome you wanted.
You should change:

Otherwise you'll always print "South won in: " for every input of the user.

You should, however, consider to follow Winstons advice.
I think it's better coding, easier and faster to use a Map.
It's a bit more difficult (I struggled with it quite a bit myself), but worth the effort in the end.

Kenneth
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have only just noticed you are running parallel arrays. I am getting déjà vu; I seem to have written a lot about parallel arrays recently, here, here and here. Why is everybody using parallel arrays all of a sudden? Why do I keep saying they are such a bad idea?
 
Sujath Mohammed
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't seem to write anything when it prompts to enter team.
 
Sujath Mohammed
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it keeps saying terminated as soon i press 1 on the main menu
 
Kenneth Van Gysegem
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,

I tested your code, only I used if(opt==1){...}
I don't know if you specifically need the break?

I think that I know why you are running into problems, first of all you use the same scanner object for both inputs.
Try making a different scanner object for the second input and test your code with this adaptation.

Also problems might occur if you, for instance, type (as input) "souths"in stead of "Souths".
Thats why you best make a piece of code that makes the first letter of the input a capital letter.
And provide your if with an else that askes for an other input if the input doesn't match the possibilities.

Regards,
Kenneth
 
Sujath Mohammed
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kenneth Van Gysegem wrote:Hi again,

Also problems might occur if you, for instance, type (as input) "souths"in stead of "Souths".
Thats why you best make a piece of code that makes the first letter of the input a capital letter.
And provide your if with an else that askes for an other input if the input doesn't match the possibilities.

Regards,
Kenneth

I did the if statment but it seems to repeat the statement invalid how do i make it only say it once and ask the user the enter a proper team again?
 
Kenneth Van Gysegem
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,

with this response i posted the code i tried. This works.
I will later look at your code again, but I have a test Java Fundamentals today so I don't have much time.

I hope this helps you a bit further
regards,
Kenneth


 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kenneth Van Gysegem wrote: . . .
I don't know if you specifically need the break?
. . .
If there is only 1 case inside a switch statement, then the break; will not change anything. It is still good practice to write break; however, because that prevents unintended fall-through when a second case is added.

What good a switch statement is if it only has one case, I shall leave to the reader to work out.
 
Sujath Mohammed
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Kenneth Van Gysegem wrote: . . .
I don't know if you specifically need the break?
. . .
If there is only 1 case inside a switch statement, then the break; will not change anything. It is still good practice to write break; however, because that prevents unintended fall-through when a second case is added.

What good a switch statement is if it only has one case, I shall leave to the reader to work out.


This is only one part of the case if you see the main menu i need 3 more cases the second case i can handle its only the first and the third case i need help with
 
Kenneth Van Gysegem
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

what i meant to say was do you need the switch ?
for example:


in this case you need the switch because it will print everything from 30, so case 30 and case 20.
If you just want to select an option, maybe it's easier to just use the if()-statement.
if (input==1){ code}
if (input==2){code}
etc.

Regards,
Kenneth
 
Sujath Mohammed
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kenneth Van Gysegem wrote:Hi,

what i meant to say was do you need the switch ?
for example:


in this case you need the switch because it will print everything from 30, so case 30 and case 20.
If you just want to select an option, maybe it's easier to just use the if()-statement.
if (input==1){ code}
if (input==2){code}
etc.

Regards,
Kenneth


Well in this program i am having a main menu with four options so i need the break statement with the cases so it doesn't run all the cases meaning all the menus
 
Kenneth Van Gysegem
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, i understand.
But why not use the for()-statement then?
if you say if(input==1), if the input is 1 it will compile the code after the if.
If it's not, the code after the if statement will not compile and just move on to the next if.

Do you get what I'm trying to say?
Kenneth
 
Sujath Mohammed
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kenneth Van Gysegem wrote:ok, i understand.
But why not use the for()-statement then?
if you say if(input==1), if the input is 1 it will compile the code after the if.
If it's not, the code after the if statement will not compile and just move on to the next if.

Do you get what I'm trying to say?
Kenneth


Because in my program there's gonna be a few if statements under the cases and i personally like having my program organised to many if statements under a if statements is not a really good way of doing java
or any other programming.And lastly after the the user exits one user case the program should direct them back to the main menu where the user can choose to continue using the program or terminate it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic