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

Thread: selecting even numbers from an array

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default selecting even numbers from an array

    Hello,

    I'm stuck with my assignment again which is to:

    1. Read in all 20 lines of text from a file and store them.
    2. Display all the 20 lines of text on the screen
    3. Display every other line in upper case (so the first, then the third etc), you don’t need to display the other line at all (i.e. no output of second, fourth etc.)
    4. Display the number of characters in each line of text and display the average of the characters for all 20 lines of text.
    5. Display the first 10 characters from each line of text

    After solving steps 1 and 2 i soon found myself hitting a brick wall again with step 3, everything i've looked at on the net just seems to be to complicated for what i need to do with this step.

    The code I have so far is:
    /** assignment one reading from a file. */
     
    import java.util.Scanner;
    import java.io.File;
     
    class alice{
     
    	public static void main (String args[]) throws Exception
    	{
     
     
    		File myFile = new File("alice.txt");
    		Scanner Scan = new Scanner(myFile);
    		String []aliceline=new String[20];
    		String uppercase;
     
    		for (int line=0;line<20; line++)
     
     
    		{aliceline[line] = Scan.nextLine();
     
     
     
    		System.out.println(aliceline[line]);
     
     
    	}
     
     
    	{
    		uppercase = aliceline; /** i need this part to read all the even lines from the array*/
     
    		System.out.println(uppercase);
    	}
    		Scan.close();
     
    	}
    }

    I have tried lots of things such as uppercase=aliceline[0][2] or [0+2] but it just won't work and i'm now at a loss.
    If anone could please help me with this........


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: selecting even numbers from an array

    This will help you move forward. It's more simple than you think

    /** assignment one reading from a file. */
     
    import java.util.Scanner;
    import java.io.File;
     
    class alice {
     
    	public static void main(String args[]) throws Exception {
     
    		File myFile = new File("alice.txt");
    		Scanner Scan = new Scanner(myFile);
    		String[] aliceline = new String[20];
    		String uppercase;
     
    		for (int line = 0; line < 20; line++)
    		{
    			aliceline[line] = Scan.nextLine();
    			System.out.println(aliceline[line]);
    		}
     
    		System.out.println("");
     
    		for (int a = 0; a < 20; a++){
    			System.out.println(aliceline[a].toUpperCase());
    			a = a+1;
    		}
     
    		Scan.close();
    	}
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. The Following User Says Thank You to JavaPF For This Useful Post:

    Tate (November 28th, 2010)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: selecting even numbers from an array

    Thankyou so much for your help now i just need to figure out the next steps.

  5. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: selecting even numbers from an array

    Related Thread - http://www.javaprogrammingforums.com...html#post21842

    I may merge these.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: selecting even numbers from an array

    Quote Originally Posted by Tate View Post
    Thankyou so much for your help now i just need to figure out the next steps.
    No problem.

    Try to move forward and show us the code you have done..
    We will help when you get stuck.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. #6
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: selecting even numbers from an array

    Hello,

    I'm now stuck with task 4 i've got as far as to display the number of letters in each line but can't quite figgure out how to display the average of the characters for all 20 lines.

    The code I have now is:

     
    /** assignment one reading 20 lines from a file. */
     
    import java.util.Scanner;
    import java.io.File;
     
    class alice{
     
        public static void main (String args[]) throws Exception
        {
     
     
            File myFile = new File("alice.txt");
            Scanner Scan = new Scanner(myFile);
            String []aliceline=new String[20];
            String uppercase;
     
            for (int line=0;line<20; line++)
     
     
            {aliceline[line] = Scan.nextLine();
     
     
     
            System.out.println(aliceline[line]);
     
     
        }
     
     
        System.out.println("");
     
        for (int odd = 0; odd < 20; odd++){
            System.out.println(aliceline[odd].toUpperCase());
                    odd = odd+1;
     
        }
     
        System.out.println("");
     
            for (int b = 0; b < 20; b++)
            {
     
     
                System.out.println(aliceline[b].length());
                System.out.println(); /** I need to work the average in this part of the code */
     
            }
            Scan.close();
     
        }
    }
    Any help with this will be much appriciated I tried following what someone had put on my other post but i'm affraid that i still seem to be getting no where

  8. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: selecting even numbers from an array

    Use a for-loop to sum up the length of the 20 lines, then divide by 20 to get an average. Bonus hint: use doubles/floats in order to accurately compute the average (otherwise it will be truncated to the nearest integer).

  9. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: selecting even numbers from an array

    Take a look at this thread - http://www.javaprogrammingforums.com...rogram-do.html

    Seeing as your questions are identical, I take it you are in the same class?

    Please be aware that it looks like your University are monitoring these threads.

    Quote Originally Posted by MelanieC View Post
    Hi its Melanie, sounds like your assignment is not going to well!! Just to let you know, you may wish to take a look at the Academic offences, located on myBu. I have advised Nan (who will be marking the work this week) that any work that is believed to be plagarised, will be marked as 0 with a disiplinary with Ruth.
    Just a heads up!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. finding specified NUMBERS in an array
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2010, 10:35 PM
  2. random numbers in array please help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2010, 01:54 AM
  3. Averaging Numbers in an Array Based on References
    By aussiemcgr in forum Java Theory & Questions
    Replies: 6
    Last Post: August 6th, 2010, 06:39 PM
  4. How to write 2 dimensional array of float numbers to binary file?
    By Ghuynh in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 17th, 2010, 04:26 PM
  5. Replies: 0
    Last Post: February 23rd, 2010, 02:12 PM