Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a sub menu using String/char?
    text
    copied!<pre><code>public static String getSubMenu(String submenu){ Scanner keyboard = new Scanner(System.in); String chosen="", A="A",B="B", a="a", b="b"; do{ chosen = keyboard.next(); keyboard.nextLine(); System.out.print("\n\n"); }while(chosen.compareTo(A)); return chosen; } //This function below is fine. public static void Menu(){ String unem=""; do{ System.out.println("Sub Menu"); System.out.println("Select an Option\n\n"); System.out.println("a.Sort by name\n" + "b.Sort by time\n" + "c.Exit sub-menu\n\n"); System.out.print("Input the number for the selected option: "); unem= getSubMenu(unem); if("a".equals(unem)|| "A".equals(unem)){ } if("b".equals(unem)|| "B".equals(unem)){ } }while ("a".equals(unem) ||"b".equals(unem) || "A".equals(unem) || "B".equals(unem)); } } </code></pre> <p>Hi, I'm trying to make a sub menu. As you can see in the function <code>Menu</code>, when <code>getSubMenu</code> is called the user has to input a selected option in the function <code>getSubMenu</code>. I looked through my textbook and online and it doesn't seem you can use <code>char</code> within arguments such as </p> <pre><code>char a="a"; if(a != b); </code></pre> <p>If you can use characters instead of strings in the functions above please tell.</p> <p>But moving on. What I am trying to do now is to get <code>getSubMenu</code> to return a String containing either <code>'A' || 'a' || 'b' || 'B' || 'c' || 'C'</code> and then loop when the user does not put any of these as an input. I've tried attempting to use <code>compareTo</code> but I receive a <code>Type mismatch: cannot convert from int to boolean error</code> how can I improve on this. What syntax can I use so that this can work.</p> <p>Thanks for everyone who will help and contribute to this.</p> <p>EDITED: NEW WORKING FUNCTION</p> <pre><code> public static String getSubMenu(String submenu){ Scanner keyboard = new Scanner(System.in); boolean looped = true; String chosen=""; do{ chosen = keyboard.next(); keyboard.nextLine(); System.out.print("\n\n"); if("a".equals(option)|| "A".equals(option) || "b".equals(option)|| "B".equals(option) || "c".equals(option)|| "C".equals(option)){ looped = false; } else System.out.println("Wrong input"); }while(looped); return option; </code></pre> <p>It may of not been what I was aiming for but it still did it job.</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