Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28
  1. #21
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Sorted. I changed the 'for' loop to a 'do while' loop and just set it to print 20 lines..

    Java Code:
    do
          {
             System.out.println(line[i]);
             i++;
          }
          while (i <= 10);
    It seems to work, so I'll use that...

    Thanks for the help everyone. I'm sure I'll be back to pester you all soon, seeing as this assignment is due in 3 weeks. :D

  2. #22
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    pssst: a for loop is probably a better choice for looping when you know in advance how many times you are going to loop. :)

  3. #23
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    pssst: a for loop is probably a better choice for looping when you know in advance how many times you are going to loop. :)
    Any chance you want to put me out of my misery and give me the 'for' loop code that I'd need to use, so that I can go to bed? :p

    Java Code:
    import java.util.Scanner;
    import java.io.File;
    
    class safe{
    	public static void main (String args[])throws Exception{
    
    		File myFile = new File("alice.txt");
    		Scanner fileScan = new Scanner(myFile);
    
    		String[] line = new String[700];
    		int count=1;
    		int i = 1;
    
    		while (fileScan.hasNextLine()){
    			line[count] = fileScan. nextLine();
    			count++;
    		}
    
    		 do
    		      {
    		         System.out.println(line[i]);
    		         i++;
    		      }
          while (i <= 10);
    	}
    fileScan.close();
    }
    `

    The previous for code that I had was
    Java Code:
    for (String num : line){
    	        System.out.println(num);
                    }

  4. #24
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ls7897 View Post
    Any chance you want to put me out of my misery and give me the 'for' loop code that I'd need to use, so that I can go to bed? :p
    Nah, it's your assignment not mine. Any basic Java tutorial will show you how to do a for loop.

    The previous for code that I had was
    Java Code:
    for (String num : line){
    	        System.out.println(num);
                    }
    That's a for-each loop, not a standard for loop.

  5. #25
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Nah, it's your assignment not mine. Any basic Java tutorial will show you how to do a for loop.



    That's a for-each loop, not a standard for loop.
    Java Code:
    		   for(int i=1; i<=20; i++){
    		               System.out.println(line[i]);
              }
    seems to work.

    Ah sleep!
    Thanks for your help! :)
    Last edited by ls7897; 11-10-2010 at 02:23 AM.

  6. #26
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  7. #27
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Quote Originally Posted by ls7897 View Post
    seems to work.
    I hope you had a good sleep so I can be a pain in your [not so nice word/] now. ;)

    You program compiles and it runs but I don't think it does what you expect: the first line in your file won't be printed.

    Re-read my yesterday's reply about the value of an index.

    Erik
    I'm new to Java but I like to help where ever I can. :)

  8. #28
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by venerik View Post
    I hope you had a good sleep so I can be a pain in your [not so nice word/] now. ;)

    You program compiles and it runs but I don't think it does what you expect: the first line in your file won't be printed.

    Re-read my yesterday's reply about the value of an index.

    Erik
    I realised that after posting this and changed it so it goes from 0 now. :)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  2. do while loop
    By sr20guy in forum New To Java
    Replies: 16
    Last Post: 04-08-2010, 03:59 AM
  3. need a loop to add new row(s)
    By doha786 in forum New To Java
    Replies: 1
    Last Post: 02-03-2010, 06:21 AM
  4. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  5. Loop, is it what I need?
    By dbashby in forum New To Java
    Replies: 4
    Last Post: 04-14-2009, 04:28 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
  •