Results 1 to 7 of 7
- 12-01-2010, 01:43 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
Write a program that sorts data from a text file and sort them in a file
Hi there ...This is my first semester to Java and I use Dr.Java.
The problem is Generate a list of 100 random numbers between 1-100 in range [1,100] to a file randomFile.txt Which I did already..
The assignment says to read from the randomFile.txt file and sort them (data) in order and write to a file called sortedFile.txt
Here is the code so far......
The Error is that it does not read the randomFile.txt from the desktop
could someone please help whats wrong???
The following numbers are from the randomFile.txt
41
40
6
65
59
16
97
84
42
18
32
32
1
34
52
80
73
6
43
33
39
22
75
35
44
91
73
89
73
6
5
57
42
29
41
64
66
67
8
98
79
40
73
66
94
81
71
38
93
78
35
95
85
38
1
24
44
70
76
12
37
40
89
31
64
19
44
31
73
88
94
11
60
19
41
98
12
32
82
41
94
87
45
31
81
87
48
56
82
71
16
58
92
29
66
70
26
14
57
49
Here is the code so far......
____________________________________________
//Daniel Mendoza
//Cecs 100
//Assignment 11
//
import java.io.*;
public class Datasort1
{
public static void main (String[]args) throws Exception
{
System.out.println("start");
final int size =100;
int j;
int k;
int temp;
int[] x;
x=new int[size];
FileReader fr=new FileReader("randomFile.txt");
BufferedReader br= new BufferedReader(fr);
FileWriter fw =new FileWriter("outputFile.txt");
PrintWriter pw= new PrintWriter(fw);
pw.println("Dan Mendoza: outputfile.txt Sorted Data");
j=0;
while (j<size-1)
{
x[j]=Integer.parseInt(br.readLine());
k=j+1;
while (k<size)
{
if (x[j]>x[k]){
temp=x[j];
x[j]=x[k];
x[k]=temp;
}
k=k+1;
}
j=j+1;
pw.println(x[j]);
{
pw.flush();
pw.close();
br.close();
System.out.println("stop");
}
}
}
}
- 12-01-2010, 01:59 AM #2
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
Hey there,
I dont know much about Java at all so this is probably totally wrong but
FileWriter fw =new FileWriter("outputFile.txt");
should that not write to randomFile.txt ?
if i'm reading it right you're writing the numbers to that file then trying to read another file
but as i said i'm probably totally wrong
- 12-01-2010, 02:12 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 33
- Rep Power
- 0
You say:
The Error is that it does not read the randomFile.txt from the desktop
And you open the file with:
FileReader fr=new FileReader("randomFile.txt");
Looks like you have a file path problem. If you copy randomFile.txt to your project directory then it should be able to open the file. If the file must be somewhere else, then use a complete path to the file.
- 12-01-2010, 02:15 AM #4
Hi,
I'm also very new to Java so just throwing this out here....
Does not the fileReader look in the current Directory>
I think the Constructor takes a Path also?
Java Code:FileReader fr=new FileReader("C:\\Users\Me\\Desktop\\randomFile.txt");
- 12-01-2010, 02:25 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
I did what code newbie told me and get this error:
java.io.IOException: Stream closed
at java.io.BufferedReader.ensureOpen(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at Datasort1.main(Datasort1.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:271)
- 12-01-2010, 03:07 AM #6
Hi....
Maybe try with IOException?
Im still learning myself so Please research IOExceptions
Java Code:public static void main(String[] args) { try { FileReader fr = new FileReader("c:\\Users\\Me\\Desktop\\randomFile.txt"); }catch (IOException e){ System.out.println("Opps!...no findy file"); }
- 12-01-2010, 06:31 AM #7
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
This is what I get but it only prints out zeros
This is what I get but it only prints out zeros in the outputFile.txt
_________________________________________
import java.io.*;
public class Datasort1
{
public static void main (String[]args) throws Exception
{
System.out.println("start");
final int size =100;
int j;
int k;
int temp;
int[] x;
x=new int[size];
FileReader fr=new FileReader("C:\\Users\\Danny\\Desktop\\randomFile. txt");
BufferedReader br= new BufferedReader(fr);
FileWriter fw =new FileWriter("outputFile.txt");
PrintWriter pw= new PrintWriter(fw);
pw.println("Dan Mendoza: outputfile.txt Sorted Data");
j=0;
while (j<size-1)
{
x[j]=Integer.parseInt(br.readLine());
k=j+1;
while (k<size)
{
if (x[j]>x[k])
{
temp=x[j];
x[j]=x[k];
x[k]=temp;
}
k=k+1;
}
j=j+1;
}
int a = 0;
while (a < size)
{
pw.println(x[a]);
a=a+1;
}
pw.flush();
pw.close();
br.close();
}
}
Similar Threads
-
How to write output data to a file
By xana in forum NetBeansReplies: 39Last Post: 08-14-2010, 02:19 AM -
How to write text file into Array
By venkat.ravala in forum New To JavaReplies: 13Last Post: 11-19-2009, 05:59 PM -
PROOF READ: Sort text file 3 different ways and compare
By VinceGuad in forum New To JavaReplies: 2Last Post: 01-26-2009, 05:28 PM -
Write a List into a Text file
By tech2000 in forum New To JavaReplies: 1Last Post: 11-13-2008, 06:09 AM -
how do i write to a text file from an arraylist?
By otoro_java in forum New To JavaReplies: 3Last Post: 01-30-2008, 07:53 AM
Bookmarks