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:
Code java:
/** 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.
Re: can't get all the information out of my array at once... please help
Quote:
Code Java:
System.out.println(aliceline[1]);
You're repeatedly printing out only the first line. You need to print out the line'th aliceline string.
Code Java:
System.out.println(aliceline[line]);
Re: can't get all the information out of my array at once... please help
Code java:
/** 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.
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.