Home > Enterprise >  How to get the result of another method from another method and print it
How to get the result of another method from another method and print it

Time:09-29

i want to get all the result from another method and compile it as one

package labexer1a;

import java.util.Scanner;

public class LabExer1A {
    public static void main(String[] args) {
        faveNumber();
        faveCartChar();
        mI();
        fullName();
        nickName();
    }
    public static void faveNumber() {
        Scanner s = new Scanner(System.in);
        System.out.print("Type your Number: ");
        int faveNumber = s.nextInt();
    }
    public static void faveCartChar() {
        Scanner s = new Scanner(System.in);
        System.out.print("Type your favorite Cartoon or Anime Character: ");
        String faveCartChar = s.next();
    }
    public static void mI() {
        Scanner s = new Scanner(System.in);
        System.out.print("Type your Middle Initial: ");
        String mI = s.next();
    }
    public static void fullName() {
        Scanner s = new Scanner(System.in);
        System.out.print("Type your FullName: ");
        String fullName = s.next();
    }
    public static void nickName() {
        Scanner s = new Scanner(System.in);
        System.out.print("Type your Nickname: ");
        String nickName = s.next();
    }
    public static void result() {
        System.out.print(faveNumber   "is my favorite number");
        System.out.print("I love"   faveCartChar);
        System.out.print("My name is"   fullName);
        System.out.print("You can call me"   nickName);
    }
}

CodePudding user response:

You can change the fuctions return type from void to String or int for example and the return the result.

Example:

public static String fullName() {
    Scanner s = new Scanner(System.in);
    System.out.print("Type your FullName: ");
    String fullName = s.next();
    return fullName;
}

public static int faveNumber() {
    Scanner s = new Scanner(System.in);
    System.out.print("Type your Number: ");
    int faveNumber = s.nextInt();
    return faveNumber;
}

If you now want to acces the data just call the fuction and it will return it.

Example:

System.out.println(nickName()   " is my favorite number");
System.out.println("You can call me "   faveNumber());

And also I would recomend naming the functions more like getNickName so anyone can direclty see that it will return the nick name!

CodePudding user response:

package labexer1a;

import java.util.Scanner;

public class LabExer1A {
    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);
        System.out.print("Type your Number: ");
        int faveNumber = s.nextInt();
        System.out.print("Type your favorite Cartoon or Anime Character: ");
        String faveCartChar = s.next();
        System.out.print("Type your Middle Initial: ");
        String mI = s.next();
        System.out.print("Type your FullName: ");
        String fullName = s.next();
        System.out.print("Type your Nickname: ");
        String nickName = s.next();

        System.out.print(faveNumber   " is my favorite number");
        System.out.print(" I love "   faveCartChar);
        System.out.print(" My name is "   fullName);
        System.out.print(" You can call me "   nickName);
    }
}

CodePudding user response:

Here's a different approach, which separates the process out into its own method, allowing you to run it multiple times.

package labexer1a;

import java.util.Scanner;

public class LabExer1A {
    public static final Scanner s = new Scanner(System.in);

    public static void main(String[] args) {
        getInfoAndPrint();
        getInfoAndPrint();
    }

    public static void getInfoAndPrint() {
        Scanner s = new Scanner(System.in);
        System.out.print("Type your Number: ");
        int faveNumber = s.nextInt();
        System.out.print("Type your favorite Cartoon or Anime Character: ");
        String faveCartChar = s.next();
        System.out.print("Type your Middle Initial: ");
        String mI = s.next();
        System.out.print("Type your FullName: ");
        String fullName = s.next();
        System.out.print("Type your Nickname: ");
        String nickName = s.next();

        System.out.print(faveNumber   " is my favorite number");
        System.out.print(" I love "   faveCartChar);
        System.out.print(" My name is "   fullName);
        System.out.print(" You can call me "   nickName);
        System.out.println();
    }
}
  • Related