I am having a problem with displaying statement remove or not removed if letter is
r
nlanka 0 Newbie Poster
import java.util.Scanner;
import edu.colorado.nodes.Node;
import edu.colorado.collections.LinkedBag;
import java.util.Iterator;
public class CheeseDemo3
{
public static void showBag(LinkedBag<Cheese> showThisBag, String title)
{
Iterator<Cheese> print;
Cheese printCheese;
System.out.println("\n" + title);
if(showThisBag.size()==0)
System.out.printf("\nEmpty Bag\n");
else
{
print = showThisBag.iterator();
System.out.printf("\n%4s\t%s\n","Type of cheese" , "Exp. year");
while (print.hasNext())
{
printCheese= print.next();
System.out.printf("%4s\t%d\n", printCheese.gettypeOfCheese(), printCheese.getexYear());
}
}
}
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
LinkedBag<Cheese> myCheeseBag = new LinkedBag<Cheese>();
String letter;
String ch;
int ex;
letter= input.nextLine();
if(letter.equals("s"))
showBag(myCheeseBag, "Bag ");
else if(letter.equals("a"))
{
ch = input.nextLine();
ex = input.nextInt();
myCheeseBag.add(new Cheese(ch, ex));
}
else if(letter.equalsIgnoreCase("r"))
{
System.out.println("removed 123");
ch = input.nextLine();
ex= input.nextInt();
myCheeseBag.remove(new Cheese(ch, ex));
}
else if(letter.equals("c"))
{
ch= input.nextLine();
ex= input.nextInt();
System.out.println(" num" + myCheeseBag.countOccurrences(new Cheese(ch, ex)));
}
else if(letter.equals("f"))
{
ch= input.nextLine();
ex= input.nextInt();
Cheese myGrabCheese = myCheeseBag.grab();
System.out.println(myGrabCheese.gettypeOfCheese() + "" + myGrabCheese.getexYear());
}
while(!letter.equals("x"))
{
letter= input.nextLine();
if(letter.equals("s"))
{
showBag(myCheeseBag, "Bag ");
}
if(letter.equals("a"))
{
ch = input.nextLine();
ex = input.nextInt();
myCheeseBag.add(new Cheese(ch, ex));
}
else if(letter.equalsIgnoreCase("r"))
{
ch = input.nextLine();
ex= input.nextInt();
myCheeseBag.remove(new Cheese(ch, ex));
}
if(letter.equals("f"))
{
ch= input.nextLine();
ex= input.nextInt();
Cheese myGrabCheese = myCheeseBag.grab();
System.out.println("found :" + myGrabCheese.gettypeOfCheese() + "" + myGrabCheese.getexYear());
}
}
}
}
Taywin 312 Posting Virtuoso
Next time, please use the CODE tag instead of upload the file. Also, you should post only the method you are having a problem with...
Anyway in main method, you are implementing 2 sets of if-else statements. One is before the while loop and the other is inside the while loop. From what I understand, you do not need the first set of if-else statement (before the while loop). What you need is the while loop and the inside if-else statement; however, you still need to fix what you really want to do inside it.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.