Using answer in if [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I ran into this problem. This is my code:
String answer;
System.out.println("Choose a day");
answer = tastatur.nextLine();
if(svar.equals("saturday"))
System.out.println("Saturday");
I want to use the answer in the if statement.

You are looking at the wrong variable.
if( answer.equals("saturday") ){
System.out.println("Saturday")
}
What's the logic?
You are reading what the user is entering into the answer variable. In your code you are checking the svar variable while you should be checking the answer variable. If indeed the user entered Saturday, you will print it on the screen. Else, not.
equalsIgnoreCase() is a better method to use because Saturday and saturday are different in the sense that one has a capitalized S and the other does not. equals() will treat them as different.
You have said that Java is new to you. I hope you are initializing the tastatur variable properly as:
BufferedReader tastatur = new BufferedReader(new InputStreamReader(System.in));
and using the import java.io.* in your program.
As an aside, please read the official tutorials provided by Java. They are a good resource. Ask questions on StackOverflow when you have run out of luck while looking for answers yourself. With that said, welcome to StackOverflow where we very mercilessly close questions if they do not meet our standards or contain spam.

Related

How do I find the most similar string from list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I have a list of strings in random format:
AppName-ver-1.1.0-data.exe
AppName-ver-1.1.1-secondData.exe
AppName-ver-1.2.0-data.exe
AppName-ver-1.2.1-data.exe
AppName-ver-1.2.3-data.exe
AnotherAppName-ver-1.0.0-data.exe
AnotherAppName-ver-1.0.0-secondData.exe
What would be an efficient way in java to find the closest value to string:
AppName-ver-1.2.4-data.exe
UPD: closest - by the naming not length so AppName-ver-1.2.3-data.exe is the expected result
To emphasize what commenters already pointed out:
If you define 'closest' to be the string length, then
AppName-ver-1.2.4-data.exe has the value 26, and
AppName-ver-1.1.0-data.exe
AppName-ver-1.2.0-data.exe
AppName-ver-1.2.1-data.exe
AppName-ver-1.2.3-data.exe
all resemble 26 as well so they are a direct match.
You could also define 'closest' to have the least Hamming Distance. This will give completely different results and AppName-ver-1.2.0-data.exe might win as it is just one bit off.

do/while loop not working in Java with functions [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
In this code, the user types in yes if they would like to play again
I have to use functions for this code, I cannot simply state while(again.equals("yes"));.
Part of code:
do{
System.out.print("Play Again? Yes/No: ");
String again=keyboard.nextLine();
boolean running=playAgain(again);
} while(running == true);
My question is why is }while(running==true); a syntax error? I declared the variable above it, shouldn't that allow it to run?
You need a "do" statement first, i.e. do { statements; } while (condition). Also
you don't need to save the return from playAgain() into a variable, you can call it directly from the while().
There are many Java tutorials out there, try the official Oracle ones from starters: The while and do-while Statements
Update
String again; // <== declared here because conditions inside
// while cannot see variables defined inside the do {} block
do {
System.out.print("Play Again? Yes/No: ");
again = keyboard.nextLine();
} while (playAgain(again));

How will this work in Java? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I compiled the following code in java and it compiled!
How?
And when i run it it just runs without any output!
Why did it compile?
public class Check{
public static void main(String args[])
{
for(int i=0;i!=0;i++)
System.out.print(i);
}
}
Indeed the program "does nothing" as far as the user can see. But the instructions(code) you have given to the compiler does in fact have valid statements in it that are syntactically correct. If there are semantic errors in your program, that is up to us as programmers to sort out. This may be considered a bug - but it is one that us, the coders must find and fix - not the compiler.
Hovercraft full of eels' comment on your post explains nicely why there is no output.

distinguishing cases to use interger.parseInt and string casting methods [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am developing a Java program and I'm meeting cases where I get undecided whether to use the casting a string to integer method, or to use the integer.parseInt method. Is there any clear benefit for either of the two methods?
With 'casting to string method', I mean:
String.valueOf(integer);
As far as I know, it's not possible to cast from a String to an int, so using Integer.parseInt seems like the best option here.
Looking at your edits about using valueOf, perhaps this link may help: Integer.valueOf() vs. Integer.parseInt()

Dead code in Java/Android [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
is there a way to tell the compiler in Java or Android to not remove some statements in code -which are intended to clean up variables after use to prevent any data remnant in ram-??
would creating a dummy method solve this issue??
these statements basically set the variables to their type-based initial values..
Thanks in advance!
The code that you describe is not dead code.
Dead code is code that will never execute.
Here is an example:
private int secretSchmarr;
public boolean blammo()
{
boolean returnValue;
secretSchmarr = calculateSecretValue();
returnValue = useSecretValue(secretSchmarr);
secretSchmarr = 99; // this is not dead code.
return returnValue;
secretSchmarr = 98; // This is dead code because it can never execute.
}
I answer under the odd assumption that you have a good reason to believe that the code is still useful even though it is dead.
Store the value false in some obfuscated form that the compiler can't understand. Then, conditionally branch to that code using your obfuscated value. The compiler will not know it is dead, so it will not be removed.
I'll use a file for my example, but it is probably not the most efficient way. Say your code that the compiler thinks is dead code was in a function called myCode(). Assume that fin is reading from a file that only contains false followed by EOF
if(Boolean.parseBoolean(fin.next()))
myCode();

Categories

Resources