hello to all.. i got a machine problem which is finding how many times the inputted number occured.. hows thar?

inputted number:(bufferedReader)

Enter number: 22
Enter number: 23
Enter number: 32
Enter number: 32
Enter number: 23

Number to be search: 2
Occurence : 6

i cant get it..
pls help me tnx..

Recommended Answers

All 14 Replies

another thing how can i store the inputted value to the array?..

Post the code that you do have and we can take a look at it.

wah, my only prob here is i dont know how to read the inputted value and store it to become an array..

is this correct? bufferedreader to array? this is the code...

for(int a=0;a<5;a++)
        {

        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Enter Number : ");

        str=br.readLine();

        value[a]=Integer.parseInt(str);

        }

Very close. Move the BufferedReader outside of your loop and put a try-catch block around the readLine() and parseInt() section.

    BufferedReader data=new BufferedReader(new InputStreamReader(System.in));

    System.out.print("Input number to be search: ");
        try
        {
            ent=Integer.parseInt(data.readLine());  }

            catch(IOException e)
        {
        }

is this correct? dunno how to store it as array..

peep.. my only problem now is counting the occurence...

enter number: 2
enter number: 222
enter number: 2
enter number: 222
enter number: 22

number to be searched is: 2
occurence: 10

but my occurence is showing only 2! it should be 10!

take a look at my code

import java.io.*;

public class MP2
{


    public static void main(String[] args) throws IOException


    {
        int value[]=new int[5];
        int sear=0;
        int d=0,c,a;
        int high=0, low=0;

        String str;


        for(a=0;a<5;a++)
            {

            BufferedReader data=new BufferedReader(new InputStreamReader(System.in));

            System.out.print("Enter Number : ");

            str=data.readLine();

            value[a]=Integer.parseInt(str);

            }
            BufferedReader data=new BufferedReader(new InputStreamReader(System.in));

            System.out.print("Input number to be search: ");
            str=data.readLine();

            sear=Integer.parseInt(str);

            for(c=0;c<5;c++)
            {
                if(sear==value[c])
                d++;
            }
            System.out.print("Occurence is "+d);






    }
}

bump

because 2 != 222 or 22. If you really need to match each digit, then you need to parse each digit of the number as an Integer, rather than the entire String, of course. Try to work something in, in between the readLine and the parseInt and then, if it still doesn't work, post that modified code and clearly explain the problem, with all error/compiler messages (if any).

yup i need to match each digit with my inputted number... is it possible to read each number and not as a whole... hw to parse each digit of the no.?

toCharArray() ?

Read the API for String.

if you need to match each digit or read each digit the best way i think is to make the whole number a string an go char by char just use the acsii value to get the exact char

ok.. yah.. i need to reach each digit .. not read the number as a whole so i can count the number to times that the number appeared... can someone give me the code hehe...

elp!

ok.. yah.. i need to reach each digit .. not read the number as a whole so i can count the number to times that the number appeared... can someone give me the code hehe...

They already told you how to do it. Get the number as a String and examine each character. In fact, you are reading the number as a String to begin with, so why bother converting it to an int if you just want to convert it back to a String? Read the API for String, there are plenty of very clear methods for looking at smaller pieces of that String.

Write some code for it and if you still can't get it to work, post it. Giving you the code won't help you learn a thing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.