Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

More complicated Program

 
Ganesh Ravi Kumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is "this program"? Did you mean to post what you have done so far?
 
Ganesh Ravi Kumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that i know.....im asking how to solve this issue........
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your homework tell you what the user should type when they are done entering in numbers?

Eric
 
Ganesh Ravi Kumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should help: http://www.javascripter.net/faq/canceled.htm
 
Ganesh Ravi Kumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.........
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic