Modify it so that it write the contents of String[] into a file named fileLoop1.txt: package stringio; // // import java.util.Scanner; import java.io.*; // public class Stringio { // public static void main(String[] args) throws IOException { File loopfile = new File("fileLoop.txt"); Scanner getAll = new Scanner( loopfile ); // connect a Scanner to the file String num; String[] items = new String[10]; int i = 0; while(getAll.hasNextLine()){ num = getAll.nextLine(); // square = num * num ; //System.out.println("The square of " + num + " is " + square); items[i] = num; System.out.println("items[i] is " + items[i]); i = i + 1; } getAll.close(); } }
1. Modify it so that it write the contents of String[] into a file named
fileLoop1.txt:
package stringio;
//
//
import java.util.Scanner;
import java.io.*;
//
public class Stringio {
//
public static void main(String[] args) throws IOException
{
File loopfile = new File("fileLoop.txt");
Scanner getAll = new Scanner( loopfile ); // connect a Scanner to the file
String num;
String[] items = new String[10];
int i = 0;
while(getAll.hasNextLine()){
num = getAll.nextLine();
// square = num * num ;
//System.out.println("The square of " + num + " is " + square);
items[i] = num;
System.out.println("items[i] is " + items[i]);
i = i + 1;
}
getAll.close();
}
}
Step by step
Solved in 3 steps with 2 images