从单行获取输入,使用 split、delim 进行分割,并将输入解析为字典。

huangapple 未分类评论41阅读模式
英文:

Getting inputs from a single line using split, delim and parsing inputs for a dictionary

问题

我有几个问题:

  1. 除了使用 split 和数组之外,是否有更好的方法来处理单行输入?如何更好地处理各种可能性,比如空白/空输入,或者如果预期的输入模式与预期不符。

  2. 在这个小片段中,让我感到疑惑的是,为什么在第一个 for 循环的输入跳过(skip)了输入,如果我不将这行代码放在这里的话:
    in.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
    在第一个输入扫描 n 变量之后。如果不加这行代码,它会跳过输入并且不能按预期工作。

  3. 验证输入还是使用 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:

  1. 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.

  2. 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(&quot;(\r\n|[\n\r\u2028\u2029\u0085])?&quot;);
    after the first input scanner for the n variable. It keeps omitting and won't work as intended.

  3. 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(&quot;(\r\n|[\n\r\u2028\u2029\u0085])?&quot;);
		HashMap&lt;String, Integer&gt; littlePhoneBook = new HashMap&lt;String, Integer&gt;();
		for (int i = 0; i &lt; n; i++) {
			String temp = in.nextLine();
			String delims = &quot;[ ]+&quot;;
				
	//Have to comment this part since it wont past the test
	//if (temp.isBlank()) {//check if the entry is blank
	//	System.out.println(&quot;It cant be a blank input&quot;);
	//	temp = in.nextLine();
	//} 
					
			if (temp.length() &gt; 1) {
				String a = temp.split(delims)[0];
				temp = temp.concat(&quot; 0&quot;);//Force to include a 0 for array[1] after the split and parseInt, if there is not argument present, ex: &quot;&quot;, &quot; &quot;, \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 + &quot;=&quot; + littlePhoneBook.get(s));
			} else if (s.equals(&quot;exit&quot;)) {//exit the loop after writing the designate word.
				System.out.println(&quot;Thanks for your query, be back soon!&quot;);
				break;
			} else {
				System.out.println(&quot;Not found&quot;);
			}
		}
		in.close();
    }
}

huangapple
  • 本文由 发表于 2020年7月24日 16:10:25
  • 转载请务必保留本文链接:https://java.coder-hub.com/63069501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定