954,219 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

array problem

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..

ronghel
Light Poster
32 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

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

ronghel
Light Poster
32 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

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

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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);

}

ronghel
Light Poster
32 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

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

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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..

ronghel
Light Poster
32 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

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);


}
}

ronghel
Light Poster
32 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

bump

ronghel
Light Poster
32 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

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).

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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.?

ronghel
Light Poster
32 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

toCharArray() ?

Read the API for String.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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

uonsin
Newbie Poster
17 posts since Oct 2007
Reputation Points: 8
Solved Threads: 2
 

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...

ronghel
Light Poster
32 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

elp!

ronghel
Light Poster
32 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 
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.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You