Hello there, thank you for taking the time to read my question. Anyways, on to the matter. I have two files called "test" and "test2".
Here's the code for "test":
And here is the code for "test2":
What I was hoping for when I did this is that the value of "amt" from file "test" would be passed down through the getAmt() method and its value would be successfully printed out using method printNum(). Although this is not the case, because the output states that it re-initializes back to zero. Say for example I entered the number "2" when prompted. This is the output:
OUTPUT:
Input number: //Prompt to input number
2 //The value of amt in file "test"
0.0 //The value of amt in file "test2"
Press any key to continue . . .
In other words, I would like the value of amt in test one to be the same as the one in test2. I am relatively new to Java and I am sorry if I bother anyone with something that "should" be incredibly obvious. Any suggestions would be greatly appreciated. Thank you.
Here's the code for "test":
Code:
import java.util.Scanner;
public class test
{
private double amt;
Scanner input = new Scanner( System.in);
public void setAmt()
{
System.out.println("Input number: "); //User imputs number here
double wagerAmt = input.nextDouble();
amt = wagerAmt; //Assigns wagerAmt to amt
}
public double getAmt()
{
return amt;
}
}
Code:
import java.util.Scanner;
public class test2
{
public void printNum()
{
test a = new test();
System.out.println(a.getAmt()); //Prints amt from file "test"
}
public static void main(String args[])
{
test a = new test();
a.setAmt(); //calls method setAmt() from file "test"
test2 b = new test2();
b.wagerNum(); //calls method printNum() from above method, although this does not function properly.
}
}
OUTPUT:
Input number: //Prompt to input number
2 //The value of amt in file "test"
0.0 //The value of amt in file "test2"
Press any key to continue . . .
In other words, I would like the value of amt in test one to be the same as the one in test2. I am relatively new to Java and I am sorry if I bother anyone with something that "should" be incredibly obvious. Any suggestions would be greatly appreciated. Thank you.
Comment