File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to display a particular array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to display a particular array?" Watch "How to display a particular array?" New topic
Author

How to display a particular array?

Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
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

Joined: Mar 17, 2011
Posts: 7674
    
  19

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
Thank you very much sorry for that i just joined
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7674
    
  19

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

Joined: Apr 17, 2013
Posts: 31
what about now
Kenneth Van Gysegem
Ranch Hand

Joined: Mar 30, 2013
Posts: 33
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

Joined: Mar 17, 2011
Posts: 7674
    
  19

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

Joined: Apr 17, 2013
Posts: 31

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

Joined: Apr 17, 2013
Posts: 31
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

Joined: Mar 17, 2011
Posts: 7674
    
  19

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

Joined: Mar 30, 2013
Posts: 33
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

Joined: Aug 11, 2011
Posts: 440

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



Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
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

Joined: Mar 17, 2011
Posts: 7674
    
  19

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

Joined: Apr 17, 2013
Posts: 31
is it like this
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

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

Joined: Mar 30, 2013
Posts: 33
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

Joined: Oct 13, 2005
Posts: 38398
    
  23
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

Joined: Apr 17, 2013
Posts: 31
I can't seem to write anything when it prompts to enter team.
Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
it keeps saying terminated as soon i press 1 on the main menu
Kenneth Van Gysegem
Ranch Hand

Joined: Mar 30, 2013
Posts: 33
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

Joined: Apr 17, 2013
Posts: 31
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

Joined: Mar 30, 2013
Posts: 33
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

Joined: Oct 13, 2005
Posts: 38398
    
  23
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

Joined: Apr 17, 2013
Posts: 31
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

Joined: Mar 30, 2013
Posts: 33
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

Joined: Apr 17, 2013
Posts: 31
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

Joined: Mar 30, 2013
Posts: 33
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

Joined: Apr 17, 2013
Posts: 31
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to display a particular array?