1

Sample input: 1/2 3/4 5/6 7/8 9/10

What I expect it to do: first split the string based on whitespace, so you end up with a string array of {1/2 , 3/4 , 5/6 , 6/8 , 9/10}, and then split that into an array of integers intA{1,2}, intB{3,4}, intC{5,6}, intD{7,8}, intE{9,10} so that I can finally pass them into a fraction object with a numerator and denominator.

Here is what I currently have:

public static void main(String[]args)
{
    Scanner keyboard = new Scanner(System.in);
    String str;
    str = keyboard.nextLine();
    String[] splitStr = str.split(" ");

    Fraction[] input = new Fraction[10];

    for(int i = 0; i < splitStr.length; i++)
    {
        String[] fract = splitStr[i].split("/");
        int num = Integer.valueOf(fract[0]);
        int den = Integer.valueOf(fract[1]);
        double fraction = (double)num / den;
    }

    for(int i = 0; i < input.length; i++)
    {
        System.out.println(input[i]);
    }
}

When I input the above string, I get null values back. If I enter any values without a forward slash, like so: 1/2 3/4 5/6 7 8 9/10 instead of reading the 7 and 8 as 7/1 and 8/1 the program runs into a NullPointerException error. There has got to be an easier way to do this, isn't there?

4 Answers4

4

I get null values back

That is because you don't add any values to the input, you only initalize it which will fill it with null values.

I'm not sure how you create Fraction objects but you need to create this kind of objects and add them to the array. Here is an example:

Fraction[] input = new Fraction[10];

for(int i = 0; i < splitStr.length; i++) {
    String[] fract = splitStr[i].split("/");
    int num = Integer.valueOf(fract[0]);
    int den = Integer.valueOf(fract[1]);
    input[i] = new Fraction(num, den);
}

If you want to set the denominator to 1 when the input contains single numbers, you can do something like this:

for(int i = 0; i < splitStr.length; i++) {
    String[] fract = splitStr[i].split("/");
    int num = Integer.valueOf(fract[0]);
    int den = 1;
    if(fract.length == 2){
        den = Integer.valueOf(fract[1]);
    }
    input[i] = new Fraction(num, den);
}

Also, it is safer to set the input array's length to be equal to the splitStr array's length.

Fraction[] input = new Fraction[splitStr.length];
Titus
  • 22,031
  • 1
  • 23
  • 33
  • Thank you! I actually managed to get the fractions into the array before seeing your answer, but the point about setting the `input` array length to be equal to `splitStr` was helpful, as was how to handle a whole number being entered. I think that is about as compact as you can get the code in Java. I was feeling lost in a sea of indexes. – Axel Finkel Oct 24 '16 at 10:26
  • Could you maybe explain what the practical difference between `Integer.parseInt()` and `valueOf()` is? – Axel Finkel Oct 24 '16 at 10:28
  • @AxelFinkel You can find details about that [HERE](http://stackoverflow.com/questions/508665/difference-between-parseint-and-valueof-in-java). Usually, `List`s are prefered instead of arrays, the size of a list can be changed and it is easier to iterate them. – Titus Oct 24 '16 at 10:34
0

For numbers without den ( 7 8 ) you need to check the length of fract:

String[] fract = splitStr[i].split("/");
int num = Integer.valueOf(fract[0]);
int den = 1
if(fract.length > 2)
        den = Integer.valueOf(fract[1]);
Jorge C
  • 478
  • 4
  • 14
0
for(int i = 0; i < splitStr.length; i++)
{  
    // if statement works for 7 or 8
    if(splitStr[i].length() == 1){
       // do something

    } else if(splitStr[i].length() == 3){
       String[] fract = splitStr[i].split("/");
       int num = Integer.valueOf(fract[0]);
       int den = Integer.valueOf(fract[1]);
       double fraction = (double)num / den;
    }
 }
Tinh Huynh
  • 76
  • 2
  • 6
0

You could use a regular expression like \s(\d+\/\d+)+ to check if the string is composed of fractions and manage the situation accordingly.