Q:

Delimiter in java

I am trying to use a delimiter in Java, but it won't work(the standard white space delimiter is working).
My code is this:
Scanner input = new Scanner(System.in);

System.out.println("Enter the first rational number seperated by '/':");
input.useDelimiter("/");
int numerator1 = input.nextInt();
int denominator1 = input.nextInt();

System.out.println(numerator1 + denominator1);

When I use the whitespace delimiter I get 12 as the output, but when I try to use "/", I get nothing.

A:

The reason is because it's still waiting for more input.  The nextInt() method stops parsing when it encounters non-numeric input, discarding the rest of the token.  This line:
int numerator1 = input.nextInt();

reads the 8 and the / off the input stream, yielding the numerator of 8,  but 4 is still in the stream.  The Scanner hasn't seen another /, so it doesn't know when the the next token will end.  It blocks.
If you enter another / then it will work.
Enter the first rational number seperated by '/':
8/4/
12

A:

You can use split and parseInt
import java.util.*;
import java.io.*;
class delimiter {
    public static void main(String[] args) {
Scanner input = new Scanner(System.in);
    System.out.println("Enter the first rational number seperated by '/':");
    String[] parts = input.nextLine().split("/");
    int numerator1 = Integer.parseInt(parts[0]);
    int denominator1 = Integer.parseInt(parts[1]);

    System.out.println(numerator1 + " " + denominator1);
}
}

Input / Output :
Enter the first rational number seperated by '/':
8/4
8 4

If you want to add the two numbers just use:
 System.out.println(numerator1 + denominator1);

which outputs 12
If you (or somebody else reading this) wanted to split with another symbol, you could use 
       String[] parts = input.nextLine().split("\\+");

if you wanted to split (8+4) in two parts separated by + or
    String[] parts = input.nextLine().split("\\*");

if you wanted to split (8+4) in two parts separated by *
We need to use \\ for + and * because these are special which the regex needs to properly take care of
You can also use any other symbol you wish to split by (just change the argument of the split() function and be careful if it is a special character or not)
Hope that helps

