you must create the data files with a text tool, such as Notepad exampleThree In the while loop, this program reads fileLoop.txt until all items are read package hasnext; import java.util.Scanner; import java.io.*; // public class Hasnext { 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 int num = 0, square = 0; while(getAll.hasNextInt()) { num = getAll.nextInt(); square = num * num ; System.out.println("The square of " + num + " is " + square); } getAll.close(); } }
you must create the data files with a text tool, such as Notepad
exampleThree
In the while loop, this program reads fileLoop.txt until all items are read
package hasnext;
import java.util.Scanner;
import java.io.*;
//
public class Hasnext {
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
int num = 0, square = 0;
while(getAll.hasNextInt())
{
num = getAll.nextInt();
square = num * num ;
System.out.println("The square of " + num + " is " + square);
}
getAll.close();
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 3 images