【问题标题】:How can I use a user input String from one method in another?如何在另一种方法中使用用户输入字符串?
【发布时间】:2022-01-11 05:43:19
【问题描述】:

此程序的这一点是将用户名的字符长度限制为 20 个字符。它是当前仅包含 Main 方法的较大程序的一部分。为了清理和澄清我的代码,我想将各种函数分成不同的方法。

目前,我正在尝试设置类变量,以便它们可以在多种方法中使用。这是我目前所拥有的:


public class Program
{
    Scanner read = new Scanner(System.in);
    String firstName = read.nextLine();
    String lastName = read.nextLine();
    
    public void main(String[] args) {
        domainCharLimit();
    }
    
    public void domainCharLimit() {
        String firstNameNew = firstName.replace("'", "");
        String lastNameNew = lastName.replace("'", "");
        String domainUsername = firstNameNew + "." + lastNameNew;
        if (domainUsername.length()>20) {
                String cutName = domainUsername.substring(0, 20);
                domainUsername = cutName;
            }
        System.out.print(domainUsername);
    }    
}

我尝试将一种或两种方法设置为静态,但未能解决问题。在这种状态下,程序运行时不会返回错误而是给出“无输出”

【问题讨论】:

    标签: java string methods


    【解决方案1】:

    主要方法必须是静态的!它是你程序的入口,它的签名必须是这样的。

    为了在其中调用非静态方法,您需要实例化一个对象并在该对象上调用它。在你的情况下,像

    public static void main(String[] args) {
       Program p =  new Program();
       p.domainCharLimit();
    }
    

    【讨论】:

      【解决方案2】:

      第一: Main Method 应该始终是static

      第二:因为你是从Main(static)调用domainChatLimit(),所以它也应该是静态的

      第三:因为你在静态方法domainChatLimit()中使用了firstNamelastName属性,那么它们也应该是静态的

      第四个:Scanner 也应该是静态的,因为您使用它来获取firstNamelastName,它们都是静态的。

      注意:无需定义此类的新实例即可调用内部方法

      解决方案应该如下(测试成功):

      import java.util.Scanner;
      
      public class Program{
      
              // variables below should be defined as static because they are used in static method
             static Scanner read = new Scanner(System.in);
             static String firstName = read.nextLine();
             static String lastName = read.nextLine();
              
             // Main method is static
              public static void main(String[] args) {
                  //There is no need to define a new instance of this class to call an internal method
                  domainCharLimit();
              }
              
              // calling from main static method so it should be static
              public static void domainCharLimit() {
                  String firstNameNew = firstName.replace("'", "");
                  String lastNameNew = lastName.replace("'", "");
                  String domainUsername = firstNameNew + "." + lastNameNew;
                  if (domainUsername.length()>20) {
                          String cutName = domainUsername.substring(0, 20);
                          domainUsername = cutName;
                      }
                  System.out.print(domainUsername);
              }   
      }
      

      如果您想为该功能创建一个通用实用程序,您可以执行以下逻辑:

      PogramUtil.java
      
      import java.util.Scanner;
      
      public class ProgramUtil {
      
          Scanner read = new Scanner(System.in);
          String firstName = read.nextLine();
          String lastName = read.nextLine();
          
          
          public void domainCharLimit() {
              String firstNameNew = firstName.replace("'", "");
              String lastNameNew = lastName.replace("'", "");
              String domainUsername = firstNameNew + "." + lastNameNew;
              if (domainUsername.length()>20) {
                      String cutName = domainUsername.substring(0, 20);
                      domainUsername = cutName;
                  }
              System.out.print(domainUsername);
          }  
      }
      

      现在你可以这样调用了:

      Program.java
      
      public class Program{
      
             // Main method is static
              public static void main(String[] args) {
                  ProgramUtil programUtil = new ProgramUtil();
                  programUtil.domainCharLimit();
              }
       
      }
      

      【讨论】:

      • 注意使everithing静态意味着您的变量将是单例,方法将在vlass级别而不是对象级别(每个应用程序只能存在一件事,它将在内存,只要应用程序正在运行)。如果您使用这种方法,您可能会在想要重用这些功能时遇到问题。除非有必要,否则将所有东西都设为静态是不好的做法
      • 从设计方法这个问题需要一个主要方法。你认为你会在你的应用程序的不同位置重用这个函数:domainCharLimit 和这些变量:firstNamelastName?如果答案是肯定的,那么我会将这个方法移到一个可以再次重用的 单独的 Util 类
      • 就是这样,我现在正尝试在其他地方重用 firstNamelastName 变量,并在这样做时获得 null。我还尝试从另一种方法打印变量 domainUsername 并且它正在打印 null。我是 Java 新手,接下来将研究单独的 Util 类——谢谢!
      • 我更新了帖子以展示最佳使用解决方案。如果你在 Java 中有一个通用的功能,你最好创建一个可以从 1 个 util 类调用的 Util 方法。每个 util 类必须始终在相同的工作范围内
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 2010-09-09
      • 1970-01-01
      相关资源
      最近更新 更多