There seems to be confusion in the control flow of your program. Remember that your program is a sequence of instructions. If you trace your control flow carefully, step by step, you will discover some problems. I will help detail what your program does and I will leave it to you to reason about it.
public static void main(String[] args) {
Noun(1); // (0)
System.out.println("Be kind to your "+Noun(0)); // (1)
}
I have added comments to label each statement. Your program always starts in the main method and on the first statement. Here is what main does.
(0) call Noun with on = 1, goto (1)
(1)
- call
Noun with on = 0, store result in tmp1
- append the string stored in
tmp1 to "Be kind to your ", store in tmp1
- print the string stored in
tmp1
I added extra detail to (1) and invented the variable name tmp1 to ease the explanation — your program will store the data somewhere.
It is important to note that calling a function yields control to that function. This means the caller does not execute its next statement until the callee has executed all of its statements.
public static String Noun(int on) {
String noun1; // (0)
Scanner input = new Scanner(System.in); // (1)
while (on == 1) { // (2)
System.out.println("Please enter a Noun."); // (3)
noun1 = input.nextLine(); // (4)
on = on - 1; // (5)
}
while (on == 0) { // (6)
noun1 = input.nextLine(); // (7)
}
return noun1; // (8)
}
Here is what the Noun function does.
(0) initialize noun1 to null, goto (1)
(1) construct a new Scanner object and store in input, goto (2)
(2) if on == 1 goto (3) else goto (6)
(3) print "Please enter a Noun.", goto (4)
(4) read a line of input and store into noun1, goto (5)
(5) store on - 1 into on, goto (2)
(6) if on == 0 goto (7) else goto (8)
(7) read a line of input and store into noun1, goto (6)
(8) halt and return the value of noun1
Notice there are a few problems just considering Noun by itself. I will focus on just one to help you get started.
If on is not equal to 0 or 1 then Noun will return null, which is why your IDE warns you. Why? To answer that, we just have to assume the hypothetical situation that on is neither 0 nor 1 and then manually execute the Noun function.
For simplicity I will choose on to be 3. I will keep track of the state of all variables as the function executes. To the left of the state I will indicate which statement I am executing, which I have decided only by following the program.
(0) on = 3, noun1 = null
(1) on = 3, noun1 = null, input = new Scanner(System.in)
(2) going to (6) because on is not equal to 1 on = 3, noun1 = null, input = new Scanner(System.in)
(6) going to (8) because on is not equal to 0 on = 3, noun1 = null, input = new Scanner(System.in)
(8) halting and returning value of noun1 on = 3, noun1 = null, input = new Scanner(System.in)
I suspect there is also confusion with variable scope. This indicates the lifetime of the variable. I will just briefly mention that variables declared in a function are destroyed (go out of scope) when the function halts (returns a value or throws an exception).