Page 1 of 2 12 LastLast
Results 1 to 20 of 22
Like Tree1Likes

Thread: Reading particular portion of text

  1. #1
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Reading particular portion of text

    Java Code:
    package DO;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class Prad1 {
    
    	public static void main(String[] args) throws FileNotFoundException {
    	
    		File file = new File ( "just");
    
    		Scanner sn = new Scanner (file);
    		
    		while ( sn.hasNext () ) {
    		
    		String om = sn.nextLine ();
    		System.out.println (om );
    			
    		}
    		sn.close ();
    		
    
    
    	}
    
    }
    Text file contains:
    On tree grows fruit: apples 11
    In the shop lays fruit: bananas 6
    Outside grows fruit: berries 5


    The result I want to be printed to console:

    apples 11
    bananas 6
    berries 5

    How to do this?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Reading particular portion of text

    How to do this?
    Can you identify where the desired String is located in the input String? Is there a character or substring that delimits the desired substring?
    The String class has methods to find substrings and extract substrings from a String.
    Last edited by Norm; 12-01-2016 at 02:06 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: Reading particular portion of text

    Quote Originally Posted by Norm View Post
    Can you identify where the desired String is located in the input String?
    I cant. it is full code too.

    Is the a character or substring that delimits the desired substring?
    The String class has methods to find substrings and extract substrings from a String.
    Something confusing you wrote here.
    Attached Thumbnails Attached Thumbnails Reading particular portion of text-2nd.jpg  

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Reading particular portion of text

    Find a pattern that is common in each sentence.

    Have the Scanner read the file and parse each line that it finds. Look for that common pattern. I am looking are something that rhymes with "bolon".... Find the index of that character, then get the substring from the String starting at that character. All methods you need are in the String class.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Reading particular portion of text

    And please...no more big ugly images in your posts. They aren't helping.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Reading particular portion of text

    If there isn't a common delimiter that can be used to find the desired text, then it will be harder to extract the part you want.
    Another possibility is to see if all of the desired part of the String is in the same relative position in the input String.
    Count the tokens (words and numbers) in each input String. Does the desired part always start after the same number of tokens?
    Or are the desired parts the same number of the last tokens?


    Please don't post images. The text in the image can not be copied to be included in a response.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Reading particular portion of text

    Surfman provide all the information you need to solve the problem as stated. The only other help that could be
    given is providing the code.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: Reading particular portion of text

    Quote Originally Posted by SurfMan View Post
    Find a pattern that is common in each sentence.

    Have the Scanner read the file and parse each line that it finds. Look for that common pattern. I am looking are something that rhymes with "bolon".... Find the index of that character, then get the substring from the String starting at that character. All methods you need are in the String class.
    Show me how to ?

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Reading particular portion of text

    Quote Originally Posted by asdfg View Post
    Show me how to ?
    ... and let poor SurfMan do all the thinking while you coud've read the API docs for the String class and see which method(s) could've come in handy ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  10. #10
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: Reading particular portion of text

    Quote Originally Posted by JosAH View Post
    ... and let poor SurfMan do all the thinking while you coud've read the API docs for the String class and see which method(s) could've come in handy ...

    kind regards,

    Jos
    I dont understand java api docs. No exmaple, no info how to use. Nothing.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Reading particular portion of text

    Go to the API doc for one of the String class's methods. Copy it here with your questions about it and someone will try to help you understand it.

    You absolutely need to be able to read and understand the API docs to be able to write code for java programs.

    The API docs explain what methods are available, what args they take and what they do. They are not a tutorial on how to use them.
    See the tutorial for java coding examples: The Really Big Index

    You need a good text book for beginning java programming.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: Reading particular portion of text

    I have typed in google java split method.
    https://docs.oracle.com/javase/7/doc...ng/String.html

    These substring is unfamilia for me

    Java Code:
     String c = "abc".substring(2,3);
      String d = cde.substring(1, 2);
    I even dont know how to adapt it for my code.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Reading particular portion of text

    After calling the substring method, print out the value that is returned. Then look at the starting String's contents and the arguments to the substring method for an understanding of what the method does.
    Take this statement and do some experimenting. Change the String and the args to the substring method and see what is printed:
    Java Code:
    System.out.println("ss="+"012".substring(2,3)+"<");

    What does the API doc say for the substring method? Can you paste it here with your questions about how it works?
    Last edited by Norm; 12-02-2016 at 01:48 AM.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Reading particular portion of text

    Why are you using substring?
    There is a split method that everyone has been pointing you towards.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Reading particular portion of text

    (this is a dead giveaway) @OP: you are interested in everything to the right of the rightmost colon in the String; read the API docs for the lastIndexOf( ... ) method in the String class; to obtain the substring of a String starting at a certain position, read the API docs for the substring( ... ) method in the API docs for the String class.

    I'm afraid programming is not for you; you lack at least the curiosity for the 'unknown'; you want to scavenge the code and expect it to work without understanding what you're doing ...

    kind regards,

    Jos
    Norm likes this.
    Build a wall around Donald Trump; I'll pay for it.

  16. #16
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: Reading particular portion of text

    https://docs.oracle.com/javase/7/docs/api/
    Cant find example of regex.

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Reading particular portion of text

    Quote Originally Posted by asdfg View Post
    https://docs.oracle.com/javase/7/docs/api/
    Cant find example of regex.
    e.g. in the description of the String.split( ... ) method, a reference to regular expressions is given; the target fully describes what regular expressions are all about. What more do you want/need?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  18. #18
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Reading particular portion of text

    If you can't find an example of regex to satisfy you then use Jos' suggestion where no
    regex is required. There are many ways to solve this problem. And this particular problem
    is extremely trivial.

    EDIT: So what is it about the following (from the split method) you don't understand?

    The string "boo:and:foo", for example, yields the following results with these expressions:

    Regex Result
    : { "boo", "and", "foo" }
    o { "b", "", ":and:f" }
    Regards,
    Jim
    Last edited by jim829; 12-02-2016 at 03:39 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  19. #19
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: Reading particular portion of text

    I saw this table somewhere too. The problem is, I dont understand regex syntac at all.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Reading particular portion of text

    I dont understand regex syntac at all.
    Yes. regular expressions are not easy and must be studied.

    See the tutorial: Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 8
    Last Post: 07-17-2011, 03:38 PM
  2. Reading Text
    By David M. in forum New To Java
    Replies: 9
    Last Post: 07-16-2011, 07:34 PM
  3. Printing portion of an image
    By thayalan in forum AWT / Swing
    Replies: 0
    Last Post: 07-06-2009, 03:04 AM
  4. remove a portion from a file
    By alon2580 in forum New To Java
    Replies: 13
    Last Post: 08-25-2008, 02:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •