Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: help with bookfinder programme

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default help with bookfinder programme

    hi, i'm trying to write a program that reads lines of text. if any line that contains the word "book" it will print the line, but replace the word "book" with [printed pages]s.

    at the moment my code prints the first line of text regardless of whether it contains the word "book"

    any help would be greatly apprec iated


    import java.util.Scanner;
     
    public class BookFinder {
     
      public static void main(String[] args) {
     
                     Scanner scan = new Scanner(System.in);
     
            String fname, description, suffix;
     
            boolean t = true;
            System.out.println("Enter lines of text - finish with \"end\" :");
            do {
     
                description = scan.nextLine();
                if(description.contains("end"))
                {
                    return;
                }
    description=description.replaceFirst("[Bb]ook", "printed pages");
     
    System.out.println(description);
     
        }while(t==true);
        }
    }


  2. #2
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: help with bookfinder programme

    Do you only want it to print if the String contains book? If so, this is how I would do it.
    import java.util.Scanner;
     
    public class BotMain {
    	public static void main(String[] args)
    	{
    		Scanner scan = new Scanner(System.in);
    		String fname, description, suffix;
     
    		System.out.println("Enter lines of text - finish with \"end\" :");
     
    		while(true)
    		{
    			description = scan.nextLine();
     
    			if (description.contains("exit"))
    				break;
     
    			else if (description.contains("book"))
    			    System.out.println((description = description.replaceFirst("[Bb]ook", "Printed pages")));
    		}
    	}
    }

  3. The Following User Says Thank You to Brt93yoda For This Useful Post:

    kidza12 (February 9th, 2011)

  4. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with bookfinder programme

    thank you very much that worked amazing, wondering if could pick your brain on one more topic

    how would i go about getting the programme to recognise higher case "B" as well as the lower case. when the word "book" is entered on a line.

  5. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with bookfinder programme

    got it working 100% thanks for the help!

  6. #5
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: help with bookfinder programme

    Glad I could help.

Similar Threads

  1. Help with my Range Finder programme
    By kidza12 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2011, 01:53 PM
  2. Simple Programme for Date
    By bhavinsparikh in forum Java Theory & Questions
    Replies: 1
    Last Post: August 28th, 2010, 07:24 AM
  3. access cisco router with java programme
    By vigneswara in forum Java Theory & Questions
    Replies: 1
    Last Post: May 11th, 2010, 01:36 AM