简体   繁体   中英

Is there any possibility to break while loop " while(Scanner.hasNext())" without condition of break it?

I'm bothering with such problem for long hours: Task is " blah blah.. You will then be given an unknown number of names to query your phone book for.. " And Like I said int title I'm using this condition "hasNext". How I can break this loop without clear knowledge of ending input, what will be write in console and amout of input? I was thinking that do while loop will help me, but still without expected result :(.

Thanks for any answers and sorry if it's so obvious to answear-I was looking for similar questions/answears, but none of it worked :(

EDIT( and complete task is here: https://www.hackerrank.com/challenges/30-dictionaries-and-maps ):

public class Test {

public static void main(String[] args) {
    HashMap<String, Integer> map = new HashMap<>();
    ArrayList<String> list = new ArrayList<>();
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();

    for (int k = 0; k < a; k++) {
        String name = sc.next();
        int numer = sc.nextInt();
        map.put(name, numer);
    }
  while(sc.hasNext()){
       String name=sc.next();
       sc.nextLine();
       list.add(name);
    }
    sc.close();
    for (String k : list) {
        if (map.containsKey(k)) {
            System.out.println(k + "=" + map.get(k));
        } else {
            System.out.println("Not found");
        }
    }

}

}

I think you might be looking for break .

while(scanner.hasNext()){
    String next = scanner.nextLine();
    if(next.equals("quit")){
        //you will exit the loop here
        break;
    }

    System.out.println(next);
    //more code here

}

It is already solved, but maybe it would help someone in future :D

public class Test {
    public static void main(String[] args) {
        HashMap<String,Integer> mapa = new HashMap<>();
        ArrayList<String> lista = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        boolean condition = true;

        for(int k = 0; k < a; k++) {
            String imie = sc.next();
            int numer = sc.nextInt();
            mapa.put(imie, numer);
        }

        while(condition == true) {
            try {
                String line;

                while(!(line = sc.nextLine()).isEmpty()) {
                    lista.add(line);
                }
            }
            catch(NoSuchElementException exception) {
                condition=false;
            }
        }

        sc.close();

        for(String k: lista) {
            if(mapa.containsKey(k))
                System.out.println(k + "=" + mapa.get(k));
            else
                System.out.println("Not found");
        }
    }
}

A possible solution for your problem:

public static void main(String[] args) throws IOException 
{
    HashMap<String, Integer> map = new HashMap<>();
    ArrayList<String> list = new ArrayList<>();
    Scanner sc = new Scanner(System.in);

    int a = sc.nextInt();
    for (int k = 0; k < a; k++) {
        String name = sc.next();
        int numer = sc.nextInt();
        map.put(name, numer);
    }

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String name;
    while ((name = in.readLine()) != null && name.length() != 0) 
    {
        list.add(name);
    }

    for (int i = 0; i < list.size(); i++) 
    {
        if (map.containsKey(list.get(i))) 
        {
            System.out.println(list.get(i) + "=" + map.get(list.get(i)));
        } else 
        {
            System.out.println("Not found");
        }
    }
    sc.close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM