Java 如何从其他方法访问用户输入

Java 如何从其他方法访问用户输入,java,user-input,Java,User Input,我正在尝试使用其他方法计算用户输入的名称的长度。当我试图从我的方法访问用户输入“ipnut”时,它有一个错误。我的代码有什么问题 import java.util.Scanner; public class LengthOfName { public static void main(String[] args) { Scanner reader = new Scanner(System.in); System.out.println("Type y

我正在尝试使用其他方法计算用户输入的名称的长度。当我试图从我的方法访问用户输入“ipnut”时,它有一个错误。我的代码有什么问题

import java.util.Scanner;

public class LengthOfName {
    public static void main(String[] args) {
         Scanner reader = new Scanner(System.in);
         System.out.println("Type your name: ");
         String input = reader.nextLine();


   calculateCharecters(text);

}
public static int calculateCharecters(String text){

    int texts = input.length();
    System.out.println("Number of charecters: " + texts);
    return texts;
}
}

应该是:

calculateCharecters(input);
您需要将输入传递给您的方法


text是您正在调用的方法的参数。您应该将输入传递给text。

计算字符(text);
更改为
计算字符(input);


更改
计算字符(文本)
应为
计算字符(输入)

input.length()
应该是
text.length()

你可以在你的主要时间做这件事

int characterLength = calculateCharacters(input); //because your method return an int
System.out.println("Number of charecters: " + characterLength);

啊!我知道这很简单。我只需要将用户输入放入参数中。应该是这样。您没有名为text.rt的变量吗?
import java.util.Scanner;

public class LengthOfName {
    public static void main(String[] args) {
         Scanner reader = new Scanner(System.in);
         System.out.println("Type your name: ");
         String input = reader.nextLine();


   calculateCharecters(input);

}
public static int calculateCharecters(String text){

    int texts = input.length();
    System.out.println("Number of charecters: " + texts);
    return texts;
}
}
public static void main(String[] args) {

    Scanner reader = new Scanner(System.in);
    System.out.println("Type your name: ");
    String input = reader.nextLine(); //"input" store the user input

    calculateCharecters(input); //and go through this metod

}

public static int calculateCharecters(String text) { // now "text" store the user input 

    int texts = text.length(); //here "texts" store the "text" lenght (text.lenght()) or number
                               //of characters

    System.out.println("Number of charecters: " + texts); //printing number of characters
    return texts; //returning number of characters so
}
int characterLength = calculateCharacters(input); //because your method return an int
System.out.println("Number of charecters: " + characterLength);