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

Dani AI

Generated

got the right idea (tokenize the input) and is correct if the goal is to learn sorting by hand. For a simple, reliable solution that avoids manual swaps, let Java parse the integers and use the built-in sort.

A compact approach is to use Scanner with a comma delimiter, collect the ints into a List<Integer>, then call Collections.sort(...). The Scanner approach parses numbers directly (no explicit split/Integer.parseInt loop) and the sort is O(n log n), so it will be far faster than nested-loop sorts on larger inputs.

import java.io.File;
import java.util.*;

try (Scanner sc = new Scanner(new File("num.txt"))) {
    sc.useDelimiter("\\s*,\\s*"); // allows optional spaces around commas
    List<Integer> nums = new ArrayList<>();
    while (sc.hasNext()) {
        if (sc.hasNextInt()) nums.add(sc.nextInt());
        else sc.next(); // skip unexpected tokens
    }
    Collections.sort(nums); // ascending order
    for (int n : nums) System.out.println(n);
}

Notes and troubleshooting: use the exact filename or an absolute path if the file isn't found; trim or skip non-numeric tokens if the file can contain stray text; use Arrays.sort(int[]) if you prefer a primitive array. If the goal is learning algorithms, implement the nested-loop sort yourself, but for real code prefer the standard libraries. See the Java docs for Scanner and Collections.sort for details: Scanner (Java API) and Collections.sort(List).

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.