The Artima Developer Community
Sponsored Link

Java Answers Forum
basic search

2 replies on 1 page. Most recent reply: Dec 11, 2002 1:31 PM by Sharif

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Sharif

Posts: 2
Nickname: sharif
Registered: Dec, 2002

basic search Posted: Dec 11, 2002 10:27 AM
Reply to this message Reply
Advertisement
hey all,
im trying to create a basic search of part numbers

i have managed to create a program but it can only search for one part number, when i try to add an extra "if" statement it only recognises the last part number, all the other numbers get the reply, that they are both instock and out of stock

here is my code for one part number

public class stockcalc
{
public static void main(String[]args)
{
double input;

System.out.println("Enter a part number");
input=KBInput.readDouble();

if(input==3)
System.out.println("Instock ");

else
System.out.println("OutofStock");
}
}



public class stockcalc
{
public static void main(String[]args)
{
double input;

System.out.println("Enter a part number");
input=KBInput.readDouble();

if(input==3)
System.out.println("Instock ");

else
System.out.println("OutofStock");
}
}

here is my code for more part numbers
public class stockcalc
{
public static void main(String[]args)
{
double input;

System.out.println("Enter a part number");
input=KBInput.readDouble();

if(input==3)
System.out.println("Instock ");

if(input==4)
System.out.println("Instock");

else
System.out.println("Outof Stock");
}
}


the result for 4 is correct showing:Instock
the output for 3 is incorrect showing:
Instock
Outofstock

i dont know how to correct it, any help would be an added bonus

thank you


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: basic search Posted: Dec 11, 2002 12:37 PM
Reply to this message Reply
The problem is that it is falling through all the if statements. You can fix this by adding an else before the second if, but really in this case it would be better to use a switch. In fact, it would be even better to have a separate search method, that way the search can be generalized later, to perhaps look in a database or some other source.

By the way, it is generally not a good idea to compare floats or doubles to ints, unless you round or truncate (depending on your purposes) the floating-point number first. In this case, in particular, since you are using "part numbers" these would almost always be entirely digits (allowing you to read it in as an int, or BigInteger, if the are really long) or a mix of numbers and digits (in which case, you'd probably use a String).

Sharif

Posts: 2
Nickname: sharif
Registered: Dec, 2002

Re: basic search Posted: Dec 11, 2002 1:31 PM
Reply to this message Reply
thanks alot, couldnt believe it was just a question on adding at extra else, i had tried using an array and then searching that, but im new to this so i settled for the easy

thanks again

Flat View: This topic has 2 replies on 1 page
Topic: Writting with C++ Previous Topic   Next Topic Topic: Write

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use