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 6 of 6

Thread: How can I search for words in a String?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can I search for words in a String?

    Here is the program:
        import easyIO.*;
     
     
        public class test {
     
        // Main method
     
        public static void main(String[] args) {
     
                In tast = new In(); // Opens keyboard for reading
     
        System.out.println("1. Register a bird");
        System.out.println("2. Write birdtype");
        System.out.println("3. Write place");
        System.out.println("4. Close program");
     
        System.out.print("Choose number (1-4:) ");
        int valg = tast.inInt();
     
        switch (valg) {
        case 1: registerBird(); break;
        case 2: writeBirdtype(); break;
        case 3: writePlace(); break;
        case 4: System.exit(0); break;
     
        default: System.out.println("Error: Choose a number from 1-4"); System.exit(0);
     
     
        } // End of switch statement
     
        } // end of main method
     
        //  registerBird() method
     
        public static void registerBird() { 
     
        In tast = new In();  // Opens keyboard for reading
        Out birds = new Out("fugler.txt", true);
     
        System.out.println("Birds name: ");
        String fuNavn = tast.inLine();
        birds.out(fuNavn);
     
        System.out.println("Sex: ");
        String fuKjonn = tast.inLine();
        birds.out(fuKjonn);
     
        System.out.println("Place of observation: ");
        String stObs = tast.inLine();
        birds.out(stObs);
     
        System.out.println("Date of observation: ");
        String fuDato = tast.inLine();
        birds.out(fuDato);
     
        birds.close();
     
        } // end of registerBird() method
     
        // writeBirdtype() method
     
        public static void writeBirdtype() {
        In innSkjerm = new In(); // Klargjør innlesing fra terminal
        In fil = new In("fugler.txt"); // Klargjør innlesing fra fil
     
     
        System.out.println("Write out all the observations of a birdtype: ");
        String ord = innSkjerm.readLine();
     
        System.out.println("All the observations you asked for:");
        String linje = fil.inLine();
     
     
        } // end of writeBirdType() method
     
     
        } // end of class test

    My problem lies in this method:

       public static void writeBirdtype() {
       In innSkjerm = new In(); // Start reading from terminal
       In fil = new In("fugler.txt"); // Starts reading fromfile
     
       System.out.println("Write out all the observations of a birdtype: ");
       String ord = innSkjerm.readLine();
     
       System.out.println("All the observations you asked for:");
       String linje = fil.inLine();
     
     
       } // end of writeBirdType() method

    The problem is when the method asks for "Write out all the observations of a birdtype:" and i type in for example "Craw" then the next line only shows "All the observations you asked for:" without all the observations of Craw.

    also the method writePlace() I haven't created that yet, just in case you're wondering

    Thanks for help


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: How can I search for words in a String?

    I see where you print out the "All the observations you asked for:" part, but I don't see anything printed out after that. What did you expect your code to do?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I search for words in a String?

    i'm expecting my code to print out all the observations of for example "Craw" in a file, if you input that, but i'm not sure what kind of code I can use to print out all the observations of "Craw". Do you know any examples or hints I need to write to print out all the observations?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: How can I search for words in a String?

    Which part is giving you trouble? Do you know how to output text to a file? Do you know how to create a List of Strings? Do you know how to use the String API to select the Strings you want? Create an SSCCE isolating the single step that you're stuck on, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I search for words in a String?

    Well i'm not sure on how I can print out all the observations of one word which user has inputed. I'm very new to java. I know how to output text to file. I dont know how to create a List of String or how to use the String API to select the Strings you want. Do you know any place I can learn it?

    --- Update ---

    actually there is nothing wrong with the code i have posted, but my problem is that i am not sure on how i can print out all the words which user has inputed. Lets say if user inputs word "Crow" then the next line should print out all the words "Crow" from the txt file.

    --- Update ---

    I added a new code at the bottom

    public static void skrivFugltype() {
      	In innSkjerm = new In(); // Klargjør innlesing fra terminal
      	In fil = new In("fugler.txt"); // Klargjør innlesing fra fil
     
      	System.out.println("Skriv ut alle observasjonene av en fugltype: ");
        String ord = innSkjerm.readLine();
     
        System.out.println("Alle observasjonene du ba om:");
        String linje = fil.readLine();
        System.out.println(linje);

    But it now prints out wrong information and only one String. Like if i input "Craw", the output shows "Bird".

    How would you print out all the info from input? For example you input "Craw" and the output shows all words related to "Craw"

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: How can I search for words in a String?

    I recommend starting much smaller, with an SSCCE that we can work from. Hardcode an array of words and print it out. When you have that working, only print out words that contain a certain hardcoded String. When you have that working, input that String from the user. When you get that working, output the words to a file instead of the command line.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 12
    Last Post: March 17th, 2013, 01:38 AM
  2. Replies: 2
    Last Post: September 29th, 2012, 07:42 PM
  3. search string in a text file
    By dewet in forum Android Development
    Replies: 2
    Last Post: April 10th, 2012, 02:39 PM
  4. Reverse every pair of words in a string
    By mulligan252 in forum Collections and Generics
    Replies: 4
    Last Post: October 11th, 2011, 04:29 PM
  5. [SOLVED] Couldn't search for a string in an array.. Help please..
    By astrojunk in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2011, 09:47 PM