0

Anyone correct my errors? I mean why it is showing exception?

import java. util.*; public class Program { public static void main(String[] args) { Scanner sc=new Scanner (System.in); String t=new Scanner (System.in).next(); int arr=sc.nextInt(); String n[]=new String[4]; for(int i=0;i<n.length;i++) { n[i]=sc.nextLine(); } String ne[]=new String [n.length+1]; for(int i=0;i<n.length;i++) { ne[i]=n[i];} ne[ne.length-1]=t; Arrays.sort(ne); //System.out.print(Arrays.toString(ne)); //System.out.println(time) for (int i=0;i<ne.length ;i++) { if(ne[i].equals(t)) arr=i; break ; } System.out.println (arr*20); } }

9th Jun 2021, 4:26 AM
Atul
Atul - avatar
10 odpowiedzi
+ 1
Atul Use 1st scanner object sc
11th Jun 2021, 6:16 AM
A͢J
A͢J - avatar
+ 1
Atul See this Scanner sc = new Scanner(System.in); String s1 = sc.next();//1st input int n = sc.nextInt(); //2nd input String s2 = ""; if(sc.nextLine() != "") s2 = sc.nextLine();//3rd input which is string separated by space //Converting space separated string to string array String[] sarr = s2.split(" ");
11th Jun 2021, 6:18 AM
A͢J
A͢J - avatar
0
In the question it is told to do so
9th Jun 2021, 4:51 AM
Atul
Atul - avatar
9th Jun 2021, 4:54 AM
Atul
Atul - avatar
0
Atul There didn't mention to create two objects of Scanner class. You can do everything from only one object.
9th Jun 2021, 4:59 AM
A͢J
A͢J - avatar
0
Why it is showing error in my code? Can you tell me it's reason?
9th Jun 2021, 5:08 AM
Atul
Atul - avatar
0
Atul Because in Sololearn we takes input one time with separate lines so wherever we create two objects of Scanner class then we get this error. So if you can work with one scanner class object then doesn't make sense to create another object because we are creating objects from same class so both objects will behave same.
9th Jun 2021, 5:24 AM
A͢J
A͢J - avatar
0
So how I can take Eric separately pls show me
9th Jun 2021, 8:16 AM
Atul
Atul - avatar
0
So can you tell me how to solve that problm?
11th Jun 2021, 6:11 AM
Atul
Atul - avatar
- 1
Atul Problem is here: String t=new Scanner (System.in).next(); Why did you take 2 times new Scanner (System.in)? You can do this: String t=sc.next();
9th Jun 2021, 4:41 AM
A͢J
A͢J - avatar