• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help searching through array and printing to screen?

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Waria Ahmed:
What is the best way of fixing this?



Fixing what ? What did you want it to do ?
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone,

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

Thanks again.

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


line 35 is


line 340 is



basically shopAdmin.java has a main() which runs the in shop.java.
 
Waria Ahmed
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK i think i have fixed the problem,

Basically i had a array of 350 shops, and only used 135 of them.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic