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.