Reputation: 71
So our professor has assigned us with a program which reads a text file he has provided us with; it sorts it, and creates a new file with the sorted stuff. He wants us to call three methods from the main i.e. read, sort, and write
I have done some of the work, but i'm confused what arguments to provide for io.sort. And how do i convert that text thing into an array to provide an argument Here's my code:
public void read() {
try {
Scanner myLocal = new Scanner(new File("dictionary.txt"));
while (myLocal.hasNextLine()) {
System.out.println(myLocal.nextLine());
}
} catch (IOException e) {
System.out.println(e);
}
}
public void sort(String[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
/* ERROR: When j == 0, j - 1 == -1, which is out of bounds */
if (arr[j - 1].compareTo(arr[j]) > 0) {
swap(j, arr);
}
}
}
}
public void swap(int j, String[] arr) {
String temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
public void write() {
try {
PrintStream writer = new PrintStream(new File("sorted.txt"));
for (int i = 0; i < 100; i++) {
writer.println(i);
}
writer.close();
} catch (IOException e) {
System.out.println(e);
}
}
Upvotes: 0
Views: 86
Reputation: 71
CORRECT CODE (SOLVED)
class IO{
String[] myArray = new String[30000];
public void read()
{
try {
Scanner myLocal = new Scanner( new File("dictionary.txt"));
while (myLocal.hasNextLine()){
for (int i=0; i<myArray.length; i++){
String a = myLocal.nextLine();
myArray[i] = a;
}
}
}
catch(IOException e){
System.out.println(e);
}
}
public void sort()
{
int n = myArray.length;
for (int i=0; i<n; i++){
for(int j=1; j<n-i; j++){
if (myArray[j-1].compareTo(myArray[j])>0){
swap(j, myArray);
}
}
}
}
public void swap(int j, String[] myArray)
{
String temp = myArray[j-1];
myArray[j-1]=myArray[j];
myArray[j]=temp;
}
public void write()
{
try{
PrintStream writer = new PrintStream(new File("myIgnoreNew.txt"));
for (int i=0; i<myArray.length; i++){
writer.println(myArray[i] + "\n");
}
writer.close();
}
catch(IOException e){
System.out.println(e);
}
}
}
Upvotes: 1
Reputation: 71
class IO{
String[] myArray = new String[30000];
public void read()
{
try {
Scanner myLocal = new Scanner( new File("dictionary.txt"));
while (myLocal.hasNextLine()){
for (int i=0; i<myArray.length; i++){
String a = myLocal.nextLine();
myArray[i] = a;
}
}
}
catch(IOException e){
System.out.println(e);
}
}
public void sort()
{
int n = myArray.length;
for (int i=0; i<n-1; i++){
for(int j=0; j<n-i-1; j++){
if(myArray[j+1].compareTo(myArray[j])<0){
String temp = myArray[j];
myArray[j] = myArray[j+1];
myArray[j+1] = temp;
//toLower
}
}
}
}
public void swap(int j, String[] arr)
{
String temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
public void write()
{
try{
PrintStream writer = new PrintStream(new File("sorted.txt"));
for (int i=0; i<myArray.length; i++){
writer.println(myArray[i] + "\n");
}
writer.close();
}
catch(IOException e){
System.out.println(e);
}
}
}
Upvotes: 1
Reputation: 71
Here is how i've changed my read() method:
public void read()
{
String[] myArray = new String[1000];
try {
Scanner myLocal = new Scanner( new File("dictionary.txt"));
String a = myLocal.nextLine();
while (myLocal.hasNextLine()){
for (int i=0; i<myArray.length; i++){
myArray[i] = a;
}
}
}
catch(IOException e){
System.out.println(e);
}
}
Upvotes: 1