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?