Is there something like a while switch? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I was wondering if there is something that takes in a user input and tests it into preset 'cases' (like a switch) and if there is no 'cases' matching the user input the switch-thing resets (like a while statement). Then it prompts the user for an input and then tests if that matches and if it doesn't it keeps doing this until the input from the user matches one of the cases. I realize that you can do this with a while/if/else combo and am simply wandering if there is a way to do this with a while statement.
Edit:
What I ended up doing is...
String aString = scanner.next();
boolean switchOff = false;
while ( switchOff = false )
{
switch (aString)
{
case "example" : //What I want to happen
switchOff=true;
break;
default: aString = scanner.next();
break;
}
}
Would this work?

You can combine them with
OUTER: while(true) switch(tested) {
case GOOD:
// something
break;
case ALSO_GOOD:
// something
break;
default:
break OUTER;
}

do{ input = askInput(); } while( !match(input) );

Related

How do i exit this while loop [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I'm trying to get this loop down but I don't know how to break from it.
System.out.printf("Please enter your given name and surname (Enter 0 to return to main menu)%n");
String name = sc.nextLine();
while (name.equals("0")) {
System.out.printf(MENU_TEMPLATE);
name = sc.nextLine();
if the user enters their name then the program will carry on as normal, but I'm having trouble doing this.
you use a conditional, when you want to break the loop. Then you use the break command.
like
while (name != Integer.toString(0)) {
if (name == "Salami") {
break;
}
}
another way to break the loop is to use a counter.
int i = 0;
while (i < 10) {
//do some code here.
i++;
}

Wich one is better to use when we ask an user to do it again or finish? In Java [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
We ask to the user if he wants to do it again or finish.
The thing is we let him put [Y] or [N] answer, but can be that he mistakes.
So my problem enters here...
Structure one
//kb = keyboard
Scanner kb = new Scanner(System.in);
//String for the [Y] or [N] answer
String r = "";
//boolean to go out the loop and finish
boolean out = true;
do {
//rest of the code of the program
some stuff
//
System.out.println("Do you want to do it again?\n"
+ "[Y] or [N]");
r = kb.next();
}while(r == "y" || r == "Y");
Structure two
do {
//rest of the code of the program
some stuff
//
System.out.print("Do you want to do it again?\n"
+ "[Y] or [N]");
String answer = kb.next();
switch (answer)
{
case "Y": out = true;
break;
case "y": out = true;
break;
case "N": out = false;
break;
case "n": out = false;
break;
default :
System.out.println("Not a valid option") ;
break;
}//end switch
}while(out);
And how I can do it better if the user answer with another word?
This is really a style question. There's no exact right answer.
The switch is a lot more code, so I'd avoid that.
You can take care of the case issue with
r = kb.next().toUpperCase();
and then use
while ( r.equals("Y") )
since r is a string.

How do I check for an empty string in a Boolean statement? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have an assignment where I have to attach the letters "un" to any word that the user inputs (unless the inputted word already has "un" in front of it, in which case I just return the inputted word). I'm testing my method but I encountered one problem: my program keeps returning an error if I were to test for an empty input. Here is my code:
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter: ");
String input = keyboard.nextLine();
if(input.substring(0,2).equalsIgnoreCase("un"))
{
System.out.println(input);
}
else if(input.equals(""))
{
System.out.println("un");
}
else
{
System.out.println("un" + input);
}
So I wanted to ask how I can test for an empty input/blank string since, evidently, the "" quotations do not work.
There's nothing wrong with checking input.equals("") per-se. The problem is that you have another test beforehand that throws an exception if input is shorter than 2 characters.
There are several ways to solve this, but I'd just simplify things and use startsWith. An empty string doesn't really need a special case of its own - just slap un before it, and you'll get un:
if (input.toLowerCase().startsWith("un")) {
System.out.println(input);
} else {
System.out.println("un" + input);
}
You are having this problem because you are trying to get the substring of string that doesnt have the required length. Put the empty string check first.
if(input.equals("")||input.length==1)
{
System.out.println("un");
}
else if(input.substring(0,2).equalsIgnoreCase("un"))
{
System.out.println(input);
}
else
{
System.out.println("un" + input);
}
If this weren't homework, and the library could be used for other things ( using it in this single purpose may be overkill ), you could use StringUtils.PrependIfMissing().
It does exactly this and handles nulls as well.

In java, how do I check if a string contains only 1 or 0 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Also, how would I prompt the user to try again until they enter something with only 1 or 0 in it?
I realize I must use a for, or a while loop, but I'm not sure what to put as the condition.
I'm trying to have it so the user is prompted to enter something in binary, and if they don't enter something in binary, to be asked again to enter something in binary, and repeated until they do.
Thanks in advance!
You can do this by a simple regular expression matching:
if (inputString.matches("^[01]+$")) {
// accept this input
}
Simply use Integer.parseInt (str, 2);
it will throw a NumberFormatException if not binary
You can inspect every character of the String like so:
String s;//user input
boolean bad=false;//Starts false-will change to true if the input is bad
for(char c:s.toCharArray())
if(!(c=='0'||c=='1')){//if c isn't 0 or 1
bad=true;
break;//break out of loop because we've already found a problem
}
You may want to use the pattern below. The concept is to provide a "regular expression" that provides the rules for a conforming string, along with the message to prompt the user for input and the source to read the user's input from. The loop continues until a conforming string is found, or the user breaks out with "exit". The function would return the conforming string, or a null if the user wants to exit.
public String getConformingString(Scanner source, String message, String pattern) {
String result = null;
boolean isConformingString = false;
System.out.println(message);
String trialString = source.mextLine();
while (!isConformingString) {
if (trialString.matches(pattern) {
isConformingString = true;
result = isConformingString;
} else if ("exit".equalsIgnoreString(trialString)) {
isConformingString = true;
} else {
System.out.println(message);
trialString = source.nextline();
}
}
return result;
}

If else statement with a string compare java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am very new to java code, only ever really used C++ before.
I am trying to compare two string variables and if they match set a new variable to string answer else new variable would be null.
e.g.
if (ID.equals(DeviceID)){
MobileDevice = "BB 9630";
else
MobileDevice = null;
end if
But this does not seem to work when I try it, and I think the logic is correct...any help?
A simpler way to do this is to use a tri-graph, something you can do in C++ as well.
String mobileDevice = id.equals(deviceId) ? "BB 9630" : "unknown";
You should use camelCase for variables in Java.
u need to correct your syntax.
if (ID.equals(DeviceID)){
MobileDevice = "BB 9630";
}else{
MobileDevice = null;
}
Your syntax is just wrong for Java:
if (ID.equals(DeviceID)) {
MobileDevice = "BB 9630";
}
else {
MobileDevice = null;
}
Note the lack of end-if. Also you only need curly braces { } around multiple statements in an if-else block. For example, if you wanted to do two things:
if (a == 1) {
//First action
//Next action
//So on..
}

Categories

Resources