3

This point of this program is to limit the character length of a username to 20 characters. It is one part of a larger program which currently only contains a Main method. In the interest of cleaning and clarifying my code, I would like to separate the various functions into distinct methods.

Currently, I'm trying to set class variables so that they can be used in multiple methods. This is what I have so far:


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);
    }    
}

I have tried setting one or both methods to static, which did not resolve the issue. In this state, when run, the program will not return an errors but rather give "no output"

2 Answers 2

5

Main method has to be static! It is entry to your program and its signature has to be like that.

In order to call non static method inside it you need to instantiate an object and call it on that object. In your case something like

public static void main(String[] args) {
   Program p =  new Program();
   p.domainCharLimit();
}
Sign up to request clarification or add additional context in comments.

Comments

1

First: Main Method should be always static.

Second: Because you are calling domainChatLimit() from Main(static) than it should be also static

Third: Because you used firstName, lastName attributes in a static method domainChatLimit() then they should be also static

Fourth: Scanner should be also static because you are using it to get firstName, lastName and they are both static.

NOTE: There is no need to define a new instance of this class to call an internal method

Solution should be like below (tested successfully):

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);
        }   
}

if you want to create a Generic Util for that functionality you can do below logic:

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);
    }  
}

now you can call this way :

Program.java

public class Program{

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

4 Comments

Note making everithing static means that your varables will be singletons and methods will be on vlass level instead of object level (only one thing can exist per application and it will be in memory as long as application is running). If you use this approach you will probably encounter problems when wanting to reuse these functionalities. Making everithing static is bad practice unleass it is necessary
from design approach this question requiring a main method. do you think you will reuse this function: domainCharLimit and these variables: firstName , lastName in a different place of your application? if the answer is yes than I would move this method to a separate Util class that can be reused again
That is the case, I am now trying to reuse the firstName and lastName variables elsewhere and getting null when I do so. I'm also attempting to print the variable domainUsername from another method and it is printing null. I'm new to Java and will look into separate Util classes next -- Thank you!
I updated the post to show the best solution to use. if you have a common functionalities in Java you better create a Util methods that can be called from 1 util class. each util class has to be always within same scope of work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.