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) {
// ...
}
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 have this:
for(String s : names){ //names is an ArrayList of strings
if( s.equals("bob") ){
//do sth and then break from the loop
break;
}
}
When the condition inside the if is true, I would expect the for-loop to break. But it doesn't.. what I code wrong?
EDIT:
the problem was that I have an extra for loop in my code
for( //a loop here){
for(String s : names){ //names is an ArrayList of strings
if( s.equals("bob") ){
//do sth and then break from the loop
break;
}
}
}
that's why the inside loop was executing after the break...
Code looks fine, there is some data issue in your list or difference of case in two strings . Try using equalsIgnoreCase instead of equals as suggested
It just prints hi, as soon as bob comes it breaks.
public static void main(String[] args) {
List<String> names = new ArrayList<String>();
names.add("hi");
names.add("bob");
names.add("bye");
for (String s : names) {
if (s.equals("bob")) {
System.out.println("breaking...");
break;
} else {
System.out.println(s);
}
}
}
output
hi
breaking...
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 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) );
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..
}