Note that there are some explanatory texts on larger screens.

plurals
  1. POI stuck with get out from while loop in method
    text
    copied!<p>I am writing program that asks user to type seven names of product.</p> <p>what I try to do is if there is duplicate, then repeat the method.</p> <p>I used while loop but I stuck.</p> <p>If I put a,b,c,d,e,f,g at the first time, method ends and moves on to next method.</p> <p>But if I typed a,a,b,c,d,e,f, program repeats same method and even if I type a,b,c,d,e,f,g, it gets in infinite loop.</p> <p>here is my codes.</p> <p>in main....</p> <pre><code> purchasedList.setShopList(); </code></pre> <p>in purchasedList class...</p> <pre><code> public void setShopList() { Scanner keyboard = new Scanner(System.in); // print out description. System.out.println("\n- Only have one entry of any type in the item list."); System.out.println("- The name of items cannot be longer than 16 characters."); System.out.println("\nType seven products."); boolean sameNames = true; while (sameNames == true) { for (int i=0; i&lt;7; i++) { String n = keyboard.nextLine(); name.add(n); name.set(i,name.get(i).toUpperCase()); } sameNames = checkName(); } } // accessor. public ArrayList&lt;String&gt; getShopList () { return name; } // check duplicate. public boolean checkName() { Set&lt;String&gt; uniqueName = new HashSet&lt;String&gt;(); boolean foundName = false; for (int i=0; i&lt;7; i++) { if (!uniqueName.add(name.get(i))) { // check duplicate foundName = true; } } if (foundName == true) { System.out.println("※ There is a duplicate. Try it again."); return true; } else { return false; } } </code></pre> <p>my checkName() method is fine because in my last project it worked.</p> <p>in my last project, I put while loop in main like this</p> <pre><code> public static void main(String[] args) { PurchasedList purchasedList = new PurchasedList(); . . . boolean sameNames = true; boolean tooLong = true; while (sameNames == true || tooLong == true) { System.out.println("\nType seven products."); purchasedList.setShopList(); sameNames = purchasedList.checkName(); tooLong = purchasedList.checkLength(); } </code></pre> <p>but this time, because my professor wants me to make all operations are done within a method, so I try to fix.</p> <p>I tried to solve it by myself in last 8 hours, but I could not get the solution.</p> <p>Please help me.</p> <p>Thank you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload