0

Im just starting out with java and so I am writing some simple programs to learn the basics. I was wondering what return type to use for my takeNames method? I know it has to return a string so I use "" so that the user cant see anything.

This is one of my first programs so I think I must be doing something wrong, or maybe not wrong but my code could definitely be more efficient.

import java.util.Scanner;

public class Name{

  String fName;
  String lName;
  String line = takeNames(fName, lName);

  public String takeNames(String fName, String lName){

    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter your first and last name");
    fName = scanner.next();
    lName = scanner.next();
    System.out.printf("Hello %s %s", fName, lName);
    return "";
  }


  public static void main(String [] args){
    Name l = new Name();
    System.out.println(l.line);


  }
}

Can anyone tell me if what I am using for a return statement is acceptable? Or could someone tell me a better more efficent way to write my code?
Many thanks!

3
  • Why do you want to return a string? Commented Sep 26, 2013 at 2:08
  • 5
    The answer is, what do you want the method to return? Commented Sep 26, 2013 at 2:08
  • I didnt see anyone else point this out, but it is worth mentioning that return ""; is a valid statement in general, its just not what you should use here. Commented Sep 26, 2013 at 2:13

6 Answers 6

1

You don't need to return anything.

public void takeNames(String fName, String lName){

Call it like this (replace your main method with this):

Name l = new Name();
l.takeNames();

And remove the line:

String line = takeNames(fName, lName);

which is then useless.

If you want to use System.out.println(l.line), then try:

Scanner scanner = new Scanner(System.in);
System.out.println("Enter your first and last name");
fName = scanner.next();
lName = scanner.next();
return "Hello " + fName + " " + lName;
// can also be String.format("Hello %s %s", fName, lName);
Sign up to request clarification or add additional context in comments.

Comments

1

You later print out the return value of this method with System.out.println(l.line), so maybe you want to return the string you print out in this method, instead of printing it out.

public String takeNames(String fName, String lName){

    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter your first and last name");
    fName = scanner.next();
    lName = scanner.next();
    return String.format("Hello %s %s", fName, lName);
  }

Comments

1

Make the return type void if you don't want to return anything, like so -

public void takeNames(String fName, String lName){

Scanner scanner = new Scanner(System.in);
System.out.println("Enter your first and last name");
fName = scanner.next();
lName = scanner.next();
System.out.printf("Hello %s %s", fName, lName);//note no return statement needed!


}

Comments

1
package com.linkage.test;
import java.util.Scanner;

public class Name{


  public static String takeNames(){

    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter your first and last name");
    String fName;
    String lName;
    fName = scanner.next();
    lName = scanner.next();

    System.out.printf("Hello %s %s", fName, lName);
    return fName+"_"+lName;
  }


  public static void main(String [] args){
      String[] arrays = takeNames().split("_");
      System.out.println();
      for (int i = 0; i < arrays.length; i++) {
          System.out.println(arrays[i]);
      }
  }
}

Comments

1

The purpose of a return statement is to return something that will be useful later. Unless printing out a blank line, like System.out.println(l.line); will do, is useful to you, there's no point in doing that particular return statement.

The bigger issue is your code structure. Change it to this:

String fName;
String lName;
//Get rid of line

public void takeNames(){ //You don't need fName or lName in this line, since they're part of the object's fields
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter your first and last name");
    fName = scanner.next();
    lName = scanner.next();
    System.out.printf("Hello %s %s", fName, lName);
}


public static void main(String [] args){
    Name l = new Name();
    //What should you call here to get the same effect as before? Will let you figure that out
}

Comments

1

Better way to do this would be

import java.util.Scanner;

public class Name{

  String fName;
  String lName;
  String fullName;

  public String takeNames(){ // don't need parameters else it will shadow above variables   

    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter your first and last name");
    fName = scanner.next();
    lName = scanner.next();

    return fName + lName; // return full name 
  }


  public static void main(String [] args){
    Name l = new Name();
    fullName= l.takeNames(); // assign fullName returned from takeNames()
    System.out.println(fullName); // print


  }
}

Comments

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.