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

Thread: can't get all the information out of my array at once... please help

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

    Unhappy can't get all the information out of my array at once... please help

    Hello,

    I'm stuck with my assignment 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

    The problem is that I have made an array to store the text but after hours of trying different things I just can't seem to figure out how to read all the information within the array to print the 20 lines of txt to the screen.
    If anyone could help me with this it would be much appriciated, i'm starting to tear my hair out and i'm getting nowhere.

    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 lines;
     
     
    {
    		for (int line=0;line<20; line++)
     
    		aliceline[line] = Scan.nextLine();
     
     
    			System.out.println(aliceline[1]);
     
     
    		Scan.close();
    	}
    	}
    }

    The code gives me line 2 of the txt as it satnds but I can't figure out how to get all the lines to print out, short of doing a system.out.print for each individual line.

    If anyone could please help me with this or just point me in the right direction.
    Thankyou in advance to anyone who can help.


  2. #2
    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: can't get all the information out of my array at once... please help

    System.out.println(aliceline[1]);
    You're repeatedly printing out only the first line. You need to print out the line'th aliceline string.

    System.out.println(aliceline[line]);

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

    javapenguin (November 26th, 2010)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: can't get all the information out of my array at once... please help

     
    /** 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];
     
     
     
     
    		for (int line=0;line<20; line++)
    {
    		aliceline[line] = Scan.nextLine();
     
     
    			System.out.println(aliceline[line]);
    }
     
    		Scan.close();
     
    	}
    }

    This should work.

  5. The Following User Says Thank You to javapenguin For This Useful Post:

    Tate (November 28th, 2010)

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

    Default Re: can't get all the information out of my array at once... please help

    Thankyou so much for your reply, I knew it had to be something simple like that, i had already tried it but it turns out I had some brakets in the wrong place. again thankyou for your help. I just need to work out step 3 and 4 now.

Similar Threads

  1. [SOLVED] Class not passing information
    By DiPep in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 26th, 2010, 10:04 AM
  2. Some information on retrieving packets
    By Cheetah in forum Java Networking
    Replies: 0
    Last Post: July 11th, 2010, 08:43 PM
  3. How do I access SENS information in JAVA?
    By cabodge in forum Java Theory & Questions
    Replies: 4
    Last Post: March 9th, 2010, 05:52 PM
  4. Getting information from a folder
    By shadihrr in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 23rd, 2010, 03:13 PM
  5. Passing Information between classes
    By SKhoujinian91 in forum Object Oriented Programming
    Replies: 4
    Last Post: December 8th, 2009, 02:40 PM