0

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("(\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.

  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("(\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();
    }
}
Abra
  • 11,631
  • 5
  • 25
  • 33
  • Regarding your second question, refer to [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Abra Aug 15 '20 at 06:08
  • Regarding your first question, the best way to handle a single line of input depends on what is contained in the input and how you want to handle it. Neither of those details appear in your question. I don't think there is a single, universal way to handle _any_ line of input. As I said, it depends on the content of the input and what handling is required. – Abra Aug 15 '20 at 06:13

0 Answers0