where the user enter a series of integers in a loop. The user enter -99 to signal the end of the series.Where all of the integer will be save in a file. After all the numbers have been entered, the program should read all of the integers save in the file and display all of numbers from smallest to largest.
I been trying but I just cant figure out, how to display the integers in order.Now I'm stuck. Please help! and thank you, I'm still a beginner.
package chapter4;
import java.util.Scanner;
import java.io.*;
import java.util.Random;
public class ProChallenge10 {
public static void main(String[]args) throws IOException{
int integer =0; //Integer enter by the user.
boolean signal = false; // To end the loop.
Scanner keyboard = new Scanner(System.in);
//Created the file for the integers entered.
PrintWriter outputFile = new PrintWriter("ProChallenge10.txt");
//Let the user enter a series of numbers.
while(signal == false){
System.out.println("Enter an integer");
integer = keyboard.nextInt();
outputFile.println(integer);
//To end the program.
if(integer == -99){
signal = true;
//Close the outputFile.
outputFile.close();
}
}
//Open the file and read input from the file.
File file = new File("ProChallenge10.txt");
Scanner inputFile = new Scanner(file);
//Read all of the values from the file and display their numbers.
while(inputFile.hasNext()){
int number = inputFile.nextInt();
}
//Close the InputFile.
inputFile.close();
}
}
Aucun commentaire:
Enregistrer un commentaire