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); } }
10 Réponses
+ 1
Atul
Use 1st scanner object sc
+ 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(" ");
0
In the question it is told to do so
0
https://www.sololearn.com/coach/18?ref=app
This is the question
0
Atul
There didn't mention to create two objects of Scanner class. You can do everything from only one object.
0
Why it is showing error in my code? Can you tell me it's reason?
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.
0
So how I can take Eric separately pls show me
0
So can you tell me how to solve that problm?
- 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();