How do I open a text file to sort using bubble sort?

Hi,
I have a text file, which is a list of #'s.. ex)
0.001121
0.313313
0.001334
how do you open a text file and read a list? I have the bubble sort code which sorts.. thanks

I wrote this, but am getting a couple errors:
import java.util.*;
import java.io.*;
public class sort
     public static void main(String[] args)
          String fileName = "numsRandom1024.txt";
          Scanner fromFile = null;
          int index = 0;
          double[] a = new double[1024];Don't use double. You should use something that implements Comparable, like java.lang.Double.
          try
               fromFile = new Scanner(new File(fileName));
          catch (FileNotFoundException e)
System.out.println("Error opening the file " +
" + fileName);
               System.exit(0);
          while (fromFile.hasNextLine())
               String line = fromFile.nextLine();
               a[index] = Double.parseDouble(line);Don't parse to a double, create a java.lang.Double instead.
               System.out.println(a[index]);
               index++;
          fromFile.close();
          bubbleSort( a, 0, a.length-1 );No. Don't use a.length-1: from index to a.length-1 all values will be null (if you use Double's). Call it like this:bubbleSort(a, 0, index);
public static <T extends Comparable<? super T>> void
d bubbleSort( T[] a, int first, int last )
          for(int counter = 0 ; counter < 1024 ; counter++)
for(int counter2 = 1 ; counter2 < (1024-counter) ;
) ; counter2++)Don't let those counter loop to 1024. The max should be last .
                    if(a[counter2-1] > a[counter2])No. Use the compareTo(...) method:if(a[counter2-1].compareTo(a[counter2]) > 0) {
                         double temp = a[counter2];No, your array contains Comparable's of type T, use that type then:T temp = a[counter2];
// ...Good luck.

Similar Messages

  • Opening a text file in notepad using swing

    Hi,
    I am trying to getting all files and directoriess from my Home Drive to JTreeModel, and i also want whenever i click into text file the contents should be open in notepad, but i am not able to add this feature, can any one help?
    Suresh

    Suresh_Dewangan_1981 wrote:
    No, I am using java 1.5 only, please help me accordingly.
    import java.io.IOException;
    class StartTxt {
        public static void main(String args[])
            throws IOException
            String fileName = "c:\\temp\\test2.txt";
            String[] commands = {"cmd", "/c", "start", "\"DummyTitle\"",fileName};
            Runtime.getRuntime().exec(commands);
    }Bye
    RG.

  • How to open a text file using button click event

    hi, How can i open a text file in a textpad or notepad on the click event of a button.?
    Thanks
    Jay

    Pnt,
    this will not work LV 8.0.1 and LV 8.6 will give back error 193.
    Attached is a VI to use the ShellExecute WinAPI. The VI is LV 7.1.1.
    Message Edited by waldemar.hersacher on 10-09-2008 10:48 PM
    Waldemar
    Using 7.1.1, 8.5.1, 8.6.1, 2009 on XP and RT
    Don't forget to give Kudos to good answers and/or questions
    Attachments:
    ShellExecute.zip ‏27 KB

  • How do I Open an HTML file for iOS?

    How do I open an HTML file for iOS using acrobat?

    This is a forum for Adobe Reader for iOS. Acrobat doesn't run on iOS. You need to run it on a Mac or Windows computer. In Acrobat, you can choose File > Create > PDF from Web Page to do what you want. You cannot do that in iOS.

  • Open a PDF file in linux using java

    Hi..
    How can I open a PDF file in linux using java.
    I am able to open PDF in windows and mac using this code
    in Windows
    Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + path_of_PDF);
    in mac
    Runtime.getRuntime().exec("open " + path_of_PDF);
    But nothing is working with linux.
    Please help
    Thanks

    One thread is enough:
    http://forum.java.sun.com/thread.jspa?threadID=5267458

  • How to open a text file

    hello
    if so have an idea on how to open a text file
    thanks

    File file=new File("[path to the text file]");
    String encoding="[encoding of the file]";
    BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
    // Do whatever you like with the reader
    // For example write the text file to the output
    String line;
    while ((line=reader.readLine())!=null)
    System.out.println(line);
    reader.close();Hope it helps,
    Marcin
    PS. This is not the write forum category to post this question.

  • How do I open a PDF file with Adobe Reader if it is in iBooks

    I have a number of PDF files in my iBooks library.  I just downloaded Adobe Reader for the iPad.  I can't see how I can open those PDF files.  The only thing I seem to be able to do with them is to open them from iBooks directly.  Doing that does not provide me with the features that Adobe Reader would provide (such as searching for particular text, etc.).

    Use iTunes to save the PDF FROM iBooks, and to add them to Adobe Reader.
    Connect your iPad to your computer. Bring up iTunes on your computer.
    To save a PDF FROM iBooks, click on "Books" in the Library section on the left. Select the PDF you want to save and right-click and choose Show in Finder (Show in Explorer in Windows). Go up one level in your computer hierarchy and you'll see all the "Books" (PDFs and eBooks) in iBooks.  You can leave them here if you want to view the PDFs in iBooks in the future, or drag them to the Desktop.
    To move the PDF TO Adobe Reader:
    Return to iTunes. Click on the iPad on the left in Devices. Click on the Apps tab. Scroll down the the File Sharing section as shown below. Click on Adobe Reader. Click the Add button and select the PDFs you identified in the previous step. They'll be added to Adobe Reader

  • How do I compare these text files (aka spell check)

    I'm having the hardest time figuring out how to get this spell check to work. Basically, I need to open the dictionary.txt file and then another file which will be spell checked. Ignoring case sensitivity, punctuation, and word endings (such as ly, ing, s). And then output to the user the words that were not found in the dictionary. Here is what I have so far (and it is very basic.... there is no attempt to compare yet since I don't know how to approach this). I only have it outputting to a JTextArea what is opened minus punctuation and upper case....
    import java.util.Scanner;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    //  Created Sept 4, 2004
    *   @version 1.0
    *   This program will store dictionary.txt to a String array and
    *   open another text file to be specified by the user of the program.
    *   As the scanner proceeds through the document, it will spell check
    *   and output any (case insensitive) words not found in the
    *   dictionary.txt file.
    public class Spell extends JFrame {
        /** Area to place the individual words in the file */
        JTextArea outputDictionary, outputWord;
    *  Constructor for the class.  Sets up the swing components.
    *  Prompts the user for the files to be processed.  Then invokes
    *  the process method to actually do the work.
    public Spell() {
         // A JTextArea is used to display each word found in the file.
         outputDictionary=new JTextArea("Dictionary List\n\n");
         add (new JScrollPane(outputDictionary));
         //Set the size to 300 wide by 600 pixels high
         setSize(300,600);
         setVisible(true);
            outputWord=new JTextArea("Opened Document\n\n");
            add (new JScrollPane(outputWord));
            setSize(300,600);
            setVisible(true);
        //Prompts user for the dictionary.txt file
        JFileChooser dictionary = new JFileChooser();
        int returnVal = dictionary.showOpenDialog(this);
         if  (returnVal == JFileChooser.APPROVE_OPTION) {
              storeDictionary(dictionary.getSelectedFile());
        //Prompt user to open file to be checked          
        JFileChooser word = new JFileChooser();
        int returnVal2 = word.showOpenDialog(this);
            if (returnVal2 == JFileChooser.APPROVE_OPTION){
                storeOpenFile(word.getSelectedFile());
    public void storeDictionary(File dictionaryFile) {
         String dictionary="";
         Scanner scanner=null;
        try {
            // Delimiters specifiy where to parse tokens in a scanner
           scanner = new Scanner(dictionaryFile).useDelimiter("\\s*[\\p{Punct}*\\s+]\\s*");
        catch (FileNotFoundException fnfe) {
            JOptionPane.showMessageDialog(this,"Could not open the file");
           System.exit(-1);
         while (scanner.hasNext()) {         
              dictionary=scanner.next();
              if (!dictionary.equals(""))
                 outputDictionary.append(dictionary.toLowerCase()+"\n");
            System.out.println("Thank you, your dictionary is stored.");
    public void storeOpenFile(File openFile){
        String word="";
        Scanner scanner2 = null;
        try {
            // Delimiters specifiy where to parse tokens in a scanner
           scanner2 = new Scanner(openFile).useDelimiter("\\s*[\\p{Punct}*\\s+]\\s*");
        catch (FileNotFoundException fnfe) {
            JOptionPane.showMessageDialog(this,"Could not open the file");
           System.exit(-1);
        //stores the words in the opened text file in a String array
         while (scanner2.hasNext()) {         
              word=scanner2.next();
                 if(!word.equals(""))
                        outputWord.append(word.toLowerCase()+"\n");
        System.out.println("Thanks, your file has successfully been opened.");
    public static void main(String args[]) {
       Spell spellck = new Spell();
       spellck.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    Yes, he actually recommends us to use java 1.5. I have revised my code to read into a TreeSet from the dictionary.txt, but I keep getting the following error when I compile:
    Note: C:\Jwork\spell1.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    Finished spell1.
    Also, when I execute, nothing happens. It looks like it is going to try to do something, but the "open" dialog box never opens and no errors pop up. It just sits there executing. Any suggestions??
    here is the code:
    import java.util.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    //  Created Sept 4, 2004
    *   @version 1.0
    *   This program will store dictionary.txt to a String array and
    *   open another text file to be specified by the user of the program.
    *   As the scanner proceeds through the document, it will spell check
    *   and output any (case insensitive) words not found in the
    *   dictionary.txt file.
    public class spell1 extends JFrame{
        JTextArea output = new JTextArea(500,500);
        Set dict = new TreeSet();
        Set file = new HashSet();
        public spell1(){
        Set dict = new TreeSet();
        Set file = new HashSet();
        //Prompts user for the dictionary.txt file
        JFileChooser dictionary = new JFileChooser();
        JFileChooser wordFiles = new JFileChooser();
        int returnVal = dictionary.showOpenDialog(this);
         if (returnVal == JFileChooser.APPROVE_OPTION) {
         storeDictionary(dictionary.getSelectedFile());
        int returnVal2 = wordFiles.showOpenDialog(this);
            if (returnVal2 == JFileChooser.APPROVE_OPTION){
                storeFile(wordFiles.getSelectedFile());
        public void storeDictionary(File dictionaryFile){
            String dictionary="";
         Scanner scanner=null;
        try {
           // Delimiters specifiy where to parse tokens in a scanner
           scanner = new Scanner(dictionaryFile).useDelimiter("\\s*[\\p{Punct}*\\s+]\\s*");
        catch (FileNotFoundException fnfe) {
            JOptionPane.showMessageDialog(this,"Could not open the file");
           System.exit(-1);
         while (scanner.hasNext()) {
              if (!dictionary.equals(""))
                 dict.add(scanner.next());
            System.out.println("Thank you, your dictionary is stored.");
        public void storeFile(File wordFile){
            String word="";
            Scanner scanner2=null;
        try{
            scanner2 = new Scanner(wordFile).useDelimiter("\\s*[\\p{Punct}*\\s+]\\s*");
        catch (FileNotFoundException fnfe){
            JOptionPane.showMessageDialog(this,"Could not open the file");
            System.exit(-1);
            while(scanner2.hasNext()){
                if(!word.equals(""))
                    file.add(scanner2.next());
        public static void main(String args[]){
            spell1 spellck = new spell1();
            spellck.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

  • Opening a text file in a bundle with C++

    Hi folks! I'm working with bundles for the first time, and I'd like to know how to open a text file inside one with C++. What I have now is this:
    ifstream file = new ifstream(fileName.c_str());
    if (!file->is_open())
    return false;
    Where fileName is a std::string. This is the way I'm accustomed to opening files (I'm from a Windows development background -- don't hate me! ). Unfortunately, the is_open() test always fails.
    I know how to get the path of the bundle. Once I add on my data subdirectory and file name, the fileName variable ends up as this:
    /Users/sb/...project location.../Debug/MyProg.app/data/Dev Options.ini
    Should the path be different somehow? Thanks for any help!

    Your data folder most likely isn't directly inside the .app bundle. The first directory inside an .app bundle is named Contents. The article at the following URL should help you:
    http://www.meandmark.com/bundlespart1.html
    If you're going to use C++ streams to open files, you'll want to ignore the section on opening the file in Part 3 of the article. You'll want to read up on the Core Foundation CFURL functions. You'll want to use one of the functions that gives you a path to the text file that you can pass to a C++ file stream.

  • How do i open an Appleworks file now that I am running Mavericks and Pages?

    How do I open an Appleworks file now that I am running Mavericks and Pages?

    Word Processing documents can be opened directly in Pages 4 (AppleWorks 6 only) but this is no longer available - the newPages 5 (Mavericks required) does not open AppleWorks documents. Panergy Software's docXConverter v3.2 ($19.95) can convert Appleworks 5 and 6 Word Processing documents to RTF (though it has been reported that it can only handle documents which contain only text, not those which include images or frames). The latest version of the free LibreOffice has announced that it can open AppleWorks 6 Word Processing documents and an ability to open ClarisWorks documents has been reported: it does appear to be able to handle at least some embedded images.
    Draw Documents can be opened directly only in EazyDraw (the more expensive version from their website, not the cheaper one from the Mac App Store, and you will need v4.0 'Retro' not the most recent version) or Intaglio (though this appears not to be able to handle very complex drawings). LibreOffice will open simple Draw documents but I found it very unreliable with complex ones.
    Spreadsheets can be opened directly only in the older versions of  Numbers - the new version for Mavericks will not do this. The old version is no longer available.
    Presentations can be opened directly only in the older versions Keynote; again, the new version for Mavericks will not do this. The old version is no longer available.
    To use other programs, or open Database documents at all, you must export from AppleWorks first, which means you need a way to run it.
    If you have upgraded to Lion/Mountain Lion/Mavericks and have AppleWorks documents you cannot open, then you should be able to install Snow Leopard on an external hard disk and start from that (provided that the Mac did not come with anything higher than Snow Leopard originally installed); and success has been reported in running Snow Leopard Server under emulation in Parallels - the method is described here: Snow Leopard Server is available in the Apple Store (by telephone) at a reduced price (at the time of writing) and is preferable to using ordinary Snow Leopard which is quite tricky. I haven't tried this myself: use at your own discretion. You can then run AppleWorks as an emergency measure.

  • How can I open and edit files that were developed and published by ftp to my host on my old macbook, which I can no longer access.

    How can I open and edit files that were developed and published via ftp to my host on my old macbook, which I can no longer access. The HD containing the files cannot be accessed. I want to be able to download the published files to be edited in iweb?

    You CAN'T download published files and then edit them in iWeb because iWeb can't open them - it has no import facility so is unable to open any html or css files - a previously published site.
    You can download these files yes, but you'll need to use them as a guide or to cut and paste text from there whilst you re-build your site in iWeb.
    The only other way is to download the files and then open them with an html editor such as TextWrangler or use one of the other web design programmes out there that is capable of importing html and css such as Dreamweaver of Flux 4.
    These are your only alternatives as iWeb can't import and if you want to continue to use iWeb, then you'll have to re-build your site from scratch.  If you do just remember to back up your domain.sites file this time around - it is found under your User/Library/Applicaion Support/iWeb/domain.sites.

  • Can Quickword open a text file 10K?

    I have a couple of simple, notepad text files.
    6K opens (after about 5 seconds)
    90K, well, it doesn't
    480K, neither does this.
    I can open the files using notepad, direct from the phone.
    Can anyone else open simple text files of this range of size.
    ...Lyall

    Well, I have a Tab Separated variable file (output from Excel) with about 20 columns.
    I edited it down from 5000 lines to 1000, 500 and 250 lines.
    I then copied all 4 files to the E90.
    QuickWord will open the 250 line file after about 5-10 seconds.
    It does NOT open (at least within my lifetime) the 500+ line files.
    So, how do I look at text files on the E90?
    ...Lyall

  • How do I get a text file from Photoshop  to work in the main sequence in pp?

    How do I get a text file to work properly in the master sequence. I moved it from Photoshop, which I learned to do from a tutorial, but when I move the animated text sequence to the master, it either isnt running, or it is scaled way too big. How do I get it to run in the main sequence?

    "Wont Work Here" !  Does not mean much.
    Are you having an audio or a video issue? 
    Looks like no video clip on the video layer above that section of audio.
    I am teaching myself this stuff completely on the fly
    I suggest you do the Basic Tutorials ( Adobe TV for example) in both Premiere Pro and PhotoShop.
    You need to be competent in the basics and fundamentals of these apps and that will also help you describe and discuss the issues.   Check the 'Products on this site....
    Adobe TV

  • How can I open a cc2014 file in my cs6 version of Indesign ?

    How can I open a cc2014 file in my cs6 version of Indesign ?
    I have several files that has been saved in a cc2014 version and I can't open them in my CS6 version of Indesign.
    I don t have access to anyone who have the cc 2014 version to convert the file.
    How can I do it from my cs6 version?
    THANKS!

    You can't do it from CS6.
    You have to export to IDML from InDesign CC2014 (or whatever the version it was created in) and then open the IDML in the earlier versoin of InDesign.
    All explained in the FAQ on the main forum page.

  • How to read a whole text file into a pl/sql variable?

    Hi, I need to read an entire text file--which actually contains an email message extracted from a content management system-- into a variable in a pl/sql package, so I can insert some information from the database and then send the email. I want to read the whole text file in one shot, not just one line at a time. Shoud I use Utl_File.Get_Raw or is there another more appropriate way to do this?

    how to read a whole text file into a pl/sql variable?
    your_clob_variable := dbms_xslprocessor.read2clob('YOUR_DIRECTORY','YOUR_FILE');
    ....

Maybe you are looking for