Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a few confusions about writing Java code, and I need some clarifications:


Java
import java.util.Scanner;

public class NameAbbreviation {

private static String  fullName;
private static Scanner nameInput;

public NameAbbreviation(String name) {
    NameAbbreviation.fullName = name;
}

public char getInitialLetterOfFirstName() {
    char initialletterOfFirstName = fullName.charAt(0);
    //System.out.println(initialletterOfFirstName);
    return initialletterOfFirstName;
}

public String getInitialLetterOfLastName() {
    int space = fullName.lastIndexOf(" ");
    String initialletterOfLastName = fullName.substring(space + 1,
            space + 2);
    //System.out.println(initialletterOfLastName);
    return initialletterOfLastName;
}

public static void main(String[] args) {

    nameInput = new Scanner(System.in);
    String name = nameInput.nextLine();
    NameAbbreviation na = new NameAbbreviation(name);
    System.out.println(na.getInitialLetterOfFirstName() + " "
            + na.getInitialLetterOfLastName());

     }
}


I have class variables fullName and nameInput. If I make nameInput local, it shows: "Resource leak: nameInput is never closed." So I just follow the Eclipse suggestion and make it a field. What should I need to follow?

I have 2 methods -
Java
getInitialLetterOfFirstName()
and
Java
getInitialLetterOfLastName()
, the return types of which are "char" and "String" respectively. I could eventually make the return type as void. What are the advantages or disadvantages if I have void or specific return type? As both the way of code works, what do I need to follow or does it depend on how I am writing the code?
Both the methods
Java
getInitialLetterOfFirstName()
and
Java
getInitialLetterOfLastName()
don't have any parameters/arguments. I could eventually create the method with argument like:

Java
getInitialLetterOfFirstName(String name) {
        this.fullName = name;    .......}


Java
getInitialLetterOfLastName(String name) {
        this.fullName = name;    .......}


If I create the methods with arguments, I need to pass the value during the instantiation of the object. So I avoid it here, and prefer to have only once to pass the argument value.

Is there any rule to follow, like to make the methods with parameters or not, as here both are possible with or without argument methods?

What do I need to follow as good practice, code performance issues, etc.?
Posted
Comments
Mohibur Rashid 20-Jul-15 21:29pm    
First of all, you have defined two static object, so that you can access them from your another static function called main. Think about it. If you declare an instance of class NameAbbreviation and then try to call getInitialLetterOfLastName or the other one before setting up.

It would be better if
i) you move your Scanner nameInput in the main function.
ii) you make fullname private
iii) you create another function to set fullname, say set_fullName
iv) declare a variable in main function get the full name and set the full name through set_fullName function;
v) you can change your getInitialLetterOfLastName function as below:
public char getInitialLetterOfLastName() {
int space = fullName.lastIndexOf(" ");
char initialletterOfLastName = fullName.charat(space + 1);
//System.out.println(initialletterOfLastName);
return initialletterOfLastName;
}

and so on.

1 solution

Just one solution
Java
import java.util.Scanner;

public class NameAbbreviation {

	private String fullName;

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public char getInitialLetterOfFirstName() {
		return fullName.charAt(0);
	}

	public String getInitialLetterOfLastName() {
		return fullName.substring(0, 1);
	}

	public void writeInitialLetterOfLastName(){
		System.out.println(this.fullName.charAt(0));
	}
	
	public static void main(String[] args) {

		Scanner nameInput = new Scanner(System.in);
		String name = nameInput.nextLine();
		nameInput.close();

		if (name.length() != 0) {
			NameAbbreviation na = new NameAbbreviation();
			na.setFullName(name);
			System.out.println(na.getInitialLetterOfFirstName() +
					" " + na.getInitialLetterOfLastName());
			na.writeInitialLetterOfLastName();
		}
	}
}


 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900