Search code examples
javafractions

How to split a fraction into two Integers?


I'm working on a program where I need to create an object list from an external file containing fractions. I need to separate the numerator and denominator into two separate integers, without having the "/" be involved.

This is what I have so far:

while (fractionFile.hasNextLine()){

    num.add(fractionFile.nextInt());
    den.add(fractionFile.nextInt());

    }

I can't figure out how to have num.add read up until the "/" and have den.add read right after the "/"

Any help would be much appreciated.


Solution

  • String fraction="1/2";
    String []part=fraction.split("/");  
    num.add(part[0])
    den.add(part[1])