0

whats the problem?

import java.util.Scanner; import java.util.ArrayList; public class Remove{ static boolean pressed = false; public static void press(){ pressed = true; } public static void notPressing(){ pressed = false; } } public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String x = scan.nextLine(); ArrayList<String> arr = new ArrayList<String>(); arr.add ("Jonathan"); arr.add ("Ron"); arr.add ("Levi"); arr.add ("Morlo"); Remove r = new Remove(); // r.press(); if (r.pressed == true && (x == "Jonathan" || "Ron" || "Levi" || "Morlo")){ arr.remove (x); System.out.println (arr); }else if (x != "Jonathan" || "Ron" || "Levi" || "Morlo"){ System.out.println ("There is no such a Participant"); }else{ System.out.println (arr); } } }

5th May 2020, 7:26 AM
Yahel
Yahel - avatar
11 Antworten
0
You are missing the check to x in the or-statements. x == "Jonathan" || "Ron" -> x == "Jonathan" || x == "Ron" Also your else would be wrong even with correct variable check. There it would have to be && instead of ||, because if one is equal the others are not and will match. You make it more simple, readable and more failure Proof by using the contains method of ArrayList. var known = arr.contains(x); if (r.pressed == true && known){ //... } else if (!known) { // ... } else { // ... }
5th May 2020, 7:49 AM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar
+ 1
var ? is this java or what? a bit confused here
5th May 2020, 8:03 AM
esQmo_
esQmo_ - avatar
0
just saw it, not really readable code
5th May 2020, 7:52 AM
esQmo_
esQmo_ - avatar
0
shouldn't be if r.pressed == true && (x == "Jonathan" || x == "Ron" || x == "Levi" || x == "Marlon"); ?
5th May 2020, 7:57 AM
esQmo_
esQmo_ - avatar
0
thanks everybody, now it works... import java.util.Scanner; import java.util.ArrayList; public class Remove{ static boolean pressed = false; public static void press(){ pressed = true; } public static void notPressing(){ pressed = false; } } public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String x = scan.nextLine(); ArrayList<String> arr = new ArrayList<String>(); arr.add ("Jonathan"); arr.add ("Ron"); arr.add ("Levi"); arr.add ("Morlo"); Remove r = new Remove(); var here = arr.contains(x); // r.press(); if (r.pressed == true && here){ arr.remove (x); System.out.println (arr); }else if (!here){ System.out.println ("There is no such a Participant."); }else if (r.pressed == false){ System.out.println (arr); } } }
5th May 2020, 8:00 AM
Yahel
Yahel - avatar
0
thats what Manu told me, and it works... :) i will check right now what var does in java
5th May 2020, 8:07 AM
Yahel
Yahel - avatar
0
AFAIK, var is not a keyword in java
5th May 2020, 8:09 AM
esQmo_
esQmo_ - avatar
0
you could simply do if(r.pressed && arr.contains(x)){}
5th May 2020, 8:11 AM
esQmo_
esQmo_ - avatar
0
ah that's a Java 10 thing, great I learned something today!
5th May 2020, 8:12 AM
esQmo_
esQmo_ - avatar
0
yes.. doesnt really metter tho... thanks for the help
5th May 2020, 8:13 AM
Yahel
Yahel - avatar