Im trying to create a program in java that takes the input o
I\'m trying to create a program in java that takes the input of a user as fraction and uses the QuickSort method to sort these equations. I created a fraction class, a QuickSort class and main.
My question is:
In main, how do i take the user input with \"/\" in it (i.e. 1/2 4/5 0/2...). Is there a way to split all the string input into an array, then read only the integers? Or how am I supposed to do that?
Solution
You can use the Scanner class with delimiter as \'/\' to read the numerator part, and then set the delimiter as whitespace to read the denominator part. Here is the code:
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Pattern whitespace = s.delimiter();
int numerator = 0;
int denominator = 0;
while(scanner.hasNext()) {
s.useDelimiter(\"/\");
numerator = s.nextInt();
s.useDelimiter(whitespace);
denominator = s.nextInt();
# Consume numerator and denominator here
# For quicksort I imagine you would want to store them in an array
}
scanner.close()
}
