0

I am truly at a loss I have been searching everywhere. I have been given this code

public class Main { public static void main(String args[]) { int target = (int) (Math.random() * 1000); System.out.println("The number is " + target); } } 

They want me to create a class Finder that takes the target and uses it in a for statement. my problem is I can not figure out how to pull target from this code. I am a student and the book we are using is no help. I have tried calling it with

numberSearch = Main.target numberSearch = Main.main.target numberSearch = Main.main() 

and many others.

2
  • try making target global. however, if you dont have access to the class Main, i dont think you can do it. Commented Aug 14, 2015 at 19:12
  • The true answer to this question depends on your interpretation of "takes the target". You're interpreting it as steal/grab, where Finder will take the value from another class. I'm interpreting it as accept, where target is given to the Finder by the other class and Finder will take the value handed to it. The accept pattern (also known as injection) is much better for testing and code isolation, advance code concepts you'll hopefully learn in good time. Commented Aug 14, 2015 at 20:20

4 Answers 4

1

You said "create a class Finder that takes the target", which means that the Finder class should accept the target value as a parameter, either on the constructor, or on the method that performs the find.

// Using constructor public class Finder { private int target; public Finder(int target) { this.target = target; } public void find() { // perform find of 'this.target' here } } // On find method public class Finder { public void find(int target) { // perform find of 'target' here } } 
Sign up to request clarification or add additional context in comments.

Comments

1

First off, lets order your code using tab

public class Main { public static void main(String args[]) { int target = (int) (Math.random() * 1000); System.out.println("The number is " + target); } } 

Now what you want to do is create an object of the class you say you need to use in school (Finder). Lets make the identifier for this object "obj".

public class Main { public static void main(String args[]) { Finder obj = new Finder(); int target = (int) (Math.random() * 1000); System.out.println("The number is " + target); } } 

Now that we have done this you have to create a method in the class Finder that takes in an integer (so it can take in the variable you call target since it is an int itself). And example of a method like this that uses the variable target in a for-loop would be:

public void forLoopMethod(int target) { //Some sort of for loop involving the int target goes here: for() { } } 

And then simply calling the method forLoopMethod in your class called Main

public class Main { public static void main(String args[]) { Finder obj = new Finder(); int target = (int) (Math.random() * 1000); obj.forLoopMethod(target); System.out.println("The number is " + target); } } 

Comments

1

You can not access local variable outside method

1 Comment

that's a comment, not an answer
0

Your problem is that you are trying to find a local variable in a separate class. Variables can be set outside of a method like main(), and if they are public variables, they can be easily accessed by another class.

Outside of the main() function, inside the Main class, you would place the numberSearch variable. Then inside Finder you can access numberSearch by getting Main.numberSearch's value.

Be aware that if your professor wants you to use a private variable, you will need to use getters and setters.

/*How I tested the code*/ public class Main { public static int numberSearch; public static void main(String args[]) { int target = (int) (Math.random() * 1000); numberSearch = target; Finder.getResult(); } } /*in a separate file*/ public class Finder { public static void getResult() { int t = Main.numberSearch; System.out.println(t); } } 

4 Comments

what? the variable isn't within the class
You're right, getting some things mixed up. I'll make edits.
I made the edit at the same time as you and it says that the non-static variable numberSearch can not be referenced from a static context.
@recebba1 Try making numberSearch a public static int

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.