944,131 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1523
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 19th, 2007
0

array problem

Expand Post »
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..
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
ronghel is offline Offline
32 posts
since Sep 2007
Nov 19th, 2007
0

Re: array problem

another thing how can i store the inputted value to the array?..
Reputation Points: 10
Solved Threads: 0
Light Poster
ronghel is offline Offline
32 posts
since Sep 2007
Nov 19th, 2007
0

Re: array problem

Post the code that you do have and we can take a look at it.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 19th, 2007
0

Re: array problem

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

}
Reputation Points: 10
Solved Threads: 0
Light Poster
ronghel is offline Offline
32 posts
since Sep 2007
Nov 19th, 2007
0

Re: array problem

Very close. Move the BufferedReader outside of your loop and put a try-catch block around the readLine() and parseInt() section.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 19th, 2007
0

Re: array problem

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..
Reputation Points: 10
Solved Threads: 0
Light Poster
ronghel is offline Offline
32 posts
since Sep 2007
Nov 19th, 2007
0

Re: array problem

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






}
}
Reputation Points: 10
Solved Threads: 0
Light Poster
ronghel is offline Offline
32 posts
since Sep 2007
Nov 20th, 2007
0

Re: array problem

bump
Reputation Points: 10
Solved Threads: 0
Light Poster
ronghel is offline Offline
32 posts
since Sep 2007
Nov 20th, 2007
0

Re: array problem

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).
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 21st, 2007
0

Re: array problem

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

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: integer comparison
Next Thread in Java Forum Timeline: Oracle datetype and format issue





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC