aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes More complicated Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "More complicated Program" Watch "More complicated Program" New topic
Author

More complicated Program

Ganesh Ravi Kumar
Greenhorn

Joined: Dec 06, 2010
Posts: 17
Write a program to find and print how many times the number 8 occurs in an array A. The
array A should take as input as many values as the user wishes to enter.

I tried this program......but still i didnt fix the problem. i wants to know the program logic.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
What is "this program"? Did you mean to post what you have done so far?
Ganesh Ravi Kumar
Greenhorn

Joined: Dec 06, 2010
Posts: 17
var A=new Array();
do{
A[i]=prompt("Enter the value","");
i++;
}while(i<10);
for(j=0;j<i;j++){
if(A[j]==8){
noOfOccurance+=1;
}
}
document.write("Number of times eight occured from the given input is : "+noOfOccurance);


i have done upto this.....but this only works upto 10 inputs,that i mentioned in do while loop. but the program has to get as many as input from the user based on his wish.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Ganesh Ravi Kumar wrote:var A=new Array();
i have done upto this.....but this only works upto 10 inputs,that i mentioned in do while loop. but the program has to get as many as input from the user based on his wish.




I think you need to find a way to get rid of that 10.

Eric
Ganesh Ravi Kumar
Greenhorn

Joined: Dec 06, 2010
Posts: 17
that i know.....im asking how to solve this issue........
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Does your homework tell you what the user should type when they are done entering in numbers?

Eric
Ganesh Ravi Kumar
Greenhorn

Joined: Dec 06, 2010
Posts: 17
no......but it may be like this.......when using "prompt" to get input from the user.........if he/she clicks "cancel" button in prompt.........the retrieval of input from the user should terminate from that time. this may give some idea to you.........
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
This should help: http://www.javascripter.net/faq/canceled.htm
Ganesh Ravi Kumar
Greenhorn

Joined: Dec 06, 2010
Posts: 17
this is ok........but the loop should iterate as far as the user inputs...........that is for n number of times...........but we are not sure, what is the value of n here, it is based on the user interest........
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Do you understand why I posted that link? What do you think the relationship is between what's described on that page and your last question?
Ganesh Ravi Kumar
Greenhorn

Joined: Dec 06, 2010
Posts: 17
i understood what that link is for.........but my query is how to get the values from the user for unknown number of times........when the user hits cancel,upto that our prompt has to come again and again.........and has to hold the values that are received from the user.we dont know how many times the user will give input..........its totally based on his wish.........i just gave a clue to understand the logic by using "prompt".
K West
Ranch Hand

Joined: Sep 10, 2008
Posts: 58

First, as Eric said you have to get rid of while loop. It will not help.
Second, as Lester said you have to bring to use the link.

Its simple, once you accept the value in input box, check the value. If its null, exit and calculate the number of occurrence else call the same function again to accept more inputs.
Ganesh Ravi Kumar
Greenhorn

Joined: Dec 06, 2010
Posts: 17
ya i have cleared that one.......now the problem is......i have to display how many digits of 8 in the given input..........For eg: 8008,5808,4008,6,9,25,18...........think that these are the inputs........i have to find how many time the digit 8 replicated in the given input.........
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: More complicated Program