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 can't figure out, how to display the integers in order. Now I'm stuck. As you are able to see, I did not use any fancy programming, such as array or list, because I'm still learning about classes and had not learn any of that yet.
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();
}
}
Add them to the List
then sort()
before printing it to the text file.
import java.util.ArrayList;
import java.util.List;
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);
List<Integer> listofInt = new ArrayList<Integer>();// Array List for your integers
//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();
listofInt.add(integer);// adding of int to list
//To end the program.
if(integer == -99){
listofInt.sort(null);// sort the list
for(int x=0;x<listofInt.size();x++){
outputFile.println(listofInt.get(x));//Printing of list to text file
}
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();
}
}