0
Why this code doesn't work?
4 ответов
+ 2
arr.add(input.nextLine());
for(int i=arr.size()-1;i>0;i--)
From these two, it take a line of input, adds to ArrayList,
Now size of array list is 1 only..
For loop never starts.., because i=0 > 0 false.
What is the need of ArrayList?
Probably, you not understood what is ArrayList... It doesnot add character by character. It adds owl elements which may be strings, numbers,...
So use a string or convert input to a char array by toCharArray() method.
+ 1
import java.util.Arrays;
...
ArrayList<String> arr = new ArrayList<>(
Arrays.asList( input.nextLine() .split("") )
);
for(int i=arr.size()-1; i>0; i--) {
for(int j=0; j<i; j++){ //not j<0
if (arr.get(j).equals(arr.get(i))) {
System.out.println("Deja Vu" );
return;
}
}}
System.out.println("Unique");
0
Task:
If you are given a string of random letters, your task is to evaluate whether any letter is repeated in the string or if you only hit unique keys while you typing.
0
import java.util.*;
public class Program
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String copied_string="";
String duplicates="";
int digit=0;
int count=0;
for(int i=0;i<s.length();i++)
{
String current=Character.toString(s.charAt(i));
if(copied_string.contains(current))
{
count++;
}
copied_string+=current;
}
if(count>=1)
{
System.out.println("Deja Vu");
}
else
{
System.out.println("Unique");
}
}
}