package arraySort;
import java.io.IOException;
import java.io.File;
import java.util.*;
public class openFile {
int x;
static int i;
static int[] myList = {100};
public static void main(String[] args){
try{
File myFile = new File("arraySort.txt");
Scanner scan = new Scanner(myFile);
while(scan.hasNext()){
myList[i] = scan.nextInt();
BubbleSort(myList);
System.out.println(myList[i]);
}
catch(IOException e){
System.out.println("File not found!");
}
}
public static void BubbleSort(int[] x){
if (x[i] > x[i + 1]){
int temp;
temp = x[i];
x[i] = x[i+1];
x[i+1] = temp;
}
}
}
views:
314answers:
5http://www.leepoint.net/notes-java/data/arrays/32arraybubblesort.html <- some bubble sort example for you ;)
Rather than give you the answer outright, here are a couple of hints:
You don't have any loops in
BubbleSort()
.You should only call
BubbleSort()
once, after you've read in all the numbers from the file. Meaning, move the call outside of thewhile
loop.You never increment the variable
i
so you're just overwritingmyList[0]
each time through yourwhile
loop.Arrays are not resizable. If you try to assign to
myList[1]
ormyList[2]
you will get anArrayIndexOutOfBoundsException
error. There are several ways to solve this--one is to change it fromint[] myList = {100}
toArrayList myList = new ArrayList()
. You can add numbers to it withmyList.add(number)
and look them up withmyList.get(i)
.
Change this:
try{
File myFile = new File("arraySort.txt");
Scanner scan = new Scanner(myFile);
while(scan.hasNext()){
myList[i] = scan.nextInt();
BubbleSort(myList);
System.out.println(myList[i]);
}
catch(IOException e){
System.out.println("File not found!");
}
to:
try{
File myFile = new File("arraySort.txt");
Scanner scan = new Scanner(myFile);
while(scan.hasNext()){
myList[i] = scan.nextInt();
}
catch(IOException e){
System.out.println("File not found!");
}
BubbleSort(myList);
System.out.println(myList[i]);
}
Change sort method according to answer by @Federico
Your program has several problems, not just related to the sorting part.
static int[] myList = {100};
This line defines myList
as an array with size 1, containing the single element 100
. Then, your main loop is
while(scan.hasNext()) {
myList[i] = scan.nextInt();
BubbleSort(myList);
System.out.println(myList[i]);
}
You do not increase i
in this loop so you are just overwriting the single value in myList
with whatever value you read from the file. And when your Bubblesort
function tries to access myList[i+1]
, it throws an ArrayIndexOutOfBoundsException
because there is no element at index i+1
(which equals 1
, since you don't increase i
).
In general, and especially for a beginner, it is better to use ArrayList
than a regular array. Also, you should fill in the array first and only after it has all the elements should you attempt to sort it. Finally, it is a better idea to make the variables local instead of class members. So that would make your main
function something like
ArrayList myList = new ArrayList();
while(scan.hasNext()) {
myList.append(scan.nextInt());
}
Bubblesort(myList);
And then change Bubblesort
to take an ArrayList
, and then you can also make the loop index i
local to the Bubblesort
method. After that is done, you can work on getting the bubble sort algorithm working. Remember to be careful with your array indices there so that you never access outside the bounds of the array.
When you have finished this assignment you might appreciate Collections.sort() :)
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#sort%28java.util.List%29