A few notes on what you have now. First, please put JAVA tags around your code, it makes it easier to read and ensures faster and better assistance. To figure out how to use JAVA tags, read my signature.
Second, based on the assignment, I can understand the loop going to 20, but when you use Scanner, you usually want to go until there are no more lines left. OR, at least check to see if the line you are reading in exists. Basically,
scan.nextLine(); will return
null when the next line doesnt exist. So, there are two ways that this can be done more efficiently. First, if you want to read all of the lines, instead of
String alice;
for (int a=0; a<20; a++)
{
alice = scan.nextLine();
System.out.println(alice);
}
you would want something like:
String alice = scan.nextLine();
while(alice!=null)
{
System.out.println(alice);
alice = scan.nextLine();
}
Alternatively, if you want to read only 20 lines, you should put a check in your statement:
for (int a=0; a<20; a++)
{
alice = scan.nextLine();
if(alice==null)
break;
System.out.println(alice);
}
This guarantees you will not continue to read when you reach the end of the file and prevents possible exceptions from being thrown.
I will tell you how to do all of these, but not give you the code for it. Try to figure out the code based on the directions I give and if you have any trouble with the code, post it and I'll help you debug.
3. So the first thing we need to do is remember each line when we read it in. I noticed you already made a String[] named text. This is where you want to store your lines of text. So, when you read in the line of text in your loop above, you should also add that line of text to the String array. Then, after you have read in all of the lines, you want to go through the array and get each String that has an even index. Why even? Because the indexes start at 0. So you want to get indexes 0,2,4,6,...16,18. Remember that index #20 does not exist because indexes start at 0 and end at the length of the array minus 1. When you get each String, you want to use the
String.toUpperCase() method. Remember that this method returns a String, not replace the String the method was evoked from. So, you simply want to print out the String the method returns.
4. Now that we have our array of Strings from #3, we can go through the array again. Before looping through the array, you need to declare a variable that will be the total of all the length. Then when you go through each index in the array, use the
String.length() method. This method returns an int representing how many characters are in the String. You want to print that value out, but you also want to add that value to our total variable. Lastly, when you exit the loop, you want to divide the total by 20 (since that is the number of Strings we have) and print out that result as well.
5. Once again, we go through the array of Strings. This time, we use the
String.substring(int,int) method. In this method, the first int is the starting index and the second int is the ending index. This method returns a String of just the characters between the indexes you identified. So, you want to get each String in the array, and print out the value of
String.substring(0,10);.
I think that should be everything. Tell me if you have any trouble.