0

I want to get a rational number from the user in the form 188/4 for example and end up with int 188 and int 4. what is an efficient way to do this in java? is stringTokenizer the best way?? or is there something like scanf in c??

private int NUM;
private int DEN;
static Scanner s;
public Rational(){
    s = new Scanner(System.in).useDelimiter("/");
    System.out.println("Enter Rational number in the form of a/b:");
    NUM = s.nextInt();
    int d = s.nextInt();
    if(d == 0){
        throw new IllegalArgumentException();
    }
    else{
        DEN = d;
    }
}

when I run this my program freezes after I input a number

1
  • 2
    Your best option here is probably to read it in as a string, split the string based on "/" so that you end up with two strings. at that point, parse to see if both are integers, and you're all set Commented Feb 13, 2015 at 0:49

3 Answers 3

1

Small tweaks...

private int NUM;
private int DEN;
static Scanner s;
public Rational(){
    System.out.println("Enter Rational number in the form of a/b:");
    s = new Scanner(System.in);
    String[] values = s.next().split("/");

    NUM = Integer.parseInt(values[0]);
    int d = Integer.parseInt(values[1]);
    if(d == 0){
        throw new IllegalArgumentException();
    } else{
        DEN = d;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try something like this (assuming each fraction can be input on a separate line):

        s = new Scanner(System.in).useDelimiter("\n");
        System.out.println("Enter Rational number in the form of a/b:");
        String in = s.nextLine();
        while (in.length() > 0) {
            if (in.contains("/")) {
                try {
                     NUM = Integer.parseInt(in.split("/")[0]);
                     DEN = Integer.parseInt(in.split("/")[1]);
                } catch (Exception e){
                    throw new IllegalArgumentException("Fraction must contain numeric values only");
                }
            } else {
                throw new IllegalArgumentException("Fraction must contain a '/' character");
            }
            System.out.println("NUM = " + NUM + " DEN = " + DEN);
            in = s.nextLine();
        }

Comments

0

The only way i could think about is to treat is as a string, remove the unnecessary "/" and then turn it back into a int with Integer.parseInt();

  int NUM;
        int DEN;
        Scanner s;
            s = new Scanner(System.in).useDelimiter("/");
            System.out.println("Enter Rational number in the form of a/b:");
            String enteredString = s.nextLine();

            String firstString = "", secondString = "";
            StringBuilder sb = new StringBuilder(enteredString);

            boolean foundWeirdChar = false;
            boolean firstChar = true;
            char tempChar = '/';

            for(int i = 0; i < sb.length(); i++) {

                if(sb.charAt(i) == tempChar) {
                    foundWeirdChar = true;
                }

                if(!foundWeirdChar) {
                    firstString += sb.charAt(i);
                }else {
                    if(firstChar) { // Because the first char will be "/"
                        firstChar = false;
                    }else{
                     secondString += sb.charAt(i);
                    }
                }
            }
            int a = 0;
            int b = 0;
            try {
                a = Integer.parseInt(firstString);
                b = Integer.parseInt(secondString);

                System.out.println("First int: " + a + "\nSecond int: " + b);

            }catch(NumberFormatException ex) {
                ex.printStackTrace();
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.