Home > Software design >  Print out returned value from one method, to main class
Print out returned value from one method, to main class

Time:10-31

How can i print out the returned value num from getTravellers() in main ONLY when there might be a soulmate?

import java.util.Scanner;

public class SoulmateFinder {

    public static void main(String[] args){

        System.out.println("Where did your journey start?");
        int jStart = kbd.nextInt();
        
        System.out.println("Where did your journey end?");
        int jEnd = kbd.nextInt();

        getTravellers(jStart, jEnd);

        System.out.println("Number of potential soulmates: "   ( ?? what to add here );
    }

    public static Scanner kbd = new Scanner(System.in);

public static int getTravellers(int startSM, int endSM){


        System.out.println("Enter the overall number of travellers on the train: ");
        int num = kbd.nextInt();

        for (int i = 1;i <= num; i   ){
        System.out.println("Enter the traveller's name:");
        String name = kbd.next();

        System.out.println("Enter the boarding station:");
        int stationBoard = kbd.nextInt();

        System.out.println("Enter the exit station:");
        int stationExit = kbd.nextInt();
        

        if (startSM <= stationExit && stationBoard <= endSM && endSM != stationBoard) {
            
            System.out.println(name   " might be the soulmate.");
        }
        else {

            System.out.println(name   " is not the soulmate.");

        }
    }
    return num;
    }
    

}

I tried making a different method to call it but i would always end up in a loop, or the getTravellers() method would print it out, when i want only the main method to print out the number of potential soulmates a.k.a num

CodePudding user response:

You are returning the num in getTravellers() so you just need to pass it to your print statement in order to include it at the end of your string.

import java.util.Scanner;

public class SoulmateFinder {

public static void main(String[] args){

    System.out.println("Where did your journey start?");
    int jStart = kbd.nextInt();
    
    System.out.println("Where did your journey end?");
    int jEnd = kbd.nextInt();

    System.out.println("Number of potential soulmates: "   getTravellers(jStart, jEnd);
}

public static Scanner kbd = new Scanner(System.in);

public static int getTravellers(int startSM, int endSM){
    int soulmates = 0

    System.out.println("Enter the overall number of travellers on the train: ");
    int num = kbd.nextInt();

    for (int i = 1;i <= num; i   ){
    System.out.println("Enter the traveller's name:");
    String name = kbd.next();

    System.out.println("Enter the boarding station:");
    int stationBoard = kbd.nextInt();

    System.out.println("Enter the exit station:");
    int stationExit = kbd.nextInt();
    

    if (startSM <= stationExit && stationBoard <= endSM && endSM != stationBoard) {
        
        System.out.println(name   " might be the soulmate.");
        soulmates  ;
    }
    else {

        System.out.println(name   " is not the soulmate.");

    }
}
return soulmates;
}

}

CodePudding user response:

The getTravellers method returns an int, so you can just put that inside the print statement.

public static void main(String[] args){
    System.out.println("Where did your journey start?");
    int jStart = kbd.nextInt();
    
    System.out.println("Where did your journey end?");
    int jEnd = kbd.nextInt();

    System.out.println("Number of potential soulmates: "   getTravellers(jStart, jEnd));
}
  • Related