ok so i need a code that can read a list of random numbers from a note pad and then sort them, i have it set that it reads the notepad that it is supposed to but how do i make it sort

// this is what the notepad file reads 8,9,7,6,5,4,10,3,2,1

and the code that reads it and prints it is this

  public static void main(String []args)throws IOException{
    File file = new File("num.txt");
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;

    try {
      fis = new FileInputStream(file);

      // Here BufferedInputStream is added for fast reading.
      bis = new BufferedInputStream(fis);
      dis = new DataInputStream(bis);

      // dis.available() returns 0 if the file does not have more lines.
      while (dis.available() != 0) {

      // this statement reads the line from the file and print it to
        // the console.
        System.out.println(dis.readLine());
      }

      // dispose all the resources after using them.
      fis.close();
      bis.close();
      dis.close();

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

and it dose print the numbers
please help me

Recommended Answers

All 5 Replies

you use a loop to go through all the elements.
for every element:

if ( element < nextElement)
swapPlace(element, nextElement)

note that you have to use 2 nested loops here, or you'll only sort a part of the array

but the numbers are not in an array yet how do i make it do that

From your first post, you say that the notepad has this line:
// this is what the notepad file reads 8,9,7,6,5,4,10,3,2,1
Is it true that the file will have only one line with all the numbers?

yes

Then you can use the method split:

String line = "1,2,3,4,5,6";
String [] tokens = line.split(",");
// tokens now has as elements: {1,2,3,4,5,6}

The split method creates an array with elements the "elements" of the String separated by the delimiter chosen. In this case the ','

After you read the line, get the array with numbers that you need into an array.
Create an int array of equal size with the Stirng tokens array and convert the elements of the String array to ints and put them into the int array:

int [] array = ...
for-loop {
   array[i] = Integer.parseInt(tokens[i]);
}
// sort the int array.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.