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 for if else + Output Problem (array) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "for if else + Output Problem (array)" Watch "for if else + Output Problem (array)" New topic
Author

for if else + Output Problem (array)

Misa Shiratori
Greenhorn

Joined: Nov 22, 2011
Posts: 3
Hi! I'm a newbie at Java.
I have to complete this assignment (no, I'm not asking for a complete answer). I don't know why it won't show the proper output that I want.
Heck, I don't even know what I'm doing anymore lolz...

Here's my code. (My question's in it too)



My problem lies in the 'printCDsLessThan999' method. The rest is perfectly fine and gives out the output that I wanted.
I've tried using 'else if' and nesting but it just screws up my coding even more.

I've tried Google-ing for some hints for a solution to my problem but I don't know which keyword should I input. ;__;

Any advice?? Please help!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Please TellTheDetails. What are you expecting, and what's happening instead?

Also, I don't know what this line is supposed to be for:

but it just looks oh so wrong: The hardcoding of the 5 array elements, the hardcoding of the prices, the whole logic of "if any one of these prices is less than some arbitrary amount specific to that price..." The code may "work", but it's certainly not a good way to do whatever it is you're trying to do.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Hi Misa, Welcome to JavaRanch,
What is the problem you are trying to solve? And what is the expected output.

Moreover the way you have been using the array is not advisable. Instead you need to be using a advanced for loop OR for-each loop to loop through arrays or Collections.


Mohamed Sanaulla | My Blog
Misa Shiratori
Greenhorn

Joined: Nov 22, 2011
Posts: 3
@Jeff Verdegan: I tried commenting out that line, it still works. So, I'm not entirely sure of it either... sorry.

@Mohamed Sanaulla: I haven't learnt how to use Collections yet. I'll research more about the for loop and for-each loop and, see what can I do.

I hope to get an output, as an example, like this...

Please enter an artist's name to check the price.
(User inputs either one of the artist listed in the 'artist' array list. If the cost of the CD is more than 999, it'll show an error message. If the cost is less than 999, then it'll show the details of the CD.)

I'd hope to let it show the (almost) same output as in the 'printArtistsCDs' method. The example output from the 'printArtistsCDs' method is like this (below).

Please enter an artist's name.
Arcade Fire
Album Title: Neon Bible Price: 949

=========================================

But what I've been getting is like this...

Please enter an artist's name to check the price.
Black Eyed Peas
Done!
Done!
Something's wrong.

=========================================

I hope I explained properly. ><
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

If you can't see the problem by reading your code, you need to do some debugging. Add println() statements so you can see what's happening.

Here's something to get you started:


EDIT: I'm not sure why you're not seeing the "Artist might not be in the database" line, and I don't see any "Done" printouts there. I'm not convinced you're running the code from your first post.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Additionally, using parallel arrays for artist/title/cost is absolutely the wrong way to go about it. It totally defeats one of the main points of OO programming. You need to define a class, say, CD, and give that class member variables artist, title, and cost, and then create one single array of CDs.
Misa Shiratori
Greenhorn

Joined: Nov 22, 2011
Posts: 3
Ah, there's actually 3 different methods in my coding.
The first one ('printCDs') is to print out a table of the artist, album title and the cost.

The second one ('printArtistsCDs') is to ask the user to input an artist's name from the list and after the user inputs the artist's name, the output will show the info of that certain artist. If the user enters the wrong artist's name, it'll print out "Artist might not be in the database. Please re-check your spelling and capitalization. Then, try again."

The third one ('printCDsLessThan999') is the problematic one. The user is suppose to enter the artist's name when asked. If the cost of the CD is more than 999, an 'error message' is suppose to show. But if the cost of the CD is less than 999, then it'll print out the artist's CD info.

Here's the screeshot of my output. I explained on the screenshots too, to avoid more confusion.





I'll be changing those output message ("Done!" and "Something's wrong.") to a different output message once I get this program running smoothly. The output message ("Done!" and "Something's wrong.") is just temporary.

Jeff Verdegan wrote:EDIT: I'm not sure why you're not seeing the "Artist might not be in the database" line, and I don't see any "Done" printouts there. I'm not convinced you're running the code from your first post.


This is the 'error message' for the second method (from 'printArtistsCDs').



When I don't enter any of the artists that's on the list (eg: I enter "Darren"), it'll show an 'error message':
Artist might not be in the database.
Please re-check your spelling and capitalization.
Then, try again.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Ok, so let me write down the steps for printCDsLessThan999 method:

1. Take input -artist name
2. Find the index of the artist name in the artist array.
3. Using this index find the cost of the album from the cost array. (I am assuming that the artist, title and cost have one-to-one correspondence between each other)
4. Now check that this cost is less than 999, if yes then print the name of the artist/title.

Alternate way would be to:
1. Define a class Album which would have artist, title and cost attribute.

2. Now you can create a- Album[] array or a List<Album>
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

There's a lot that needs to be fixed before even trying to address the behavior you're asking about.

First, as I think somebody else alluded to, this makes no sense:



The whole point of the for loop is that you don't have to explicitly enumerate each array index, or even know how many there are. It should look like this:



Additionally, as I suggested, you need to create a class to encapsulate the Artist/Title/Cost combination, since they logically go together to form one entity in your domain model.

Gerald Lam
Greenhorn

Joined: Aug 23, 2010
Posts: 6
You're getting Something's Wrong because,

cost[2] is 1099 while your costPrice is 999.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for if else + Output Problem (array)