array problem

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2007
Posts: 30
Reputation: ronghel is an unknown quantity at this point 
Solved Threads: 0
ronghel ronghel is offline Offline
Light Poster

array problem

 
0
  #1
Nov 19th, 2007
hello to all.. i got a machine problem which is finding how many times the inputted number occured.. hows thar?

inputted numberbufferedReader)

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..
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 30
Reputation: ronghel is an unknown quantity at this point 
Solved Threads: 0
ronghel ronghel is offline Offline
Light Poster

Re: array problem

 
0
  #2
Nov 19th, 2007
another thing how can i store the inputted value to the array?..
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: array problem

 
0
  #3
Nov 19th, 2007
Post the code that you do have and we can take a look at it.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 30
Reputation: ronghel is an unknown quantity at this point 
Solved Threads: 0
ronghel ronghel is offline Offline
Light Poster

Re: array problem

 
0
  #4
Nov 19th, 2007
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);

}
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: array problem

 
0
  #5
Nov 19th, 2007
Very close. Move the BufferedReader outside of your loop and put a try-catch block around the readLine() and parseInt() section.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 30
Reputation: ronghel is an unknown quantity at this point 
Solved Threads: 0
ronghel ronghel is offline Offline
Light Poster

Re: array problem

 
0
  #6
Nov 19th, 2007
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..
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 30
Reputation: ronghel is an unknown quantity at this point 
Solved Threads: 0
ronghel ronghel is offline Offline
Light Poster

Re: array problem

 
0
  #7
Nov 19th, 2007
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);






}
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 30
Reputation: ronghel is an unknown quantity at this point 
Solved Threads: 0
ronghel ronghel is offline Offline
Light Poster

Re: array problem

 
0
  #8
Nov 20th, 2007
bump
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: array problem

 
0
  #9
Nov 20th, 2007
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).
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 30
Reputation: ronghel is an unknown quantity at this point 
Solved Threads: 0
ronghel ronghel is offline Offline
Light Poster

Re: array problem

 
0
  #10
Nov 21st, 2007
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.?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1335 | Replies: 14
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC