Results 1 to 14 of 14

Thread: Sorting Problem

  1. #1
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Sorting Problem

    I am trying to sort a set of parallel arrays. I really believe that the code is correct, but it is not working out as expected.
    This is the specific code for the sort:
    Java Code:
    for (int y = 1; y < (dataArray.length + 1); y++)
          {
            for (int x = 0; x < dataArray.length - 1 ; x++)
            {
              if ((dataArray[x][1]) <= (dataArray[x + 1][1]));
              {
                tempOpen = dataArray[x][1];
                dataArray[x][1] = dataArray[x + 1][1];
                dataArray[x + 1][1] = tempOpen;
                
                tempDate = dateArray[x];
                dateArray[x] = dateArray [x + 1];
                dateArray [x + 1] = tempDate;
              }
            }
          }
    The entire program is here:
    Java Code:
    public class DataAnalyzer
    {
      public static void main (String[] args)
      {
        //This creates an instance of ReadFiles
        //ReadFiles aReadFiles = new ReadFiles();
        Calculations aCalculations = new Calculations();
        
        //This calls the method fileCheck in the ReadFiles Class
        aCalculations.fileCheck();
        
        //This calls the method findRows in the ReadFiles Class
        aCalculations.findRows();
        
        //This calls the method printArray in the ReadFiles Class
        aCalculations.convertFile();
        
        //This calls the method convertFile in the ReadFiles Class through the Calculations Class
        //aCalculations.printArray();
        
        //This calls the method createDateArray in the Calculations Class    
        aCalculations.createDateArray();
        
        //This calls the method createDataArray in the Calculations Class
        aCalculations.createDataArray();
        
        //This calls the method printArray in the Calculations Class
        //aCalculations.printNewArray();
        
        //This calls the method performCalculations in the Calculations Class
        aCalculations.performCalculations();
      }
    }
    Java Code:
    import java.nio.file.*;
    import static java.nio.file.AccessMode.*;
    import java.io.IOException;
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.StringTokenizer;
    
    
    public class ReadFiles
    {
      public static int numberOfRows;
      public static int rowNumber = 0;
      public static int columnNumber = 0;
      public static int newRowNumber = 0;
      public static int newColumnNumber = 0;
      public String [][] myFBfile = new String [431][7];
    
      //This method attempts to read a file to ensure that it is available to be used by the rest of the program.
      public void fileCheck()
      {
        Path filePath = Paths.get("C:\\Users\\Brad\\Downloads\\FB.csv");
        System.out.println("Path is " + filePath.toString());
        
        try
        {
          filePath.getFileSystem().provider().checkAccess(filePath, READ, EXECUTE);
          System.out.println("File can be read and executed");
        }
        
        catch (IOException e)
        {
          System.out.println ("File cannot be used for this application");
        }
      }
      
      //This method reads a file, then tokenizes it to find the number of rows in the file.
      public void findRows()
      {
        String filePath = ("C:\\Users\\Brad\\Downloads\\FB.csv");
        
        try
        {
          BufferedReader br = new BufferedReader(new FileReader(filePath));
          StringTokenizer st = null;
          System.out.println("Your file is being analysed. This may take several minutes.");
    
          while((filePath = br.readLine()) != null)
          {
            rowNumber++;
                    
            st = new StringTokenizer(filePath, ",");
            columnNumber = 0;
          }
        }
        
        catch (FileNotFoundException e)
        {
          System.out.println ("Number of rows for this file cannot be found");
        } 
        
        catch (IOException e) 
        {
          System.out.println ("Number of rows for this file cannot be found");
        }
            
        System.out.println("Total Rows: " + (rowNumber));
      }
      
      //This method converts the file into an array to be used by the rest of the program.
      public void convertFile()
      {
        String convertFilePath = ("C:\\Users\\Brad\\Downloads\\FB.csv");
        //The fields for the file are: Date,Open,High,Low,Close,Volume,Adj Close
        try
        {
          BufferedReader br = new BufferedReader(new FileReader(convertFilePath));
          StringTokenizer st = null;
          System.out.println("Your file is being converted. This may take several minutes.");
    
          while((convertFilePath = br.readLine()) != null)
          {
            st = new StringTokenizer(convertFilePath, ",");
    
            while(st.hasMoreTokens())
            {
              myFBfile[newRowNumber][newColumnNumber] = st.nextToken();
              newColumnNumber++;          
              //this is a test to make sure the array is working properly
              //System.out.println (newRowNumber +" "+ newColumnNumber +" "+ myFBfile[newRowNumber][newColumnNumber]);
            }
            newRowNumber++;
            newColumnNumber = 0;
          }
        }       
        catch (FileNotFoundException e) 
        {
          System.out.println("Encountered critical error. Your file has not been converted");
        } 
        catch (IOException e) 
        {
          System.out.println("Encountered critical error. Your file has not been converted");
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
          System.out.println ("Your file exceeds the available size limit.");
        }
        
      }
      
      //This method prints out all of the data from the array
      public void printArray()
      {
        for (int x = 0; x <= rowNumber; x++)
        {
          for(int y = 0; y <= 6; y++)
          {
            System.out.print (x +" "+ y +" "+ myFBfile[x][y]);
          }
          System.out.println();
        }
      }
      
      //This method returns the array
      public void returnArray()
      {
        //return myFBfile;
      }
    }
    Java Code:
    import java.io.FileWriter;
    import java.io.PrintWriter;
    
    
    public class Calculations extends ReadFiles
    {
      public String []dateArray = new String [myFBfile.length];
      public float [][]dataArray = new float[myFBfile.length][7];
      public float highestOpen = 0;
      public float highestClose = 0;
      public float lowestOpen = 100000000;
      public float lowestClose = 100000000;
      public float []highValue = new float[10];
      public float []lowValue = new float[10];
      public float []differenceArray = new float[myFBfile.length];
      public float difference;
      public String record;
      public String tempDate;
      public float tempOpen;
      
    
      public void createDateArray()
      {     
        System.out.println(myFBfile.length);
        try
        {
          System.out.println("Your file is being read to find dates. This may take several minutes.");
          for (int x = 1; x < myFBfile.length; x++)
          {
            dateArray[x] = myFBfile[x][0];
            //this is a test to make sure the array is working properly
            //System.out.println (dateArray[x]);
          }
        }
        
        catch (ArrayIndexOutOfBoundsException e)
        {
          System.out.println ("Your date array cannot be created. Your file exceeds the available size limit.");
        }
      }
      
      public void createDataArray()
      {
        try
        {
          System.out.println("Your file is being read to organize information for processing. This may take several minutes.");
          for (int x = 0; x < myFBfile.length; x++)
          {
            for (int y = 1; y <= 6; y++)
            {
              if (myFBfile[x][y] != null)
              {
                dataArray[x][y] = Float.parseFloat(myFBfile[x][y]);
                //this is a test to make sure the array is working properly
                //System.out.println (dataArray[x][y]);
              }
              else
              {
                dataArray[x][y] = 0;
              }
            }
          }
        }
        
        catch (ArrayIndexOutOfBoundsException e)
        {
          System.out.println ("The data array has not been created. Your file exceeds the available size limit.");
        }
      }
     
      public void printNewArray()
      {
        try
        {
          for (int x = 0; x < myFBfile.length; x++)
          {
            System.out.print(dateArray[x]);
            for (int y = 1; y <= 6; y++)
            {
              System.out.print (" " + dataArray[x][y]);
            }
            System.out.println();
          }
        }
        
        catch (ArrayIndexOutOfBoundsException e)
        {
          System.out.println ("Your array cannot be printed. Your file exceeds the available size limit.");
        }
      }
      
      public void performCalculations()
      {
        //try
        //{
          //find the highest opening value
          for (int x = 1; x < dataArray.length; x++)
          {
            if (highestOpen <= dataArray[x][1])
            {
              highestOpen = dataArray[x][1];
            }
          }
          System.out.println ("The highest opening value was " + highestOpen);
        
          //find the highest closing value
          for(int x = 1; x < dataArray.length; x++)
          {
            if (highestClose <= dataArray[x][4])
            {
              highestClose = dataArray[x][4];
            }
          }
          System.out.println ("The highest closing value was " + highestClose);
                
          //find the lowest opening value
          for(int x = 1; x < dataArray.length; x++)
          {
            if (dataArray[x][1] != 0)
            {
              if (lowestOpen > dataArray[x][1])
              {
                lowestOpen = dataArray[x][1];
              }
            }
            else
            {
            }
          }
          System.out.println ("The lowest opening value was " + lowestOpen);
                
          //find the lowest closing value
          for(int x = 1; x < dataArray.length; x++)
          {
            if (dataArray[x][4] != 0)
            {
              if (lowestClose > dataArray[x][4])
              {
                lowestClose = dataArray[x][1];
              }
            }
            else
            {
            }
          }
          System.out.println ("The lowest closing value was " + lowestClose);
        
          //first, let's sort the entire array
          for (int y = 1; y < (dataArray.length + 1); y++)
          {
            for (int x = 0; x < dataArray.length - 1 ; x++)
            {
              if ((dataArray[x][1]) <= (dataArray[x + 1][1]));
              {
                tempOpen = dataArray[x][1];
                dataArray[x][1] = dataArray[x + 1][1];
                dataArray[x + 1][1] = tempOpen;
                
                tempDate = dateArray[x];
                dateArray[x] = dateArray [x + 1];
                dateArray [x + 1] = tempDate;
              }
            }
          }
          //for (int x = 0; x < dataArray.length; x++)
          //{
          //  System.out.println (dataArray [x][1]);
          //}
    
          //find the top ten highest values
          for (int x = 0; x < 10; x++)
          {
            System.out.println (dateArray[x] + " - " + dataArray[x][1]);
          }
          
        
          //find the top ten lowest values
          
          
        /*
          //create a file for the difference in opening and closing values
          FileWriter outFile = new FileWriter ("C:\\Users\\Brad\\Downloads\\NewFBFile.txt");
          PrintWriter out = new PrintWriter(outFile);
          
          for (int x = 0; x < 431; x++)
          {
            
            difference = Math.abs(dataArray[x][1] - dataArray[x][4]);
            differenceArray[x] = difference;
            
            //this is a test to see results and if the formula is working.
            //System.out.println("the difference between " + dataArray[x][1] + " and " + dataArray[x+1][4] + " is " + difference);
            //RESULT: kind of working....I think
    
            record = (dateArray[x] + " " + dataArray[x][1] + " " + dataArray[x][2]
                        + " " + dataArray[x][3] + " " + dataArray[x][4] + " " + dataArray[x][5] + " " + dataArray[x][6]
                        + " " + difference + System.getProperty("line.separator"));
            //this is a test to see if the output is working
            System.out.println (record);
            //out.println(record); 
            
          }
          out.close();
        }*/
        }
        //catch (ArrayIndexOutOfBoundsException e)
        //{
        //  System.out.println ("Calculations cannot be performed. Your file exceeds the available size limit.");
        //}
      
      //}
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: Sorting Problem

    it is not working out as expected.
    Please explain what the code is doing, perhaps post some output and say what the unexpected part is.

    You've posted a lot more code than needs to be posted for the problem. It should be stripped down to a couple of arrays and the code to sort those arrays. Everything else is just in the way.
    Make a small, simple, complete program that compiles, executes and shows the problem and post that.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Cg33k is offline Member
    Join Date
    Mar 2014
    Location
    Leb
    Posts
    9
    Rep Power
    0

    Default Re: Sorting Problem

    Quote Originally Posted by shodai View Post
    I am trying to sort a set of parallel arrays. I really believe that the code is correct, but it is not working out as expected.
    This is the specific code for the sort:
    Java Code:
    for (int y = 1; y < (dataArray.length + 1); y++)
          {
            for (int x = 0; x < dataArray.length - 1 ; x++)
            {
              if ((dataArray[x][1]) <= (dataArray[x + 1][1]));
              {
                tempOpen = dataArray[x][1];
                dataArray[x][1] = dataArray[x + 1][1];
                dataArray[x + 1][1] = tempOpen;
                
                tempDate = dateArray[x];
                dateArray[x] = dateArray [x + 1];
                dateArray [x + 1] = tempDate;
              }
            }
          }

    If you're only working on the x subscript why do you need the y subscript for?
    Althugh incomplete but I can see a logic problem here; basically you are looping y times and hence overiding what you had to do in a single iteration.
    Your code will basically keep shifting the data to the right y times...

  4. #4
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Sorting Problem

    Quote Originally Posted by Cg33k View Post
    If you're only working on the x subscript why do you need the y subscript for?
    Althugh incomplete but I can see a logic problem here; basically you are looping y times and hence overiding what you had to do in a single iteration.
    Your code will basically keep shifting the data to the right y times...
    So I removed the extra loop, and there is a slight improvement. I am trying to sort a file into ascending order. The output that I currently get is as follows:

    02/03/14 - 63.03
    01/31/14 - 60.47
    01/30/14 - 62.12
    01/29/14 - 54.61
    01/28/14 - 54.02
    01/27/14 - 54.73
    01/24/14 - 56.15
    01/23/14 - 56.37
    01/22/14 - 58.85
    01/21/14 - 56.6

    The first value is correct, but the following values just seem to follow the date as they appear in the original file.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: Sorting Problem

    Can you make a small simple program that compiles, executes and shows the problem? Two arrays and the sort method.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Cg33k is offline Member
    Join Date
    Mar 2014
    Location
    Leb
    Posts
    9
    Rep Power
    0

    Default Re: Sorting Problem

    Quote Originally Posted by shodai View Post
    So I removed the extra loop, and there is a slight improvement. I am trying to sort a file into ascending order. The output that I currently get is as follows:

    02/03/14 - 63.03
    01/31/14 - 60.47
    01/30/14 - 62.12
    01/29/14 - 54.61
    01/28/14 - 54.02
    01/27/14 - 54.73
    01/24/14 - 56.15
    01/23/14 - 56.37
    01/22/14 - 58.85
    01/21/14 - 56.6

    The first value is correct, but the following values just seem to follow the date as they appear in the original file.
    What does your dataArray hold?

  7. #7
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Sorting Problem

    Ok, so I have put something small together that is supposed to sort numbers from low to high; it is does not work. The dates array is sorted in parallel with the testNums array, so the only thing that is compared in sorting is the testNums values. I have tried it with the first for loop and without the first for loop and the output is the same.
    Java Code:
    public class SortTest
    {
      public static void main (String args[])
      {
        int[] dates = new int [10];
        int[] testNums = new int [10];
        int tempOpen;
        int tempDate;
        
        dates[0] = 5;
        dates[1] = 7;
        dates[2] = 1;
        dates[3] = 16;
        dates[4] = 4;
        dates[5] = 12;
        dates[6] = 11;
        dates[7] = 3;
        dates[8] = 9;
        dates[9] = 10;
        
        testNums[0] = 22;
        testNums[1] = 54;
        testNums[2] = 12;
        testNums[3] = 2;
        testNums[4] = 18;
        testNums[5] = 7;
        testNums[6] = 42;
        testNums[7] = 1;
        testNums[8] = 21;
        testNums[9] = 14;
        
        for (int y = 0; y < (testNums.length + 1); y++)
        {
          for (int x = 0; x < testNums.length - 1 ; x++)
          {
            if ((testNums[x]) <= (testNums[x + 1]));
            {
              tempOpen = testNums[x];
              testNums[x] = testNums[x + 1];
              testNums[x + 1] = tempOpen;
                
              tempDate = dates[x];
              dates[x] = dates [x + 1];
              dates [x + 1] = tempDate;
            }
          }
        }
    
        for (int j= 0; j < testNums.length; j++)
        {
          System.out.println (dates[j] + " - " + testNums[j]);
        }
      }
    }
    It outputs the following results

    7 - 54
    1 - 12
    16 - 2
    4 - 18
    12 - 7
    11 - 42
    3 - 1
    9 - 21
    10 - 14
    5 - 22

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: Sorting Problem

    What are the expected results? Neither column appears to be sorted.

    Hint: To load arrays for testing:
    Java Code:
    int[] dates = {3, 6, 1, 4};
    int[] temp =  {22, 44, 11, 33};
    To print out the contents:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    Last edited by Norm; 03-31-2014 at 01:41 AM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Sorting Problem

    Well, as you signature says; I do not understand. I get the method of loading variables into an array, but that really does not make a difference in the error of my logic.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: Sorting Problem

    What don't you understand about my post?

    There were 3 items:
    1)output not sorted
    2)how to load an array for testing
    3)how to print the contents of an array

    I get compiler warnings when I compile the code. You should use the -Xlint compiler option so you get them.
    The commandline I use:
    C:\Program Files\Java\jdk1.7.0_25\bin\javac.exe -Xlint -cp . SortTest1.java
    Last edited by Norm; 03-31-2014 at 01:58 AM.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Sorting Problem

    It would have to be with the sorting of the array is what does not make sense. The loading makes sense (much for efficient than what I did). The output makes sense, but I have to ask why you convert to String?

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: Sorting Problem

    why you convert to String?
    Is that about printing the contents of an array? The posted code converts an array's contents to a String so it can be printed.


    I get compiler warnings when I compile the posted code. You should use the -Xlint compiler option so you get them.
    The commandline I use:
    C:\Program Files\Java\jdk1.7.0_25\bin\javac.exe -Xlint -cp . SortTest1.java
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Sorting Problem

    Quote Originally Posted by Norm View Post
    Is that about printing the contents of an array? The posted code converts an array's contents to a String so it can be printed.


    I get compiler warnings when I compile the posted code. You should use the -Xlint compiler option so you get them.
    The commandline I use:
    C:\Program Files\Java\jdk1.7.0_25\bin\javac.exe -Xlint -cp . SortTest1.java
    Ok, you have completely lost me here; it might as well be latin. The printing of the array is fine, but I have no clue about the rest. I am using a compiler so I do not know where anything for a command line would go. What is -Xlint?

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: Sorting Problem

    The javac.exe program is the java compiler. In most OSs a commandline is built when a command is executed. On the command line goes the filename of the program followed by its options.
    See this for the javac command: http://docs.oracle.com/javase/7/docs...ows/javac.html

    Most IDEs hide this from you. You need to go into the IDE's settings and turn on warnings so you see them. Don't let the IDE ignore and not display warnings about the java source program.
    Last edited by Norm; 03-31-2014 at 02:18 AM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Sorting Problem
    By Moorag in forum New To Java
    Replies: 2
    Last Post: 03-12-2011, 12:58 AM
  2. Sorting Problem
    By Freakzoyd in forum New To Java
    Replies: 5
    Last Post: 11-30-2010, 05:40 AM
  3. sorting problem
    By vasug in forum Advanced Java
    Replies: 2
    Last Post: 02-25-2010, 04:55 AM
  4. sorting problem...
    By mark-mlt in forum New To Java
    Replies: 4
    Last Post: 04-17-2008, 02:15 PM
  5. sorting problem
    By mcal in forum New To Java
    Replies: 1
    Last Post: 02-14-2008, 08:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •