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..
}
Related
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 6 years ago.
Improve this question
enter image description hereI'm trying to use trim to figure out if someone imputed an empty string, and return the response " Say something, please". This is the peace of code:
else if(statement.trim().length() == 0 )
{
response = "Say something, please";
}
To invoke the methods from String, you invoke from the String variable. Not the String class.
You probably wanted:
else if(userInput.trim().length() == 0)
where userInput is the string object you are interested to check whether it is empty.
Similar to what Danny said.
Before your if/else branches you should have a string variable already. Then you simply call trim on that variable.
String s = "Hey this isn't empty!! ";
if(false){
// never runs
else if(s.trim().length() == 0){
response = "Say something please";
}
You need first to create an instance of String
String Str = new String();
Then invocke trim methid
str.trim();
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 am facing a very basic issue with my logic. Basically I have 2 conditions. One is when !quiet and the other is when its !quiet && _cmdLine.isInteractive.
I have tried to put them in an if else block but with the else I get a syntax error, and if I use just if statements or if-else if, it does not work. I have pasted my code below and need some help. I just cant figure out where I am going wrong on this.
if (!quiet) {
String targetName = getPrintoutNameFromStartable(start,
picoName);
System.out.print("message here");
}
if (_cmdLine.isInteractive()) {
System.out.println("a different message");
return 1;
}
Hope this Helps you understand how to handle two different flags.
if (!quiet) {
String targetName = getPrintoutNameFromStartable(start,
picoName);
System.out.print("message here");
if ( _cmdLine.isInteractive()) {
System.out.println("a different message");
return 1;
}else{
//do something
}
}else{
if ( !_cmdLine.isInteractive()) {
System.out.println("a different message");
return 1;
}else{
//do something
}
}
I think what you are looking for is this:
if (!quiet && _cmdLine.isInteractive()) {
// ...
} else if (!quiet) {
// ...
}
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.
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;
}
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) );