简体   繁体   中英

Why is my return value not holding a value in my main method?

The issue I am having is where the break in my code is. I have commented out sections too see where it breaks and cannot seem to find it. What happens now is, once the stringMethod is called the for loop does nothing and b is never used. I'm not sure why a isn't holding the same value from my method. Any help is appreciated and if I wasn't clear about something, just ask.

import java.util.Scanner;

public class TestTwoClassMain {
static Scanner input = new Scanner(System.in);

public static void main(String[] args) {

    String j = "", b = "", c = "", a = "";

    do {
        stringMethod();

        for (int i = j.length() - 1; i >= 0; i--)
            b = b + a.charAt(i);
//the above 'a' is not holding any value for the sentence entered in method

        System.out.println("Reverse: " + b);
        System.out.print("Try Again? ");
        c = input.nextLine();
    } while (c.equalsIgnoreCase("YES"));
}

public static String stringMethod() {
    String a = "";
    System.out.print("Enter: ");
    a = input.nextLine();
    return a;
}

}

You need to assign your a variable to the output of stringMethod()

a = stringMethod()

You need to do this because you've declared a within the scope of your main method, as opposed to declaring it within the scope of the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM