There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Solved: Java problem - using delimiter


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
TNTXxX's Avatar
Junior Member with 12 posts.
 
Join Date: Jul 2008
Experience: Beginner
17-Jul-2008, 06:02 AM #1
Solved: Java problem - using delimiter
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's Avatar
Computer Specs
Distinguished Member with 6,598 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
17-Jul-2008, 06:51 AM #2
Hi TNTXxX,

Welcome to TSG !

Here's a small example :
Code:

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());
        }
    }
}
TNTXxX's Avatar
Junior Member with 12 posts.
 
Join Date: Jul 2008
Experience: Beginner
17-Jul-2008, 05:12 PM #3
hmmm.. but what if im only allowed to use useDelimiter() and no other mthod?
Chicon's Avatar
Computer Specs
Distinguished Member with 6,598 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
17-Jul-2008, 05:28 PM #4
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's Avatar
Junior Member with 12 posts.
 
Join Date: Jul 2008
Experience: Beginner
18-Jul-2008, 03:45 AM #5
thx for the help
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:10 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.