Results 1 to 5 of 5
  1. #1
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default Little help with java count++

    I've created a program below that reads in 20 lines from a file, saves them and prints them out to the screen.

    However I want to be able to print out every other line, for example line 2, line 4, line 6...

    I thought this would be as easy as changing the (i) number, but I must have been mistaken.

    Can anyone nudge me in the right direction please? :)

    Java Code:
    import java.util.Scanner;
    import java.io.File;
    
    class testone{
    	public static void main (String args[])throws Exception{
    
    		File myFile = new File("alice.txt");
    		Scanner fileScan = new Scanner(myFile);
    
    		String[] line = new String[20];
    		int count=0;
    		
    			while (count<=19)
    				{
    					line[count] = fileScan. nextLine();
    					count++;
    				}
    		
    		   for(int i=0; i<line.length; i++){
    		               System.out.println(line[i]);
              }
    	}
    }

  2. #2
    smray7 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    ok omg, i think i may know something. after your for statement try putting
    if (i % 2 = =0)
    {
    print statement
    }

    you can try this but i'm no expert. good luck!

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

    Default

    Quote Originally Posted by smray7 View Post
    ok omg, i think i may know something. after your for statement try putting
    if (i % 2 = =0)
    {
    print statement
    }

    you can try this but i'm no expert. good luck!
    This didn't work... I'm guessing it's to do with the i % 2 part..... I'll mess around with it and see if I can get it working! Thanks for trying! :)

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

    Default

    It's the for loop that needs to be changed, and in the for loop the last part of the top line tells the loop how you want to change the index variable. You currently have it incrementing by one each time via i++. Instead what if you do i += 2?

    Java Code:
    for(int i=0; i<line.length; i += 2) {
       System.out.println(line[i]);
    }

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

    Default

    Quote Originally Posted by Fubarable View Post
    It's the for loop that needs to be changed, and in the for loop the last part of the top line tells the loop how you want to change the index variable. You currently have it incrementing by one each time via i++. Instead what if you do i += 2?

    Java Code:
    for(int i=0; i<line.length; i += 2) {
       System.out.println(line[i]);
    }
    Thank you... I worked out I could do it by using a if else loop and typing i ==2 else if i==4 etc... but your way works much easier and tidier.
    Thanks once again! :D

Similar Threads

  1. Replies: 2
    Last Post: 10-24-2010, 04:34 PM
  2. post count
    By codeflip in forum Forum Lobby
    Replies: 2
    Last Post: 03-12-2009, 02:20 AM
  3. Select Count
    By Apple2 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-29-2008, 09:02 AM
  4. Getting row count
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 08:49 AM
  5. making a count down timer using java
    By saytri in forum New To Java
    Replies: 3
    Last Post: 12-29-2007, 09:49 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
  •