67 072
Assignments Done
99,3%
Successfully Done
In November 2018

Answer to Question #52332 in Java | JSP | JSF for mikechen1234prolo@yahoo.com

Question #52332
write a program that reads in a set of positive integers and outputs how many times a particular number appears in the list. you may assume that the data set has at most 100 numbers and -999 marks the end of the input data. the numbers must be output in increasing order.
Expert's answer
Code.
import java.util.Scanner;
import java.util.TreeMap;
 
public class Main {
    public static TreeMap<Integer, Integer> tm  = new TreeMap<Integer, Integer>();
   
    public static Scanner sc = new Scanner(System.in);
   
    public static void main(String[] args) {
        int newNumber;
        while (true) {
           newNumber =sc.nextInt();
           if (newNumber == -999) {
               break;
           } else if (tm.containsKey(newNumber)) {
               tm.put(newNumber, tm.get(newNumber) + 1);
           } else {
               tm.put(newNumber, 1);
           }
        }
        System.out.println(tm);
    }
}
Input/Output.
10
11
13
5
6
7
19
5
7
-999
{5=2, 6=1, 7=2, 10=1,11=1, 13=1, 19=1}


 


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions