0

Can you simplify my code? (shows how many poeple liked something like on instagram)

Hi guys, do you think I could change something or simplify this Code somehow? (Java): public Name() { for (int i = 0; i >= 0; i++) { name = sc.nextLine(); if (check(name)==true){ names.add(name); } if (names.size()==0) { System.out.println("No one likes this"); }else{ if (names.size() == 1) { System.out.println(names.get(0) + " likes this"); } else if (names.size() == 2) { System.out.println(names.get(1) + " and " + names.get(0) + " like this"); } else if (names.size() == 3) { System.out.println(names.get(2) + " and " + names.get(1) + " and " + names.get(0) + " like this"); } else { int hinten = names.size() - 2; System.out.println(names.get(names.size() - 1) + ", " + names.get(names.size() - 2) + " and " + hinten + " others like this"); } } } } public boolean check(String name) { for(char c: name.toCharArray()) { if(c>='a'&&c<='z') { continue; } if(c>='A'&&c<='Z') { continue; } if(c=='ä'||c=='ö'||c=='ü'||c=='Ä'||c=='Ö'||c=='Ü'||c=='ß') { continue; } return false; } return true; } }

1st Jul 2022, 4:41 PM
Yanic
9 Réponses
+ 3
Yanic this is how I tried to simplify your code https://code.sololearn.com/cb5cS9rjlb4g/?ref=app
1st Jul 2022, 5:49 PM
Bhaveshsingh Pawar
Bhaveshsingh Pawar - avatar
+ 2
this is readable String and = " and "; int size = names.size(); String who = switch( size) { case 0 -> "No one"; case 1 -> names.get(0); case 2 -> names.get(1) +and+ names.get(0); case 3 -> names.get(2) +and+ names.get(1) +and+ names.get(0); default -> names.get( size-1) +", "+ names.get( size-2) +and+ (size-2) +" others"; }; System.out.println( who +" like this");
1st Jul 2022, 10:17 PM
zemiak
+ 1
What are you trying to do?
1st Jul 2022, 4:51 PM
Justice
Justice - avatar
+ 1
ɴᴜʟʟ thanks! That looks good. Gonna try this in a moment :D
1st Jul 2022, 6:06 PM
Yanic
+ 1
first correct for() import java.util.*; public class Name { Scanner sc = new Scanner( System.in); ArrayList<String> names = new ArrayList<>(); String name; public static void main(String[] args) { new Name(); } public Name() { //for (int i=0; i >= 0; i++) { for (int i=0; i < 5; i++) {
1st Jul 2022, 7:23 PM
zemiak
0
sorry i couldnt write everything because of the character restrictions. I am trying to make a code where i can type in random names and based on the names and number of names it gives me an output like this: [] --> "no one likes this" ["Peter"] --> "Peter likes this" ["Jakob", "Jan"] --> "Jakob and Jan like this" ["Max", "Jan", "Jens"] --> "Max, Jan and Jens like this" ["Jan", "Jakob", "Jens", "Max"] --> "Jan, Jakob and 2 others like this"
1st Jul 2022, 5:11 PM
Yanic
0
zemiak that's also a really good idea. Thank you guys really appreciate the help. Really helped me out and learned something
1st Jul 2022, 10:33 PM
Yanic
0
edit, i add size variable
1st Jul 2022, 11:24 PM
zemiak
0
I need output of this code with explanation user input and do the following 19->1+9=10 1,9, 10->9+10=19user input and final number is same print success
3rd Jul 2022, 1:13 PM
Jagathis T
Jagathis T - avatar