Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
I get an error: "The local variable y may not have been initialized." How would I initialize it?

Code:
package numberdoub;
import java.util.*;
public class Numberdoub
{
	static double redouble (double x, double y){
		
		//equation
		while (y < 1000){
		y = 2*x;}
		return x;
	}
	
	public static void main(String[] args) {
		
		//defines variable
		double x, y;
		
		Scanner sc = new Scanner (System.in);
		
		//user input
		System.out.print("x = ");
		x = sc.nextDouble();

		//answers
		System.out.println("The answer is: " + y);		
	}
}
 

kpua

macrumors 6502
Jul 25, 2006
294
0
The problem is, you never set y to anything in main() before you print it out. What do you expect the program to print out there?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I think i understand what you're supposed to do, which is read in a value from the user, and if that number is less than 1000, double it. If the result is less than 1000, double it again, and so on. Once you've gotten a value that is greater than or equal to 1000, display it.

Your redouble function only needs to take one argument, the original value.

You're pretty close on this one. After you've read a number from your scanner, you just need to pass it to your function.

Your function to do the redoubling is almost there, but it needs some tweaks. It only needs to deal with a single value that will eventually be returned.

In your main function, you can either print the result of that thing, or store it and print that.

-Lee
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
I tried to only have one variable, but then the answer is the same as what I put in. Here is the code:
Code:
package numberdoub;
import java.util.*;
public class Numberdoub
{
	static double redouble (double x){
		
		//rd method
		while (x < 1000){
		x = 2*x;}
		return x;
	}
	
	public static void main(String[] args) {
		
		//defines variable
		double x;
		
		Scanner sc = new Scanner (System.in);
		
		//user input
		System.out.print("x = ");
		x = sc.nextDouble();

		//answers
		System.out.println("The answer is: " + x);		
	}
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I tried to only have one variable, but then the answer is the same as what I put in. Here is the code:
Code:
package numberdoub;
import java.util.*;
public class Numberdoub
{
	static double redouble (double x){
		
		//rd method
		while (x < 1000){
		x = 2*x;}
		return x;
	}
	
	public static void main(String[] args) {
		
		//defines variable
		double x;
		
		Scanner sc = new Scanner (System.in);
		
		//user input
		System.out.print("x = ");
		x = sc.nextDouble();

		//answers
		System.out.println("The answer is: " + x);		
	}
}[/QUOTE]

redouble looks great. Now you need to use it. You never call redouble. Just because you called the variable in redouble x, and you have an x in main, doesn't mean they are the same variable. You will need to call redouble from main, and pass it main's x. What it returns will be your answer, so you'll need to do something with it.

-Lee
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
I think I used redouble in the right way. The answer is still the same, though.
Code:
package numberdoub;
import java.util.*;
public class Numberdoub
{
	static double redouble (double x){
		
		//rd method
		while (x < 1000){
		x = 2*x;}
		return x;
	}
	
	public static void main(String[] args) {
		
		double redouble = 0;
		double x;
		
		Scanner sc = new Scanner (System.in);
		
		//user input
		System.out.print("x = ");
		redouble = sc.nextDouble();

		//answers
		System.out.println("The answer is: " + x);		
	}
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I think I used redouble in the right way. The answer is still the same, though.
Code:
package numberdoub;
import java.util.*;
public class Numberdoub
{
	static double redouble (double x){
		
		//rd method
		while (x < 1000){
		x = 2*x;}
		return x;
	}
	
	public static void main(String[] args) {
		
		double redouble = 0;
		double x;
		
		Scanner sc = new Scanner (System.in);
		
		//user input
		System.out.print("x = ");
		redouble = sc.nextDouble();

		//answers
		System.out.println("The answer is: " + x);		
	}
}[/QUOTE]

I know for sure we went over this before. =)
You don't need to declare the type of functions in java. this is not fortran.
You were assigning sc.nextDouble() to x. That was good. You then need to call the function redouble on x, and do something with its result. Calling a function that returns a value looks like:
[CODE]double myResult = myDoubleFunction(myArgument);

-Lee
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
Like this?

Code:
package numberdoub;
import java.util.*;
public class Numberdoub
{
	static double redouble (double x){
		
		//rd method
		while (x < 1000){
		x = 2*x;}
		return x;
	}
	
	public static void main(String[] args) {
		
		double redouble = 0;
		double x;
						
		Scanner sc = new Scanner (System.in);
		
		//user input
		System.out.print("x = ");
		redouble = sc.nextDouble();

		x = redouble(x);
		
		//answers
		System.out.println("The answer is: " + redouble(x));		
	}
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Like this?

Code:
package numberdoub;
import java.util.*;
public class Numberdoub
{
	static double redouble (double x){
		
		//rd method
		while (x < 1000){
		x = 2*x;}
		return x;
	}
	
	public static void main(String[] args) {
		
		double redouble = 0;
		double x;
						
		Scanner sc = new Scanner (System.in);
		
		//user input
		System.out.print("x = ");
		redouble = sc.nextDouble();

		x = redouble(x);
		
		//answers
		System.out.println("The answer is: " + redouble(x));		
	}
}[/QUOTE]

Very nearly. For one, get rid of double redouble = 0;
Next, you need to assign the value from your Scanner to what you're going to pass to redouble.
Lastly, you only need to call redouble on x once. Now you're doing it, and assigning the result to x (so x has the result from redouble in it), then you're calling it again in your print statement. You can do it in one or the other. In this case it won't matter, because calling redouble on something over 1000 should result in the same value as you passed in, in general you want to call a function like this only once.

-Lee
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
IT WORKS!!!! YAY!! Thanks so much. Just one more to go!
Code:
package numberdoub;
import java.util.*;
public class Numberdoub
{
	static double redouble (double x){
		
		//rd method
		while (x < 1000){
		x = 2*x;}
		return x;
	}
	
	public static void main(String[] args) {
		
		double x;
						
		Scanner sc = new Scanner (System.in);
		
		//user input
		System.out.print("x = ");
		x = sc.nextDouble();

		//answers
		System.out.println("The answer is: " + redouble(x));		
	}
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
IT WORKS!!!! YAY!! Thanks so much. Just one more to go!
Code:
package numberdoub;
import java.util.*;
public class Numberdoub
{
	static double redouble (double x){
		
		//rd method
		while (x < 1000){
		x = 2*x;}
		return x;
	}
	
	public static void main(String[] args) {
		
		double x;
						
		Scanner sc = new Scanner (System.in);
		
		//user input
		System.out.print("x = ");
		x = sc.nextDouble();

		//answers
		System.out.println("The answer is: " + redouble(x));		
	}
}[/QUOTE]

Great! =)
One tip, though... what if x is entered as -2? I am not sure if you're supposed to consider this, but in reality it is very important, as no matter how many times you double a negative number, it will never get over 1000 (unless there is an overflow, but i believe java will die with an exception instead of overflowing).

There is a Math.abs() function that will give you the absolute value, otherwise it's pretty easy to fake yourself.

-Lee

Edit: I was wrong, even for integers Java allows for +/-Infinity. So this will lead to an infinite loop instead of a crash condition, which is probably worse depending on who you ask.
 

Cromulent

macrumors 604
Oct 2, 2006
6,802
1,096
The Land of Hope and Glory
That would make the answer 0 which isn't what I want.

No it wouldn't. When you declare a variable such as:

double y;

you have no idea what it contains. It is garbage, so what you do is initialise the variable so that it contains a safe value like so:

double y = 0;

that way you know what it contains from the start. Then you just put the new value that you want it to contain in as normal. It is good practice to always initialise variables to a safe value when you declare them.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.