permaculture playing cards*
The moose likes Beginning Java and the fly likes Help searching through array and printing to screen? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help searching through array and printing to screen?" Watch "Help searching through array and printing to screen?" New topic
Author

Help searching through array and printing to screen?

Waria Ahmed
Ranch Hand

Joined: Jul 09, 2008
Posts: 56
Morning fellow ranchers,

I have the following code�


What the code does is it asks user for shop number, and loops through the array checking each position whether the users input equals the array, and for every position that doesn�t equal the users input, it prints out shop not found.. so for example, if I typed in 6, and the array was of 10 shops, it would print the following.

Shop not found
Shop not found
Shop not found
Shop not found
Shop not found
6. SHOP 6
Shop not found
Shop not found
Shop not found
Shop not found

What is the best way of fixing this?

Thanks in advance
[ August 12, 2008: Message edited by: Bear Bibeault ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
Originally posted by Waria Ahmed:
What is the best way of fixing this?


Fixing what ? What did you want it to do ?


Joanne
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Its not clear to me what is your question.

And the loop you mentioned should be doing the desired work,I think.

for (shopIndex = 0; shopIndex < 350; shopIndex++)


By the way its not really a good idea to use a hard coded constant for the loop. You may end up with array index out of bounds exception and hard to maintain code.

Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
That loop is doing exactly what you told it to. You told it to go through all the shops and print out "shop not found" unless you find the right index. And Amit Ghorpade is correct about not using 350 for the count; that assumes the array will always have 350 members.
Waria Ahmed
Ranch Hand

Joined: Jul 09, 2008
Posts: 56
Sorry, i should have been more specific.

I dont want it to print shop not found for each position, id rather it just returned the right shop details only.

Anyhow i can change the outcome?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Try changing the if-else to a straight if.

I presume the shop number and the [index] are different numbers; if they are the same, then the linear search would be unnecessary.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
And if you want to print 'shop not found' if you don't get a match, then one way is to initialise the string to 'shop not found', change it if you find a match and then print the string after the loop.
Something like
Waria Ahmed
Ranch Hand

Joined: Jul 09, 2008
Posts: 56
Thanks everyone,

I have used the following code and it did the trick... works perfect now.

Thanks again.

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
I realise you are a beginner so please take this as constructive critiscism aimed at preventing you picking up bad habits early on. That is a horrible piece of code. Never use exceptions to control program flow like that. Search these forums and Google for discussions on when exceptions should be used. There are many opinions but I'm sure that nobody would ever suggest that this is a good use of them.

For a start there is a bug just waiting to happen - consider the case where you have 350 shops - you won't get any output.
Also, as was mentioned earlier you should not hard code 350. Use greggs.length instead. Of course, if you do this, your code will no longer work. You could get around it using a different exception (I'm not going to say how because that would still be horrible code), but it is far better to use something like my previous post.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

For a start there is a bug just waiting to happen - consider the case where you have 350 shops - you won't get any output.

Yes Joanne is right.And you should consider this if you dont want to write buggy programs.

A very typical use of exceptions is to report problems that weren't caused by the program itself but other dependencies like IO.
Exceptions like the NPE should not be caught instead we should check if a reference is null before manipulation.

Your program will certainly cause the array index exception, which can be avoided easily otherwise.

Using exceptions to control program flow is really bad practice.Since you never know when an exception occurs.


Hope this helps
Waria Ahmed
Ranch Hand

Joined: Jul 09, 2008
Posts: 56
Thanks guys, i have took on board all the critisicm, and now the code looks like this...



But now, when I put in a wrong shop number, i get a nullpointerexception.

Is there anything else i need to know?

Thanks again for the earlier points, I'm learning as i go along
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
NullPointerException?

Where? The stack trace will give details.

You need to make sure the greggs array, and every member of it, all point to real objects, not to null. If the array itself is null, or if any of its members is null, you will get a NullPointerException.
Waria Ahmed
Ranch Hand

Joined: Jul 09, 2008
Posts: 56


line 35 is


line 340 is



basically shopAdmin.java has a main() which runs the in shop.java.
Waria Ahmed
Ranch Hand

Joined: Jul 09, 2008
Posts: 56
OK i think i have fixed the problem,

Basically i had a array of 350 shops, and only used 135 of them.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Originally posted by Waria Ahmed:


line 35 is


line 340 is



basically shopAdmin.java has a main() which runs the in shop.java.
It's line 35 you want; I see you have found it. The line where the problem occurs is printed first in the stack trace.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
If you don't have exactly the same number of elements as the length of your array, it sounds like a job for a List<Shop> rather than an Array.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Help searching through array and printing to screen?
 
Similar Threads
Method: Return String Array position
JOptionPane and ArraySize not known
How can I check if a particular value is present in an Array using assertTrue ??
Error trying to print array to screen through toString()..
How to do find the braces?