Java if statements with user input (beginner) [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
This is just something im trying out, i just recently started coding in java would like some help. Would like to ask user to pick their favorite movie, then take their input and use if statements to give different responses to each response.
import java.util.Scanner;
public class miniFFF {
public static void main (String[]p) {
System.out.println("What is your favourite movie? pick from the answers below:");
System.out.println("a");
System.out.println("b");
System.out.println("c");
System.out.println("d");
System.out.println("e");
answer();
//ifStatements();
System.exit(0);
}
public static String answer() {
String favMovie;
Scanner test = new Scanner(System.in);
favMovie = test.next();
if(favMovie == "a") {
System.out.println("1234");
}
else if (favMovie == "b") {
System.out.println("123");
}
return favMovie;
}
}

Using switch will make your code much readable.
switch (favMovie){
case "a": //do for a
break;
case "b" //do for b
break;
default: //no match
}
Java enum was designed for when you have a finite set of options. As good practice it will be a better option over string literal

I don't really see a question there and your code seems, I could point two thing I might do differently tho.
One I would use equalIgnoreCase instead of == in case someone doesn't put the correct capitalization (Also add a general case)
Also putting the method outside main seems to be better practice.

Related

Java - code is executing in 2 switch cases [duplicate]

This question already has answers here:
Switch statement just returning the last case
(4 answers)
Closed 3 years ago.
I'm debugging my application in IntelliJ and it seems like my code is going into 2 switch statements instead of 1.
My code looks like this:
final String CASE_1 = "case_1";
final String CASE_2 = "case_2";
switch(CASE): {
CASE_1: {
// do something
}
CASE 2: {
// do something
}
}
My application executes the code that's in both cases.
Is there any reason why the code would go into both cases?
(I'm intentionally not pasting my actual code since it's impossible to reproduce locally without creating the whole structure of the project)
Put a break after each case, otherwise it forwards to the next case.
public class Main {
public static void main(String args[]) {
final String CASE_1 = "case_1";
final String CASE_2 = "case_2";
String CASE = "case_1";
switch(CASE){
case CASE_1:{
System.out.println("Case 1 Found");
break; //Put a break here if you don't want the code to execute for the next case as well.
}
case CASE_2:{
System.out.println("Case 2 Found");
}
}
}
}
Because you dont end the case
public void runTest(String CASE) {
switch (CASE) {
case CASE_1:
// code block
break;
case CASE_2:
// code block
break;
default:
break;
}
}

how to check if the variable is null? [duplicate]

This question already has answers here:
Check whether a String is not Null and not Empty
(35 answers)
Closed 5 years ago.
I know this doesnt work but how can I know if the user added even one char?
public class Program
import java.util.Scanner;
{
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
String b = a.nextLine();
//I know this doesnt work but how can I know if the user added even one char?
if (b!=null){
System.out.println(b);
}
}
}
you can use .equals("") or .isEmpty()
check check if the variable is null
You can do this
if (!b.isEmpty()) {
System.out.println(b);
}

Scanner library issue in java

Why if condition become false value even if the ans is hell. Is there something wrong with the program or what. I am using blueJ for java.
import java.util.Scanner;
public class QuizContest
{
Scanner value = new Scanner(System.in);
public void Contest()
{
System.out.println("Please type- hell");
String ans=value.next();
if(ans=="hell")
{
System.out.println("Congratulation. You are right");
}
else
{
System.out.println("You are wrong");
}
}
}
Comaparing string in java should be done with equals(), not ==
change:
if(ans=="hell")
to:
if(ans.equals("hell"))
When you are comparing with ==, you expect both arguments to be the exact same instance in memory. this works well with primitives, but not with objects like String

switch statement and how to represent a block of statements? [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 writing a program involving ArrayList.
The program is about editing the ArrayList through user input:
ArrayList<Type> array = new ArrayList<Type>();
switch (verify)
{
case 1:
//A list of statement including variables, conditionals and loops
break;
case 2:
//Another list of statement here includes variables, loops etc
break;
default:
do something else;
}
My question is that I don't want to put a lot of statement in a single case, is it possible for me to separate them and redirect the Java to read blocks of statement from somewhere else?
Use methods and pass the ArrayList to them:
public static void main(String args) {
ArrayList<Type> array = new ArrayList<Type>();
switch (verify) {
case 1:
case1(array);
break;
case 2:
case2(array);
break;
default:
do something else;
}
}
private static void case1(ArrayList<Type> array) {
//A list of statement including variables, conditionals and loops
}
private static void case2(ArrayList<Type> array) {
//Another list of statement here includes variables, loops etc
}
The ArrayList is a mutable type, so any changes you make to the list within the case1() and case2() methods affect the ArrayList in your main method.
Use functions!
public class Main {
public static void main(String[] args) {
//snip
ArrayList<Integer> array = new ArrayList<Integer>();
switch(verify) {
case 1: doAThing(array);
break;
case 2: doSomethingElse(array);
break;
default: doAThirdThing(array);
}
}
private static void doAThing(ArrayList<Integer> array) {
//do logic here
}
private static void doSomethingElse(ArrayList<Integer> array) {
//do logic here
}
private static void doAThirdThing(ArrayList<Integer> array) {
//do logic here
}
By the way, I'm impressed you're already looking for ways to keep your code clean and easy to write. Mature programmers usually break out complicated code into smaller functions, exactly like you're thinking about doing. I'm excited for you to continue studying Java!

Strings from input are never equal? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
import java.util.Scanner;
public class Michal {
public static void main(String[] args) {
reply();
}
public static void reply() {
Scanner input=new Scanner(System.in);
String name=input.nextLine();
if(name=="john"){
System.out.println("bear!");
}else if(name=="mary")
{
System.out.println("lovely lady!");
}else{
System.out.println("I don't know that person.");
}
System.out.println(name);
input.close();
}
}
I consider myself still a beginner to Java ,so please don't be harsh in your answers. I was trying to create a program that returns an answer every time it gets a certain input , however it seems to return "I don't know that person" all the time.
if(name=="john")
is not the right way to compare strings. Use equals() instead:
if(name.equals("john")){
System.out.println("bear!");
} else if(name.equals("mary")){

Categories