0

I have to make a program that interacts with the user interface that ask a user for a number and a base to convert to binary, hex, or octo. I made a program that works but when the user types in "0 0", it is suppose to terminate and end the program. My program on the other hand doesn't do that but keep going in the while loop.

Here is the code:

import java.util.*;  //for Scanner

public class BaseConversionApp {
public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    String combo = numbers(console);

    while(combo != "0 0") {
        if(combo.length() > 0) {     //breaks the string into integers to do math to it
            Scanner s = new Scanner(combo);
            int count = s.nextInt();
            int countSecond = s.nextInt();
            s.close();
            conversion(count,countSecond);
            System.out.println();
            //now if it goes 0 0 or

            String again = numbers(console);
            //  conversion(count,countSecond);
        }
    }


//prompts the user for two numbers and checks if the bases are 16, 8, 2.
public static String numbers(Scanner console) {
        String combination = "";
        String nothing = "0 0";
        System.out.print("Enter an integer and the new base: ");
        int integer = console.nextInt();
        int base = console.nextInt();
        if(base == 16 || base == 2 || base == 8) {
            combination = integer + " " + base;
            return combination;
        } else if (base == 0 && integer == 0){
            System.out.println("Good bye!");
            return nothing;
        } else {
            System.out.println("Sorry, that is an invalid base. Please enter 2, 8, or 16 
only.");
        }
        return "";
    }

    //converts the integers into binary, hexa, or octo.
    public static void conversion (int integer, int base) {

        //takes cares of the special case if the user wants to know hexidecimal
        if(base <= 16) {
            String calculations = Integer.toString(integer, base);


            if(integer > 0 && base > 0) {
                System.out.println(integer + " in binary -> " + Integer.toString(integer, 
base));
            }
        }
    }
}
Rosanna Lui
  • 31
  • 1
  • 4
  • You need to re-think your code. The logic is faulty. What you want to do is to ask the user if he/she wants to insert numbers for conversion. If the user wants to - use the function numbers and numbers and conversion. If not (input is "0 0") - close the scanner and exit. – simeg Jul 22 '14 at 19:04

2 Answers2

1

You can't compare strings like that, you have to use the String object's equals() method. So, you should have:

while(!"0 0".equals(combo)) {
    ...
}

Notice that I've put the constant "0 0" first -- that protects you against combo being null. Otherwise you'd have !combo.equals("0 0"), and if combo were null you'd get a NullPointerException when you try to call equals() on a null value.

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
  • 1
    I changed that but the issue I'm having is the while loop won't terminate. I think it has to do with calling combo twice but under different variable names. I can't think of a way to fix that. – Rosanna Lui Jul 22 '14 at 18:33
0

Try this code instead. Yours looks complicated. Btw, your code works for me.

import java.util.Scanner;

public class NewClass {

    static Scanner inp = new Scanner(System.in);
    static String line1 = "";
    static String line2 = "";
    static String exit = "exit";

    public static void main(String[] args) {
        System.out.println("Enter int first and then base...");
        System.out
                .println("Enter the word exit to exit!");

        while (true) {

            line1 = inp.next();
            if (line1.equalsIgnoreCase(exit)) {
                break;
            }
            line2 = inp.next();

            try {
                conversion(Integer.parseInt(line1), Integer.parseInt(line2));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }

        }

        System.out.println("Bye...");

    }

    public static void conversion(int integer, int base) {

        // takes cares of the special case if the user wants to know hexadecimal
        if (base <= 16) {
            String calculations = Integer.toString(integer, base);

            if (integer > 0 && base > 0) {
                System.out.println(integer + " in binary -> "
                        + Integer.toString(integer, base));
            }
        }
    }

}
james
  • 1,667
  • 5
  • 26
  • 38