0

user inserts 5 strings, if any string has been repeated the user gets alerted and asked to try again,once the user has given 5 c

import java.util.* ; import com.sun.org.apache.bcel.internal.generic.NEW; class Main { public static void main(String[] args) { List<String> sList = new ArrayList<String>() ; System.out.println("Insert 5 strings!"); Scanner sc = new Scanner(System.in) ; for(int i = 1 ; i <= 5 ; i++){ String v = sc.nextLine() ; sList.add(v) ; while(true){ for(String x : sList){ if(v.equalsIgnoreCase(x)){ break; System.out.println("Try again!") ; } for(String y : sList){ System.out.println(x) ; } } } } } }

18th Sep 2022, 1:36 PM
Lenoname
5 ответов
+ 1
Your question is incomplete. And See you are adding input string to list then checking if it is in the list.. Why not it don't exist? You just added. Instead before adding , if it already exist in list..? After break; loop exits and next statements won't executed.. In last loop, you declared String y but using x another Error.
18th Sep 2022, 2:33 PM
Jayakrishna 🇮🇳
+ 1
Pls edit your question. Describe the problem in the question description, not in the title (which is meant to be short). Save your code in Code Playground and add a link to it in the question description (use "+" button). Also in the question description, explain your difficulties. This way, we know what you want to do, what you did, what is wrong, and which help you need.
18th Sep 2022, 5:25 PM
Emerson Prado
Emerson Prado - avatar
+ 1
import java.util.*; import java.io.* ; class Main { public static void main(String[] args) { List<String> sList = new ArrayList<String>() ; Scanner sc = new Scanner(System.in) ; for(int i = 1 ; i <= 5; i++){ String a = sc.nextLine() ; boolean repeat = false; // bool value to tell, item for repeated or not. for(String b : sList) if(a.equalsIgnoreCase(b)){ System.out.println("This strig already exists in the list!"); repeat = true; // setting true means exist in loop. break; } // only if not repeated then added to list, after loop. if( repeat == false) sList.add(a); } } }
19th Sep 2022, 7:46 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 import java.util.* ; import com.sun.org.apache.bcel.internal.generic.NEW; class Main { public static void main(String[] args) { List<String> sList = new ArrayList<String>() ; System.out.println("Insert 5 strings!"); Scanner sc = new Scanner(System.in) ; while(true){ for(String x : sList){ if(v.equalsIgnoreCase(x)){ break; System.out.println("Try again!") ; } for(int i = 1 ; i <= 5 ; i++){ String v = sc.nextLine() ; sList.add(v) ; for(String x : sList){ System.out.println(x) ; } } } } } } like this??
19th Sep 2022, 6:27 PM
Lenoname
0
import java.util.*; import java.io.* ; class Main { public static void main(String[] args) { List<String> sList = new ArrayList<String>() ; Scanner sc = new Scanner(System.in) ; for(int i = 1 ; i <= 5; i++){ String a = sc.nextLine() ; for(String b : sList) if(a.equalsIgnoreCase(b)){ break; System.out.println("This strig already exists in the list!"); } else{ sList.add(a); } } } } i even tried this
19th Sep 2022, 7:01 PM
Lenoname