w3resource

Java Exercises: Reads a list of pairs of a word and a page number, and prints the word and a list of the corresponding page numbers

Java Basic: Exercise-243 with Solution

Write a Java program which reads a list of pairs of a word and a page number, and prints the word and a list of the corresponding page numbers.
The number of pairs of a word and a page number is less than or equal to 1000. A word never appear in a page more than once. The words should be printed in alphabetical order and the page numbers should be printed in ascending order.

Input:
Input pairs of a word and a page number:
apple 5
banana 6
Word and page number in alphabetical order:
apple
5
banana
6

Sample Solution:

Java Code:

import java.util.PriorityQueue;
import java.util.Scanner;
 
public class Main {
     
    static class Dic implements Comparable<Dic>{
        String moji;
        int page;
        Dic(String moji, int page){
            this.moji=moji;
            this.page=page;
        }
        public int compareTo(Dic d) {
            if(this.moji.equals(d.moji)) {
                return this.page-d.page;
            }
            else {
                return this.moji.compareTo(d.moji);
            }
        }
    }
     
    public static void main(String[] args) {
        try(Scanner sc = new Scanner(System.in)){
            PriorityQueue<Dic> pq=new PriorityQueue<>();
			System.out.println("Input pairs of a word and a page number:"); 
            while(sc.hasNextLine()) {
                String str=sc.nextLine();
                String[] token=str.split(" ");
                String s=token[0];
                int n=Integer.parseInt(token[1]);
                pq.add(new Dic(s, n));
            }
            String pre="";
           System.out.println("\nWord and page number in alphabetical order:"); 
            while(!pq.isEmpty()) {
                Dic dic=pq.poll();
                if(dic.moji.equals(pre)) {
                    System.out.print(" "+dic.page);
                }
                else if(pre.equals("")) {
                    System.out.println(dic.moji);
                    System.out.print(dic.page);
                }
                else {
                    System.out.println();
                    System.out.println(dic.moji);
                    System.out.print(dic.page);
                }
                pre=dic.moji;
            }
            System.out.println();
        }
    }
}

Sample Output:

Input pairs of a word and a page number:
apple 5
banana 6

Word and page number in alphabetical order:
apple
5
banana
6

Pictorial Presentation:

Java exercises: Reads a list of pairs of a word and a page number, and prints the word and a list of the corresponding page numbers.

Flowchart:

Flowchart: Reads a list of pairs of a word and a page number, and prints the word and a list of the corresponding page numbers.

Flowchart:

Flowchart: Reads a list of pairs of a word and a page number, and prints the word and a list of the corresponding page numbers.

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java program which adds up columns and rows of given table as shown in the specified figure.
Next: Write a Java program which accepts a string from the user and check whether the string is correct or not.

What is the difficulty level of this exercise?



Java: Tips of the Day

Array vs ArrayLists:

The main difference between these two is that an Array is of fixed size so once you have created an Array you cannot change it but the ArrayList is not of fixed size. You can create instances of ArrayLists without specifying its size. So if you create such instances of an ArrayList without specifying its size Java will create an instance of an ArrayList of default size.

Once an ArrayList is full it re-sizes itself. In fact, an ArrayList is internally supported by an array. So when an ArrayList is resized it will slow down its performance a bit as the contents of the old Array must be copied to a new Array.

At the same time, it's compulsory to specify the size of an Array directly or indirectly while creating it. And also Arrays can store both primitives and objects while ArrayLists only can store objects.

Ref: https://bit.ly/3o8L2KH