aspose file tools*
The moose likes Beginning Java and the fly likes Getting same numbers 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 » Java » Beginning Java
Bookmark "Getting same numbers" Watch "Getting same numbers" New topic
Author

Getting same numbers

Jack Terry
Greenhorn

Joined: May 12, 2008
Posts: 10
Hey guys, I was making a programm to get the ln[i]-ln[i+1] from i=0 till i=30 using a method, with a input choosing the number of answers on 1 row. But after I made this programm, the numbers that came out are all the same!
Does anyone know how to solve this problem? o.0? Thanks alot!



[ May 12, 2008: Message edited by: fred rosenberger ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

well, without looking deeply at your code, i suggest the following line of attack.

you are either not putting anything into the array, or you are not correctly printing out the array.

I'd suggest putting a few "System.out.println()" statements in your code. for example, every time you try inserting something into the array, print out the value.

put in a print statement showing the result of your (i+1)%z. and a few other places.

FWIW, 'z' is a horrible variable name. why not use something meaningful, like 'numOfInputs'?

also, why does your loop go from 0-30? where does the 30 and the 32 come from i see in the code? these are called 'magic numbers', because they appear like magic. Nobody knows what they really mean. if you only want to loop for the number of times the user input, then loop like this:

for (i=0; i<z; i++)

lastly, note that each time through your loop, you call the RedoMethod.arr() function, thus re-bulding your array MANY times...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18656
    
    8

"Le Bron Jamess", please check your private messages regarding an important administrative matter.
Jack Terry
Greenhorn

Joined: May 12, 2008
Posts: 10
Originally posted by fred rosenberger:
well, without looking deeply at your code, i suggest the following line of attack.

you are either not putting anything into the array, or you are not correctly printing out the array.

I'd suggest putting a few "System.out.println()" statements in your code. for example, every time you try inserting something into the array, print out the value.

put in a print statement showing the result of your (i+1)%z. and a few other places.

FWIW, 'z' is a horrible variable name. why not use something meaningful, like 'numOfInputs'?

also, why does your loop go from 0-30? where does the 30 and the 32 come from i see in the code? these are called 'magic numbers', because they appear like magic. Nobody knows what they really mean. if you only want to loop for the number of times the user input, then loop like this:

for (i=0; i<z; i++)

lastly, note that each time through your loop, you call the RedoMethod.arr() function, thus re-bulding your array MANY times...



THe 30 is because i want 30 answers coming out, so the z isnt the number of inputs, but the number of answers on each row. The array works, because if i edit the last line:

return answer[i-1]

or

return answer[i-10]

and the programm gives the answer, but like 30 times the SAME answer. Should I introduce a new variable? The problem is getting the same answer back, because without using a new method the programm works.

Thanks for your tip of putting printlines, i'm tryin now.
Rodrigo Tomita
Ranch Hand

Joined: Apr 28, 2008
Posts: 70
Jack,

Think for a moment about what you are doing in the code below:




I think you need to think about whether your method arr() is going to do the calculations for all the 30 numbers at once (so you have a for inside it) or if it is supposed to give you an answer for just one item (the one you gave by passing i as an argument).

Also, think about the role of the i coming as argument of arr() the way you coded it - does it matter if you set i = 0 or i = 10?

Good luck.

Rodrigo
[ May 12, 2008: Message edited by: Rodrigo Tomita ]
Jack Terry
Greenhorn

Joined: May 12, 2008
Posts: 10
Originally posted by Rodrigo Tomita:
Jack,

Think for a moment about what you are doing in the code below:




I think you need to think about whether your method arr() is going to do the calculations for all the 30 numbers at once (so you have a for inside it) or if it is supposed to give you an answer for just one item (the one you gave by passing i as an argument).

Also, think about the role of the i coming as argument of arr() the way you coded it - does it matter if you set i = 0 or i = 10?

Good luck.

Rodrigo

[ May 12, 2008: Message edited by: Rodrigo Tomita ]


Thanks for your answer. Yes i want different answers, so it should only print each answer of one item. I thougt this was already achieved by using:

//Perform calculation of ln first
for (i=0; i<31; i++){
ln[i]=Math.log(i/10.);
}
//Now ln[i+1] is defined, answer can be defined
for(i=0; i<30; i++){
answer[i] = ln[i]-ln[i+1];
}

I thougt if i is running from zero to 30, then the array answer[i] contains different numbers. And by the way which i do you mean?

Thanks alot!
Rodrigo Tomita
Ranch Hand

Joined: Apr 28, 2008
Posts: 70
Jack,

Yes, you are correct. What I'm trying to say is that the way your arr() method is designed, you do all the calculations at once and store it to your answer[] array. Then check what you return.

Also, notice that you call the arr() method 3 times in your main method. That's why I'm asking you if your point is to do all the calculations inside arr() and return all the results to main or just give the answer back for a given i.

Try to put some println(i) in your code and check it out. Sorry if it sounds vague, but I'm trying to do the "focus on helping them discover their own solutions, instead of simply providing answers."
Jack Terry
Greenhorn

Joined: May 12, 2008
Posts: 10
Originally posted by Rodrigo Tomita:
Jack,

Yes, you are correct. What I'm trying to say is that the way your arr() method is designed, you do all the calculations at once and store it to your answer[] array. Then check what you return.

Also, notice that you call the arr() method 3 times in your main method. That's why I'm asking you if your point is to do all the calculations inside arr() and return all the results to main or just give the answer back for a given i.

Try to put some println(i) in your code and check it out. Sorry if it sounds vague, but I'm trying to do the "focus on helping them discover their own solutions, instead of simply providing answers."



Hahaha its nice to learn myself, but I already used 8 hours finding out whats wrong XD
Jack Terry
Greenhorn

Joined: May 12, 2008
Posts: 10
I put some printlines in method arr:

//Definition of method arr
public static double arr(int i){

//Declare local variables
double [] ln = new double [31]; //natural logarithm
double [] answer = new double [31]; //answer of ln[i]-ln[i+1]

//Perform calculation of ln first
for (i=0; i<31; i++){
ln[i]=Math.log(i/10.);
System.out.println(ln[i]);
}
//Now ln[i+1] is defined, answer can be defined
for(i=0; i<30; i++){
answer[i] = ln[i]-ln[i+1];
System.out.println(answer[i]);
}

And the result is a long list of DIFFERENT numbers. So I think the problem is getting these numbers back to the main method... How (not) to do?
Rodrigo Tomita
Ranch Hand

Joined: Apr 28, 2008
Posts: 70
ok, so let's cut to the chase

Suppose that you call you method: arr(10), so i == 10 at that point.

Then you have a for loop using the same i - so the i == 0 at the begin of the first loop and will be 30 at the end of it.

Then you have a second for loop using again i - so i == 0 at the begin of the loop and will be 29 at the end of it.

Then you do a return answer[i]. The i variable is certainly not equals to 10 at that point, right?

So, you are in the right path. Answering to your question, I believe it would make more sense if you return answer (the whole array) to your main(). That way you do the calculations only once. So your code would look like:



Hope it helps.

[ May 12, 2008: Message edited by: Rodrigo Tomita ]
[ May 12, 2008: Message edited by: Rodrigo Tomita ]
Jack Terry
Greenhorn

Joined: May 12, 2008
Posts: 10
Originally posted by Rodrigo Tomita:
ok, so let's cut to the chase

Suppose that you call you method: arr(10), so i == 10 at that point.

Then you have a for loop using the same i - so the i == 0 at the begin of the first loop and will be 30 at the end of it.

Then you have a second for loop using again i - so i == 0 at the begin of the loop and will be 29 at the end of it.

Then you do a return answer[i]. The i variable is certainly not equals to 10 at that point, right?

So, you are in the right path. Answering to your question, I believe it would make more sense if you return answer (the whole array) to your main(). That way you do the calculations only once. So your code would look like:


//Perform calculations
double[] answer = RedoMethod.arr(i);

//Print out the values of array3
for(i=0; i<30; i++){
if((i+1)%z==0)
System.out.println( answer[i] );
else
System.out.print( answer[i] );
}
}

//...

//Definition of method arr
public static double arr(){

//...

for (int i = 0; i < 30; i++) {
//...
}



Hope it helps.

[ May 12, 2008: Message edited by: Rodrigo Tomita ]

[ May 12, 2008: Message edited by: Rodrigo Tomita ]


Thanks for your answer. so you already define answer in the main method.
I tried this once, but the problem is an error occurs. I marked it bold(but I see now everything is bold).


double[] answer = RedoMethod.arr(i);



double [] answer is an array, but RedoMethod.arr(i) is just a number right? So the two types are incompatible...

[ May 12, 2008: Message edited by: Jack Terry ]
[ May 12, 2008: Message edited by: Jack Terry ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

but RedoMethod.arr(i) is just a number right?

no. it's a method call that RETURNS a number.
Jack Terry
Greenhorn

Joined: May 12, 2008
Posts: 10
Originally posted by fred rosenberger:

no. it's a method call that RETURNS a number.



But the types are still incompatible if i try to compile it.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

ughhh... ok. Remember how above I talked about using better variable names? look VERY CLOSELY at your arr() method.

you pass in a paramter, and call it i.

you then, in your first loop, use as an index a variable named... i. so, you've just blown away whatever data you passed in.

you use it AGAIN in the second loop, blowing it away again... so it would appear that by the time you exit, no matter what, i is going to be 31.
Jack Terry
Greenhorn

Joined: May 12, 2008
Posts: 10
Originally posted by fred rosenberger:
ughhh... ok. Remember how above I talked about using better variable names? look VERY CLOSELY at your arr() method.

you pass in a paramter, and call it i.

you then, in your first loop, use as an index a variable named... i. so, you've just blown away whatever data you passed in.

you use it AGAIN in the second loop, blowing it away again... so it would appear that by the time you exit, no matter what, i is going to be 31.


uhuh, that I know by know, but I simplified the questions I now still dont know:



It is like you have 4 variables, and use these as input to calculate, and get an answer in array form, and getting this answer BACK to the main method in array form (and printed out).

Thanks for the help!
[ May 12, 2008: Message edited by: Jack Terry ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
In your new TEST class, the method test() declared that it returns a double, but what you say you want to return, and what you're trying to return is a int[]. You have to return what the method specifies in its signature:


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Jack Terry
Greenhorn

Joined: May 12, 2008
Posts: 10
Thanks, that seemed to work. But now the problem in my 'original' problem:


//This program is a redo of assignment 3 part 1, but now with input of number of numbers diplayed on each row, and using methods

import java.text.*;
import java.io.*;
public class RedoMethod{

//Define main method
public static void main (String [] args) throws IOException {

//Declare variables
double avg; //average value of arr3
int z; //number of answers presented each row
int i=0; //Array index
double [] array = new double [32]; //The answer
int j;
//Introduce decimalformat
DecimalFormat df = new DecimalFormat ("0.000");

//Create bufferedreader
BufferedReader in1 = new BufferedReader( new InputStreamReader(System.in));

//Obtain number of answers presented each row
System.out.print("Enter the number of answers you want in 1 row: ");
z = Integer.parseInt( in1.readLine());

//Perform calculations
double [] answer = new double [31];

for (j=0; j<30; j++){
answer[j] = RedoMethod.arr(i);
}

//Print out the values of array3
for(j=0; j<30; j++){
if((j+1)%z==0)
System.out.println( answer[j]);
else
System.out.print( answer[j]);
}


}

//Definition of method arr
public static double [] arr(int i){

//Declare local variables
double [] ln = new double [31]; //natural logarithm
double [] answer = new double [31]; //answer of ln[i]-ln[i+1]

//Perform calculation of ln first
for (i=0; i<31; i++){
ln[i]=Math.log(i/10.);
//System.out.println(ln[i]);
}
//Now ln[i+1] is defined, answer can be defined
for(i=0; i<30; i++){
answer[i] = ln[i]-ln[i+1];
//System.out.println(answer[i]);
}

//Return answer
return answer;


}
}


I define: double [] answer = new double [31];
and: answer[j] = RedoMethod.arr(i);
and: //Definition of method arr
public static double [] arr(int i){

This seems right, because answer is a double and an array, and RedoMethod.arr(i) also gives a double array back. What is wrong here?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
There are lots of things wrong there. First you've declared your method to return a double[], but in the calling method you're trying to assign the result to an element of a double array, which is a double. Also, as others have said, you pass an int "i" to your method, and the first thing you do with it is reset it to zero. (The i in your loop is the same i in your method parameter).
Jack Terry
Greenhorn

Joined: May 12, 2008
Posts: 10
Originally posted by Garrett Rowe:
There are lots of things wrong there. First you've declared your method to return a double[], but in the calling method you're trying to assign the result to an element of a double array, which is a double. Also, as others have said, you pass an int "i" to your method, and the first thing you do with it is reset it to zero. (The i in your loop is the same i in your method parameter).



Thanks alot, the problem is (finally) solved
its the element part that I didnt understand, I've changed the code now into:



The line I couldn't understood in the beginning was:

array = RedoMethod.arr();

Now its copying the whole array, while first I wanted to put a whole array into an element of the array right?

Really thanks alot people!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting same numbers