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

Thread: Stuck again

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

    Default adding all values of an array and finding the average??

    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

    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
    Last edited by Tate; November 28th, 2010 at 10:45 AM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Stuck again

    Ok, so before you loop through and get the lengths of each line, you want to declare an int variable. This int variable will hold the sum of all of the lengths. Then at the end you will have to divide the number in the int by the number of things added to it (20 in this case).

    I wrote this program to demonstrate what I am saying:
    public class TestRead 
    {
        public static void main(String[] args) 
        {
        	int total=0;
     
    		for (int i = 0 ; i<50; i++)
    		{
    			int value = (int)(20*Math.random());
    			total += value;
    			System.out.println(value);
    		}
     
    	    System.out.println(total/50);
        }
    }
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    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: Stuck again

    Related Thread - http://www.javaprogrammingforums.com...ers-array.html

    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.

Similar Threads

  1. Stuck help please!
    By mindlessn00b in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 5th, 2010, 05:31 PM
  2. Stuck, looking for input
    By ptabatt in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 30th, 2010, 02:54 AM
  3. I am stuck
    By hawkman4188 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 29th, 2010, 12:46 AM
  4. Stuck :(
    By hing09 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 17th, 2010, 05:20 PM
  5. I'm stuck
    By iank in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2009, 10:21 AM