I need to write a program which allows me to input "3 Joe,123-5432 Linda,983-4123 Frank,867-5309 Frank" and output "867-5309". This is the code I have so far..." import java.util.Hashtable; import java.util.Scanner; public class Main { public static void main(String[] args) { // initialize Scanner class Scanner sc = new Scanner(System.in); // initialize Hashtable // tells java that both key and value will be of type String Hashtable my_dict = new Hashtable<>(); // get the count of records to be entered // 1st part of the input int n = sc.nextInt(); // execute for loop n times // 2nd part of the input for (int i = 0; i < n; i++) { // get a record from the user. example: Linda,983-4123 String s = sc.next(); // split the string on comma to get the name and code in an array String[] details = s.split(","); // get the name String name = details[0]; // get the code String code = details[1]; // put the record in the dictionary // here name is key and code is value corresponding to it my_dict.put(name, code); } // get the name // 3rd part of the input String name = sc.next(); // get the code corresponding to name in the dictionary String code = my_dict.get(name); // print the code System.out.println(code); } }" I try to run it, but I keep getting " LabProgram.java:4: error: class Main is public, should be declared in a file named Main.java public class Main { ^ 1 error"
I need to write a program which allows me to input "3 Joe,123-5432 Linda,983-4123 Frank,867-5309 Frank" and output "867-5309". This is the code I have so far..."
import java.util.Hashtable;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// initialize Scanner class
Scanner sc = new Scanner(System.in);
// initialize Hashtable
// <String, String> tells java that both key and value will be of type String
Hashtable<String, String> my_dict = new Hashtable<>();
// get the count of records to be entered
// 1st part of the input
int n = sc.nextInt();
// execute for loop n times
// 2nd part of the input
for (int i = 0; i < n; i++) {
// get a record from the user. example: Linda,983-4123
String s = sc.next();
// split the string on comma to get the name and code in an array
String[] details = s.split(",");
// get the name
String name = details[0];
// get the code
String code = details[1];
// put the record in the dictionary
// here name is key and code is value corresponding to it
my_dict.put(name, code);
}
// get the name
// 3rd part of the input
String name = sc.next();
// get the code corresponding to name in the dictionary
String code = my_dict.get(name);
// print the code
System.out.println(code);
}
}"
I try to run it, but I keep getting "
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images