Hello Everyone, I'm having a bit of trouble seperating my user interface code in a main class from my logic code in another class.

Basically i've put a while statement and some if statements in order to take input of a number from the user and decide which of two statements to call.

while ((result < 1) || (result > 2)) {
            System.out.println("Enter 1 to convert from Pounds to Euros and 2 to convert from Euros to Pounds");
            result = scan.nextInt();

        }
        if (result == 1) {

            output123 = converter.PoundEuro(input, exchange);
            System.out.println(output123);

        } else if (result == 2) {

            output123 = converter.EuroPound(input, exchange);
            System.out.println(output123);

        }

If i'm correct ideally parts of this should be in my other class and then it should be called. I'm a total Java novice so can't say for sure. But if i'm right does anyone have any suggestions on how to do this?

Thanks for any advice.

Ranek

Recommended Answers

All 3 Replies

Put everything in a separate method. Have result, input and exchange as arguments and return the output123.

If separate, then separate:

import java.util.Scanner;

/**
 *
 * @author j3c
 */
public class Ranek {

    static Scanner scan = new Scanner(System.in);

    static int ConsoleOptionGUI(String s) {
        int res = -1;
        do {
            System.out.println(s);
            res = scan.nextInt();
        } while (res < 0 || res > 2);
        System.out.println("--> " + res);
        return res;
    }

    public static void main(String[] args) {
        int result = ConsoleOptionGUI("Enter 1 to convert from Pounds to Euros and 2 to convert from Euros to Pounds");
        if (result == 1) {
            // output123 = converter.PoundEuro(input, exchange);
            // System.out.println(output123);
        } else if (result == 2) {
            // output123 = converter.EuroPound(input, exchange);
            // System.out.println(output123);
        }
    }
}

Thanks for the advice. Its turns out that my code was already perfectly fine and I didn't need to move what I had. Just confusion on my part I feel.

Cheers all the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.