count occourance no of time no between 0 to 9 given by user in java

count occourance no of time no between 0 to 9 given by user in java

import java.io.*; class count_no { public static void main(String args[]) { try { int [] size=new int[9]; int i,temp=0; DataInputStream dis=new DataInputStream(System.in);

           for(i=0;i<9;i++)
            {
                    int no=Integer.parseInt(dis.readLine());

                    if(no==1)
                     {
                                   temp++;
                     }

                  else if(no==2)
                   {
                        temp++;
                   }
             }System.out.println(temp);     

        }catch(Exception e){}               
     }

}

View Answers

October 12, 2012 at 8:25 PM

thanks


October 8, 2012 at 4:23 PM

Here is a java example that accepts 9 integers from the user and count the occurrence of each integer.

import java.util.*;

public class CountNumberFrequency {
               public static void main(String[] args) {
                Scanner input = new Scanner(System.in);
                int[] arr = new int[9];
                System.out.println("Enter numbers:");
                for (int i = 0; i < arr.length; i++) {
                     arr[i] = input.nextInt();
                }
                Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < arr.length; i++) {
            int key = arr[i];
            if (map.containsKey(key)) {
                int occurrence = map.get(key);
                occurrence++;
                map.put(key, occurrence);
            } else {
                map.put(key, 1);
            }
        }

        Iterator iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            int key = (Integer) iterator.next();
            int occurrence = map.get(key);

            System.out.println(key + " : " + occurrence + " times.");
        }
    }
}

Ads









Related Tutorials/Questions & Answers:
count occourance no of time no between 0 to 9 given by user in java
count occourance no of time no between 0 to 9 given by user in java  ... example that accepts 9 integers from the user and count the occurrence of each...(System.in); for(i=0;i<9;i
what is the difference between productList==null and productList.size()==0
what is the difference between productList==null and productList.size()==0  what is the difference between productList==null and productList.size()==0? here productList is of type LIST
Advertisements
days between two given dates using PHP
days between two given dates using PHP  How can we know the number of days between two given dates using PHP?   Hi friends, Example...;"; // Number of days in between to given date in php $days = (strtotime
Finding the longest word in a user given string
Finding the longest word in a user given string  In java, accept a string from the user and find the longest word in it.   The given code...; } else{ return st2; } } LongestWord(){ String word = ""; for(int i=0;i<
if i run this prog, i got exeption in thread "main" java.lang.ArrayIndexOutOfBoundsException:0 at Maxof2.main(Maxof2.java:9), whats the resolution for ths?
" java.lang.ArrayIndexOutOfBoundsException:0 at Maxof2.main(Maxof2.java:9), whats the resolution for ths... int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); if(i..., ArrayIndexOutOfBoundsException:0 means that you haven't specify the arguments along
calculate difference between two time in jsp
calculate difference between two time in jsp  How to calculate difference between two dates
Java count frequency of words in the string
Java count frequency of words in the string. In this tutorial, you will learn how to count the occurrence of each word in the given string. String...;; } str = str.toLowerCase(); int count = -1; for (int i = 0; i < str.length
ModuleNotFoundError: No module named 'py-time-between'
ModuleNotFoundError: No module named 'py-time-between'  Hi, My... named 'py-time-between' How to remove the ModuleNotFoundError: No module named 'py-time-between' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'py-time-between'
ModuleNotFoundError: No module named 'py-time-between'  Hi, My... named 'py-time-between' How to remove the ModuleNotFoundError: No module named 'py-time-between' error? Thanks   Hi, In your python
time
time  how to find the current system time between the user login and logout using java
Get Date and Time
current date and time in the user given format. To do all this we have first...;How to get date and time in Java?"... Get Date and Time   
Count the character in java
Count the character in java  Write a java program to count the character â??aâ?? typed by the user in a paragraph by implementing thread.   Count characters by implementing thread import java.util.*; class
java 9 32 bit download
java 9 32 bit download  Hi, I am trying to install JDK..., and then contact software publisher. What is this issue? How o get get Java 9 32 Bit... be able to install JDK 9. Thanks   Hi, Chief Java Architect Mark
Java 9 Features
Java 9 Features: First version of Java 9 Features announced The plan... features list of Java 9 on August 18, 2014. The first set of Java Enhancement... the development of JDK 9. Here is the details of the first set of Java 9 features: Here
Java Time Zone Example
Java Time Zone Example       Converting time between time zones In this section we have written a java program which will take two Time Zone IDs as its argument and then convert
JAVA leftshift operator add 1 instead of 0
JAVA leftshift operator add 1 instead of 0  Hi Guys, I have a task... on that, it adds "0" on left side of value. For example, view plaincopy... this, 5 as binary : 0101 1st time left shift: 01010 2nd time left shift: 010100
Can u print in turbo c 9 8 7 6 5 4 3 2 1 0 using pre increment operator Only use print ++a in the same printf() command ten times)
Can u print in turbo c 9 8 7 6 5 4 3 2 1 0 using pre increment operator Only use print ++a in the same printf() command ten times)  Can u print in turbo c 9 8 7 6 5 4 3 2 1 0 using pre increment operator Only use print
Display PHP clock with user input date and time
Display PHP clock with user input date and time  The following PHP code displays a clock with current date and time. I want the clock to receive user... "Date: ".$aa[0]; echo "</br>"; echo "Time: " .$aa[1]; die
Java 9 Tutorial, news and examples
News, articles and example of the Java 9 Programming Language After... of the Java 9 programming Language. Here we will be discussing about the news, articles and example of the proposed Java 9 Language. Java 9 is going
How to find out the friend user between two columns in sql database
How to find out the friend user between two columns in sql database  Hi, Can any one please tell me ..how to find out the friend user between two columns in sql database? In other words i wanted to get the corresponding data from
Can u print in turbo c 9 8 7 6 5 4 3 2 1 0 using pre increment operator Only use print ++a in the same printf() command ten times)
Can u print in turbo C  Can u print in turbo c 9 8 7 6 5 4 3 2 1 0 using pre increment operator Only use print ++a in the same printf() command ten times
Time validation
. If the user enters the time in wrong format, it displays the error message. <... || hour > 23) { alert("Hour must be between 1 and 12. (or 0 and 23 for military time)"); document.frm.timetext.value=""; return false; } if (minute<0
Nb hours between 2 time stamps meets specific criteria
Nb hours between 2 time stamps meets specific criteria  sql - I have 2 columns with time stamps and 2 empty columns, named DAYHOURS AND NIGHTHOURS. I want to update DAYHOURS and NIGHTHOURS with Number of hours fall under - 06:00
return 0 vs exit 0
return 0 vs exit 0  what is the difference between return 0 and exit 0 in c? i could not find any difference in their behaviour (both terminates the process execution) in a program
ArrayIndexOutOfBoundsException: 0
) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds) here is my code...ArrayIndexOutOfBoundsException: 0  I'am a newbie to java, i have...; ether.dst_mac=new byte[]{(byte)0,(byte)6,(byte)7,(byte)8,(byte)9
Java: Some problems in given condition
Java: Some problems in given condition   SIR, I want to get the values from the table( database), if any one of the column of the table value is 0. Then one alert should b printed in application using Java. If(att.getdata1()==0
Count characters from text file in Java
is a java code that count the occurrence of each character from text file. import...++){ char ch= third[counter]; int count=0; for ( int i=0; i<third.length; i++){ if (ch==third[i]) count++; } boolean flag=false; for(int j=counter-1;j>=0;j
Ask java count
Ask java count  Good morning, I have a case where there are tables... | Java 1 | 10 | | b002 | beginner java | 5 | | b003 | advanced java book | 26 | | b004 | MySQL 1
Java get number of days between dates
Java get number of days between dates  ... the number of days between two dates, the given example has set two dates by using... and 9 for the month October. The given dates are 2008,Sep,1 and 2008,Oct,31
difference between hashcode,reference in java
difference between hashcode,reference in java  difference between hashcode,reference in java
Java count vowels
Java count vowels In this section you will learn how to count the number... Scanner(System.in); String input = sc.nextLine(); int count=0... and then you will get the number of vowels count. Description of code : In this code
how to count words in string using java
: "); String st=input.nextLine(); int count=0... count=0; String arr[]=st.split(" "); System.out.println("Number...how to count words in string using java  how to count words in string
Calculator program in Java
Calculator program in Java is used by programmer to add, subtract, multiply or divide two integer numbers that are input by system. In the given example a class "calculator" is used. System.in takes the input from the system/user at run
Java Count Vowels
Java Count Vowels       In this program you will learn how to count vowels in a String. Here you... a variable- count = 0. Now, we have applied a loop here which will go up
Count number of "*"
:"); String text = bf.readLine(); int count = 0; for (int i = 0; i...Count number of "*"  I have this code to count the number of * from...); if (c=='*' ) { count
Java Get Time in MilliSeconds
Java Get Time in MilliSeconds   ... the particular time, go through the below given example that illustrate how to get...;   Java Syntax to get time in milliseconds import 
Java program to generate the total count
Java program to generate the total count  I need a java program which would take a text file as an input and scans through it to generate the output.... END OF TEXT FILE The output required is: Oranges:8 Apples:14 Bananas:0
program on factorial of a given number in java
program on factorial of a given number in java  please tell me, how to write program on factorial of a given number in java
program on factorial of a given number in java
program on factorial of a given number in java  please tell me, how to write program on factorial of a given number in java
Maven dependency for com.adeptj - adeptj-parent version 9 is released. Learn to use adeptj-parent version 9 in Maven based Java projects
( com.adeptj - adeptj-parent version 9 ) in their Java project if it is based... in project? Steps to use  com.adeptj - adeptj-parent version 9 in Java projects... version 9 java library in your project. Now you can save the file in Eclipse
Factorial Program in Java
Factorial program in Java helps programmer to write factors of any given.... System.in is used to take the input from system/user at run time. InputStreamReader reads the input from user and then keep it into a buffer. As Java reads
Java Word Count - Word Count Example in Java
Java Word Count - Word Count Example in Java  ... to count the number of lines, number of words and number of characters... some strings and program will count the number of characters and number of words
how to count no.of sheets in excel through POI jar as well as through Java?
how to count no.of sheets in excel through POI jar as well as through Java... am able to read one sheet at a time by using POI jar(workbook.getSheetAt(0... know the method to count the no.of sheets in excel through POI jar or else java
Count number of occurences and print names alphabetically in Java
Count number of occurences and print names alphabetically in Java  I... for the printCount() method for the code to count the number of occurences of each... i=0;i<st.length;i++){ str+=st[i
get absolute time java
get absolute time java  How to get absolute time in java
MySql Time Interval
MySql Time Interval This example illustrates how to get date and time between specific time interval. In this example we create a query "select lastAccess from emp WHERE lastAccess BETWEEN '2008-09-01' AND '2008-12-01'"
difference between servletconfig and servletcontext in java
difference between servletconfig and servletcontext in java  difference between servletconfig and servletcontext in java   ServletContext ServletContext is implemented by the servlet container for all servlet
Comparison between the types of sorting in java
Comparison between the types of sorting in java  welcome all i wanna program in java compare between selection,insertion,bubble,merge,quick sort In terms of timer and put all types in frame and find the timer for example array
difference between wite and writeto in java
difference between wite and writeto in java  I am using the stream... the writeTo() method. I want to know difference between the write() and writeTo() method in java.   The writeTo() method writes the complete contents
Java count characters from file
Java count characters from file In this section, you will learn how to count...++) { char ch = third[counter]; int count = 0; for (int i = 0; i <... into string. Then we have removed all the spaces between the string using replaceAll

Ads