Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Print out returned value from one method, to main class

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading