0

The following program asks user to enter an option and then runs one of two methods. I'm using Eclipse as my IDE. Currently, when selecting any of the methods the program just hangs and doesn't do anything. The console shows my input but does not progress beyond that. I'm not getting any errors but it hangs. What could be the issue? Thank you.

UPDATE: I inserted a println emmidiately after the user types in an option.

System.out.println("You entered option number " + option);

The program then does not seem to execute the if statements. I also added another println after the sc.close().

System.out.println("done");

It prints out what the user entered but then after user input it just prints out "done" instead of executing code in the if blocks.

import java.util.Scanner;


public class SimpleGame {
    
    public String convertTime(int seconds){
        // get the hours part of the seconds
        int hrs = seconds / 3600;
        // get the minutes part of the seconds
        int min = (seconds % hrs) / 60;
        // seconds 
        int sec = min % 60;
        String time = hrs + ":" + min + ":" + sec;
        return time;
    }
    
    public int digitsSum(int input){
        int sum = 0;
        String str = Integer.toString(input);
        for (int i = 0; i <= str.length(); i++) {
            int chr = Integer.valueOf(str.charAt(i));
            sum += chr;
        }
        return sum;
    }

    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        SimpleGame sg = new SimpleGame();

        // Ask the user to select an option.
        // Then ask the user for input and pass the value to the corresponding method.
        System.out.println("Select an option to play");
        System.out.println("1. convertTime");
        System.out.println("2. digitSum");

        String option = sc.next();
        // If the user enters 1, ask for an integer to convert and call the convertTime method.
        if (option == "1") {
            System.out.println("Enter an integer to convert from seconds to time");
            String s = sc.next();
            int input = Integer.parseInt(s);
            System.out.println(sg.convertTime(input));

        }
        // If the user enters 2, ask for an integer and call the digitsSum method.
        if (option == "2") {
            System.out.println("Enter an integer to sum up");
            String s = sc.next();
            int input = Integer.parseInt(s);
            System.out.println(sg.digitsSum(input));
        }
        
        sc.close();

    }

}
larry8989
  • 339
  • 1
  • 11
  • add `System.out.println("done");` after `sc.close();` – Iłya Bursov May 26 '22 at 18:09
  • 3
    after realization that application is not hung, check the https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Iłya Bursov May 26 '22 at 18:10
  • I see what was the issue. Thank you Iłya Bursov. That was the issue. I was not comparing strings correctly. – larry8989 May 26 '22 at 18:50
  • 1
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – John3136 May 26 '22 at 20:21

0 Answers0