Re: ReadWrite program advise
Well I corrected my code to print and write 100 integers. On the other hand I'm completely stumped about how to sort it. Help me please!
Do I need an array? Where should that code go if I do? If not, then what?
Code java:
import java.util.Random;
import java.util.Scanner;
public class ReadWriteData {
// Main method
public static void main (String[] args)throws Exception {
//Create a file instance
java.io.File file = new java.io.File("100 random sorted numbers txt.");
if (file.exists()) {
System.out.println("File Already Exists");
System.exit(0);
}
//Create a file
java.io.PrintWriter output = new java.io.PrintWriter(file);
//Create ramdom number to be sorted
Random randomGenerator = new Random();
for (int i = 0; i < 100; i++) {
//Write formatted output to the file
output.println(randomGenerator.nextInt(100));
}
//close the output file
output.close();
//Create a Scanner for the file
Scanner input = new Scanner(file);
//Read data from a file
while (input.hasNext()) {
int score = input.nextInt();
System.out.println(
score);
}
//Close the input file
input.close();
}
}
Re: ReadWrite program advise
I'm almost there. I decided to use my own class to sort but it's not working. It might be the placement.
Take a look...
Code java:
import java.util.Random;
import java.io.*;
public class ReadWriteSort {
//Creates an exception
public static void main (String [] args)throws IOException {
//Assigns the variable digit as a random number
Random digit = new Random();
//Array with 100 numbers
int list[] = new int[100];
File file = new File("Exercise9_19.txt");
if (file.exists()){
System.out.println("The file " + file.getName() + " already exists.");
System.out.println();
}//ends if statement
else {
System.out.println("Your file has been written to " + (file.getName()+ "."));
System.out.println();
}
try {
Writer output = null;
//Name of txt file
File x = new File("Exercise9_19.txt");
output = new BufferedWriter(new FileWriter(x));
//for loop to create 100 random numbers
for (int i = 0; i < list.length; i++) {
//Set an array of random numbers with a range of 0 - 100
list[i] = (digit.nextInt(100));
//output is the array of random numbers
output.write(list[i]+" ");
}//ends for loop
SelectionSort.selectionSort(list);//THIS MY BE MY PROBLEM!
//ends output
output.close();
FileInputStream fstream = new FileInputStream("Exercise9_19.txt");
// Get the object of DataInputStream
DataInputStream input = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(input));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
input.close();
}//ends try
//If there is no file
catch (IOException e) {
System.err.println("The file was not saved.");
e.printStackTrace();
}//ends catch
}
//Selction sort method
public static class SelectionSort {
static void selectionSort(int list[]) {
for (int i = 0; i < list.length - 1; i++) {
int currentMin = list[i];
int currentMinIndex = i;
for( int j = i + 1; j < list.length - 1; j++) {
if (currentMin > list[j]){
currentMin = list[j];
currentMinIndex = j;
}
}
if (currentMinIndex != i ){
list[currentMinIndex] = list[i];
list[i] = currentMin;
}
}
}
}
}//ends class
Please show me the way of my error!
Re: ReadWrite program advise
Define 'its not working'... you've got a lot of code there that does several things so it can fail at any number of places. Break the problem down into its individual components and focus on what fails...if its the sort, then remove all else but the sort and some sample test input and go from there - post that code if you are having problems (this in other words, is the process of writing an SSCCE)
Re: ReadWrite program advise
You don't have to create a separate class for sorting an array of ints. Just write a separate method for it.
Re: ReadWrite program advise
1. Use list instead of array and use it's built in sort.
2. I mean to say, first put all random numbers in the list, sort them and place them in the file.