0

I am writing a "Gallons to Liters and vice versa" program in Java, but once I finished my program, i gave it the first run. I displayed the system outputs, like expected, but then it skipped the user input stage of my program.

import java.util.Scanner;

public class Main {
    private static void intro(String[] args) {
        final Scanner galOrLit = new Scanner(System.in);

        System.out.println("[1] => Gallons to Liters");
        System.out.println("[2] => Liters to Gallons");

        if ("1".equals(galOrLit)) {
            galToLit(args);
        } else if ("2".equals(galOrLit)) {
            litToGal(args);
        }
    }

    private static void galToLit(String[] args) {
        double liters;

        System.out.println("Enter Gallons: ");
        final Scanner gallons = new Scanner(System.in);

        int input = gallons.nextInt();
        liters = input * 3.7854;

        System.out.println("\n" + input + " gallons is " + liters + " liters.");
    }

    private static void litToGal(String[] args) {
        double gallons;

        System.out.println("Enter Liters: ");
        final Scanner liters = new Scanner(System.in);

        int input = liters.nextInt();
        gallons = input / 3.7854;

        System.out.println("\n" + input + " liters is" + gallons + " gallons.");
    }

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

    }
}

I tried to final my Scanners, since I had multiple in one class, following this post. I was expecting my code to work smoothly, but it did no difference to the output. this is as far as I get with both versions of code.

Diego Borba
  • 1,282
  • 8
  • 22
arivvid27
  • 28
  • 4
  • 3
    `"1".equals(galOrLit)`, you are comparing the `Scanner` object itself against the string `"1"`. That will never be `true`. Instead you have to read something from the scanner like `nextLine()` or `nextInt()` and compare **that** against the string `"1"` or `"2"` (or against actual `int` numbers). – Progman Aug 09 '23 at 20:55
  • 1
    The post you linked uses `hasNext()` and `nextInt()`. Perhaps you want to do the same for the choice? – OneCricketeer Aug 09 '23 at 20:57
  • Also note that British gallons are 4.55 litres. Maybe choose inches and cm instead, which don't have regional variations. – k314159 Aug 09 '23 at 20:57

2 Answers2

0

It looks like you are not actually reading any input from the user. You are only printing out the options to choose from, but you are not reading the user's input. You only to add one line:

String choice = scanner.nextLine();

Here is how it would look like:

private static void intro(String[] args) {
    final Scanner scanner = new Scanner(System.in);

    System.out.println("[1] => Gallons to Liters");
    System.out.println("[2] => Liters to Gallons");

    String choice = scanner.nextLine();

    if ("1".equals(choice)) {
        galToLit(args);
    } else if ("2".equals(choice)) {
        litToGal(args);
    }
}

Another thing, I suggest to not use nextLine(), but use: Integer.parseInt(scanner.nextLine()); for integer

So that line would look like this:

String choice = Integer.parseInt(scanner.nextLine());

Double.parseDouble(scanner.nextLine()); if you have any double Because if you input from string to int sometimes it doesn't work.

Hope this helps!

Skerdi Velo
  • 121
  • 2
  • 13
0

The problem lies in your usage of the Scanner class to read user input. Here's the corrected version of your program:

import java.util.Scanner;

public class Main {
    private static void intro(String[] args) {
        final Scanner galOrLit = new Scanner(System.in);

        System.out.println("[1] => Gallons to Liters");
        System.out.println("[2] => Liters to Gallons");

        int choice = galOrLit.nextInt();  // Read user's choice

        if (choice == 1) {
            galToLit(args);
        } else if (choice == 2) {
            litToGal(args);
        }
    }

    private static void galToLit(String[] args) {
        double liters;

        System.out.println("Enter Gallons: ");
        final Scanner gallons = new Scanner(System.in);

        double input = gallons.nextDouble();  // Read a double value
        liters = input * 3.7854;

        System.out.println("\n" + input + " gallons is " + liters + " liters.");
    }

    private static void litToGal(String[] args) {
        double gallons;

        System.out.println("Enter Liters: ");
        final Scanner liters = new Scanner(System.in);

        double input = liters.nextDouble();  // Read a double value
        gallons = input / 3.7854;

        System.out.println("\n" + input + " liters is " + gallons + " gallons.");
    }

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

In this corrected version, I've made the following changes:

  1. Use nextInt() to read the user's choice of conversion.
  2. Use nextDouble() to read the user's input for gallons or liters.
  3. Updated the conditions in the intro method to compare the user's choice correctly.
  4. Changed the input variable type to double in the galToLit and litToGal methods to accommodate double values.

This way it now correctly read user input and perform the desired conversions.

Diego Borba
  • 1,282
  • 8
  • 22