# Solved: Java problem - using delimiter



## TNTXxX (Jul 16, 2008)

Hi all!
my question is i dont know how to code this problem.

write a program that breaks down an ISBN entered by the user as follows:
Please enter the ISBN number in the following format xxx-xxx-
xxxxxx-x: 978-0-12-345678-9
Prefix = 978
Language = 0
Publisher = 12
Book = 345678
Check = 9

You will need to read in the input as a String and then use the method
available in the Scanner class called useDelimiter("-") to break down the
ISBN

His what i try to code

import java.util.Scanner;
public class Exercise4
{
public static void main(String[] args)
{

int isbn,prefix,lang,publisher,book,check;

Scanner input = new Scanner(System.in);	
input.useDelimiter("\\s*-");
System.out.println("Please enter ISBN number in the following format xxx-xxx- xxxxxx-x: ");
isbn = input.nextInt();
System.out.print("Prefix = " ); 
System.out.println(input.nextInt());
System.out.print("Language = " );
System.out.println(input.nextInt());
System.out.print("Publisher = ");
System.out.println( input.nextInt());
System.out.print("Book = ");
System.out.println(input.nextInt()); 
System.out.print("Check = ");
System.out.println(input.nextInt());

}
}

your help is greatly appreciated =)

[using Windows XP]


----------



## Chicon (Jul 29, 2004)

Hi TNTXxX,

Welcome to TSG ! 

Here's a small example :

```
[SIZE=2]
import java.util.Scanner;
import java.util.regex.Pattern;

public class Isbn {
    public static void main(String[] args) {
        String isbn = "978-0-12-345678-9";
        Pattern p = Pattern.compile("-");
        Scanner s = new Scanner(isbn);
        s.useDelimiter(p);
        while (s.hasNextInt()) {
            System.out.println(s.nextInt());
        }
    }
}[/SIZE]
```


----------



## TNTXxX (Jul 16, 2008)

hmmm.. but what if im only allowed to use useDelimiter() and no other mthod?


----------



## Chicon (Jul 29, 2004)

You may remove the _Pattern_ statement of my example and replace the _s.useDelimiter(p);_ by _s.useDelimiter("-");_. It will work the same way.

The _useDelimiter(String pattern)_ method does exactly what I've coded in my example.


----------



## TNTXxX (Jul 16, 2008)

thx for the help


----------

