英文:
Getting inputs from a single line using split, delim and parsing inputs for a dictionary
问题
我有几个问题:
-
除了使用 split 和数组之外,是否有更好的方法来处理单行输入?如何更好地处理各种可能性,比如空白/空输入,或者如果预期的输入模式与预期不符。
-
在这个小片段中,让我感到疑惑的是,为什么在第一个 for 循环的输入跳过(skip)了输入,如果我不将这行代码放在这里的话:
in.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
在第一个输入扫描 n 变量之后。如果不加这行代码,它会跳过输入并且不能按预期工作。 -
验证输入还是使用 try-with-resources,并在发生错误时执行某些操作?
/*
* @关于 使用映射或字典组装电话簿
* 此版本为从HackerRank用户RodneyShag修改的代码
* @作者 (Ricardo Garcia)
* @版本 (19-7-2020/8:44am)
*/
import java.util.Scanner;
import java.util.HashMap;
public class Day8Dictionarys {
public static void main(final String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();// 输入数量
in.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
HashMap<String, Integer> littlePhoneBook = new HashMap<String, Integer>();
for (int i = 0; i < n; i++) {
String temp = in.nextLine();
String delims = "[ ]+";
//必须注释掉这部分,因为它不能通过测试
//if (temp.isBlank()) {//检查条目是否为空白
// System.out.println("它不能是空白输入");
// temp = in.nextLine();
//}
if (temp.length() > 1) {
String a = temp.split(delims)[0];
temp = temp.concat(" 0");//强制在拆分和解析之后的数组[1]之前包含0,如果没有参数,例如:"", " ", \n,空白或空格。
String b = temp.split(delims)[1];
int c = Integer.parseInt(b);
littlePhoneBook.put(a, c);
}
}
while (in.hasNext()) {
String s = in.next();
if (littlePhoneBook.containsKey(s)) {//检查哈希映射中是否存在键,并在控制台上显示
System.out.println(s + "=" + littlePhoneBook.get(s));
} else if (s.equals("exit")) {//在写入指定词语后退出循环。
System.out.println("感谢您的查询,很快会回来!");
break;
} else {
System.out.println("未找到");
}
}
in.close();
}
}
英文:
I have a couple of questions:
-
Is there a better way to handle a single line input other than using split and arrays? How do I handle it in a better way for the various possibilities, like blank/empty inputs or if the expected pattern of input didn't happen as expected.
-
It got me wondering why, in the little snippet, keeping skip jumps the inputs of the first for-loop, if I don't place this line of code.
in.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
after the first input scanner for the n variable. It keeps omitting and won't work as intended. -
Validating the inputs or using the try with resources and do something if an error happens?
/*
* @About Assemble a phone book with map or dictionary
* This version is a modified code from HackerRank user RodneyShag
* @Author (Ricardo Garcia)
* @Version (19-7-2020/8:44am)
*/
import java.util.Scanner;
import java.util.HashMap;
public class Day8Dictionarys {
public static void main(final String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();// Number of inputs
in.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
HashMap<String, Integer> littlePhoneBook = new HashMap<String, Integer>();
for (int i = 0; i < n; i++) {
String temp = in.nextLine();
String delims = "[ ]+";
//Have to comment this part since it wont past the test
//if (temp.isBlank()) {//check if the entry is blank
// System.out.println("It cant be a blank input");
// temp = in.nextLine();
//}
if (temp.length() > 1) {
String a = temp.split(delims)[0];
temp = temp.concat(" 0");//Force to include a 0 for array[1] after the split and parseInt, if there is not argument present, ex: "", " ", \n, empty or blank space.
String b = temp.split(delims)[1];
int c = Integer.parseInt(b);
littlePhoneBook.put(a, c);
}
}
while (in.hasNext()) {
String s = in.next();
if (littlePhoneBook.containsKey(s)) {//check the hashMap for the key and show on console
System.out.println(s + "=" + littlePhoneBook.get(s));
} else if (s.equals("exit")) {//exit the loop after writing the designate word.
System.out.println("Thanks for your query, be back soon!");
break;
} else {
System.out.println("Not found");
}
}
in.close();
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论