+ 1

New Driver’s License problem

Hello ! So when I compile the code on my computer works fine and the calculations are right. But here I have some problem with the input of the names of the other four customers. Does somebody see the mistake? import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner in=new Scanner(System.in); String myName=in.nextLine(); int agents=in.nextInt(); String four=in.nextLine(); String[]names=four.split(" "); int myPlace=1; int howLong=0; for (int i=0; i<names.length; i++) { if(myName.compareTo(names[i])>0) myPlace++; } if(myPlace==1||names.length+1==agents) howLong=20; else howLong=(Math.round(myPlace/agents))*20; System.out.println(howLong); } }

26th Jan 2020, 10:35 AM
Georgi
7 Respostas
+ 8
well, try that, it worked😉: import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String myname = sc.nextLine(); int agent = sc.nextInt(); String dummy = sc.nextLine(); // <-- ?????????????? String others = sc.nextLine(); sc.close(); String[] alt = others.split(" "); ArrayList<String> names = new ArrayList<String>(); for (int i=0; i<alt.length; i++) { names.add(alt[i]); } names.add(myname); Collections.sort(names); int pos = names.indexOf(myname) + 1; int a = 0; do { pos -= agent; a++; } while (pos>0); System.out.println(a*20); } }
1st Oct 2020, 8:00 AM
ă‚€ăƒ‡ăƒȘă‚»{非掻性}
ă‚€ăƒ‡ăƒȘă‚»{非掻性} - avatar
+ 3
After inputting the number of agents, add this line: String dummy = sc.nextLine() The reason why your code is not working is because after inputting the int, there is a newline character which is not inputted. So storing that in a dummy String will make the program work and the rest of the inputs will work fine.
26th Jan 2020, 10:43 AM
Tejaswi Hegde
Tejaswi Hegde - avatar
+ 3
Then you would have to check the second letter and follow the alphabetical order. If 2nd letter also matches then check the 3rd and so on.
26th Jan 2020, 11:20 AM
Tejaswi Hegde
Tejaswi Hegde - avatar
+ 3
so finally how is the result?????'
28th Jan 2020, 6:37 PM
Sergio R
Sergio R - avatar
+ 1
that fixed it ! thanks ! 2 tests fail though..i wonder why.. for example if the name of someone else starts with the same letter as yours what would be the case then
26th Jan 2020, 11:18 AM
Georgi
+ 1
it worked. the problem were the number of agents not the names
28th Jan 2020, 7:34 PM
Georgi
0
myname = input() num_agents = int(input()) other_names = list(input().split()) other_names.append(myname) other_names.sort() ind_myname = other_names.index(myname)+1 print('20' if ind_myname <=num_agents else (ind_myname-num_agents)*20+20)
25th Nov 2021, 10:29 PM
Mas'ud Saleh
Mas'ud Saleh - avatar