Results 1 to 13 of 13

Thread: java while loop

  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default java while loop

    I am trying to print out labels for text. it will read a file, which hold tasks on each line. what this code does, is it prints out one line (or task). shouldn't it be printing out every line (or task)?

    Java Code:
    while ((str = in.readLine()) != null) {
    			for (int i = 0; i < str.length(); i++) {
    				char c = str.charAt(i);
    				text = text + c;
    				
    				if(text.length() == str.length()) {
    					Matcher m = Pattern.compile("(.+)(.+)(\\d)").matcher(text);
    					if(m.find()){
    						System.out.println("Task Title: " + m.group(1) + "\nTask Content: " + m.group(2) + "\nTask Priority: " + m.group(3));
    					}
    				}
    			}
    		}
    tasks file:

    Java Code:
    345
    tcp
    tcp
    tcp
    tcp
    output from program:

    Java Code:
    Task Title: 3
    Task Content: 4
    Task Priority: 5
    thanks.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: java while loop

    you should try to understand what the code does which i have given you!
    \\d = A digit: [0-9]
    So p isnt a digit so it will not match! I thought the last one should be only a digit(priority) so i used \\d! change the \\d to . or something else....is it always one letter/digit? then you can also add {1}....
    See: Pattern (Java Platform SE 6)

  3. #3
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: java while loop

    so something like this?: Matcher m = Pattern.compile("(.+)(.+)(.)").matcher(text);

    what does the .+ mean? and what do you mean by {1}?

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: java while loop

    Pattern (Java Platform SE 6)
    -------------
    . Any character (may or may not match line terminators)
    X+ X, one or more times
    X{n} X, exactly n times
    ---------------

  5. #5
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: java while loop

    alright. but what about the while loop? why does it only run through once?

  6. #6
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: java while loop

    its not clear to me why you need the for loop? you don`t understand the code right? :-)

    Java Code:
    while ((str = in.readLine()) != null) {
                        Matcher m = Pattern.compile("(.+)(.+)(.+)").matcher(str);
                        if(m.find()){
                            System.out.println("Task Title: " + m.group(1) + "\nTask Content: " + m.group(2) + "\nTask Priority: " + m.group(3));
                        }
     }

  7. #7
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: java while loop

    well when i take out the for loop, i don't get anything in return... here is my code:

    Java Code:
    while ((str = in.readLine()) != null) {
    				
    				if(text.length() == str.length()) {
    					Matcher m = Pattern.compile("(.+)(.+)(.+)").matcher(text);
    					if(m.find()){
    						System.out.println("Task Title: " + m.group(1) + "\nTask Content: " + m.group(2) + "\nTask Priority: " + m.group(3));
    				}
    			}
    		}

  8. #8
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: java while loop

    if(text.length() == str.length()) { <-- what`s that? What is text? ... can you give us a full example?! I think you dont need that line ....

  9. #9
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: java while loop

    ok - i removedit. but still, it just doesn't list any tasks... i call on the method. it does system.out.print. it doesn't print out anything.

  10. #10
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: java while loop

    ok, if you don`t post a whole example or description, i will:
    the input file (tasks file):
    Java Code:
    345
    tcp
    tcp
    tcp
    tcp
    my simple java code:
    Java Code:
    	public static void main(String[] args) throws Exception {
    		Scanner scanner = new Scanner(new File("tasks.txt"));  //import java.util.Scanner; - but you can also use the BufferedReader!
    		Pattern pattern = Pattern.compile("(.+)(.+)(.+)");
    		while (scanner.hasNextLine()) {
    			Matcher m = pattern.matcher(scanner.nextLine());
    			if (m.find()) {
    				System.out.println("Task Title: " + m.group(1)+ " Task Content: " + m.group(2) + " Task Priority: " + m.group(3));
    			}
    		}
    	}
    the generated output:
    Java Code:
    Task Title: 3 Task Content: 4 Task Priority: 5
    Task Title: t Task Content: c Task Priority: p
    Task Title: t Task Content: c Task Priority: p
    Task Title: t Task Content: c Task Priority: p
    Task Title: t Task Content: c Task Priority: p
    that`s what you want or did I misunderstand you/something?
    sorry but I do not know how I can really help now ?!

  11. #11
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: java while loop

    i will get right back to you once as i am able to get my code up and running. but yes, that is what i am trying to do.

  12. #12
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: java while loop

    how would i use the buffered reader with this, and what would be the benefits of using it, instead of the scanner?

  13. #13
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: java while loop

    No benefit!
    The scanner class has the beneifits if you ask me :)
    How to use the buffered reader please ask google or look at your code in posting #1 :)

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  2. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  3. Using while loop in Java?
    By popeyito18 in forum New To Java
    Replies: 4
    Last Post: 06-26-2011, 02:15 AM
  4. Help with loop in java
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:36 AM
  5. Help, loop with java
    By cachi in forum New To Java
    Replies: 5
    Last Post: 08-01-2007, 06:03 AM

Posting Permissions

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