need help figure out the problem of the program
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: need help figure out the problem of the program

  1. #1
    Join Date
    Oct 2009
    Posts
    3

    need help figure out the problem of the program

    here's my project i'm working on now i have confirmed everything with my partner but the thing is we can get a print out correctly of the sortRow so i would really appricication the help if someone can help us figure this out

    thank you heres' the code

    import javax.swing.*;
    import java.util.*;

    /***************************************************************
    *
    * @author S. Singh
    * A program that works with TextFileInput, String Tokenizer,
    * and a two-dimension array.
    *
    */

    public class Project1 {

    public static int[][] intArray;// declare a new 2 dimension array with type
    // int
    public static int row, column;
    /*
    * We need to know the row and column of the
    * 2-dimension array and declare two variables
    * with type int
    */
    /**
    *
    * @param args
    */

    public static void main(String[] args) {
    readFile("project1.txt");

    JOptionPane.showMessageDialog(null, "Row 0 min value: " + findMin(0)
    + "\n Row 1 min value: " + findMin(1) + "\n Row 2 min value: "
    + findMin(2) + "\n Row 3 min value: " + findMin(3)
    + "\n Row 4 min value: " + findMin(4));// JOptionPane window
    // displays the minimum value in each row of the 2-D array using
    // the method findMin()

    JOptionPane.showMessageDialog(null, "Even numbers in the array are: "
    + displayEven(intArray));// displays the even numbers in the
    // array through the method displayEven

    JOptionPane.showMessageDialog(null, "Sum of all numbers is: "
    + getSum());// returns the sum of the 2-d array in the
    // JOptionPane


    JOptionPane.showMessageDialog(null, "After sorting each row: \n"
    + print(intArray) + " ");
    }

    private static void readFile(String myFile) {
    /* this method reads from the file,
    * and populates the array
    * it also makes use of stringTokenizer to break a string into
    * tokens
    */

    TextFileInput in = new TextFileInput(myFile);

    String line = in.readLine();

    StringTokenizer t = new StringTokenizer(line, ",");

    row = Integer.parseInt(t.nextToken());
    column = Integer.parseInt(t.nextToken());

    intArray = new int[row][column];

    for (int i = 0; i < row; i++) {
    line = in.readLine();
    t = new StringTokenizer(line, ",");
    for (int j = 0; j < column; j++) {
    intArray[i][j] = Integer.parseInt(t.nextToken());
    }
    }
    }

    private static int findMin(int row) {
    /* method findMin will display the minimum values
    * in each row of the two dimensional array
    */

    int min = intArray[row][0];
    for (int i = 1; i < intArray.length; i++) {
    if (intArray[row][i] < min)
    min = intArray[row][i];
    }

    return min;

    }

    private static String displayEven(int[][] theArray) {
    /*
    * method displayEven will display only the even numbers of the array.
    * To do this, we need only set a condition such as "Whether the
    * remainder is zero" to control the output.
    */
    int[] evenArray = new int[100];
    String even = "";
    int evcounter = 0;
    for (int i = 0; i < theArray.length; i++) {

    for (int j = 0; j < theArray[i].length; j++) {
    if (theArray[i][j] % 2 == 0) {
    evenArray[evcounter] = theArray[i][j];
    evcounter++;
    }

    }

    }
    for (int i = 0; i < evcounter; i++) {
    even += evenArray[i] + " ";

    }
    return even;
    }

    private static int getSum() {
    /*
    * method getSum will find the sum of the array.
    */
    int sum = 0;
    for (int i = 0; i < row; i++) {
    for (int j = 0; j < column; j++) {
    sum += intArray[i][j];
    }

    }
    return sum;
    }

    private static void sortRow(int row) {
    /*
    * method sortRow will Sort all of the rows of the array,
    * to do this we must sort by selection, and set a condition
    * where the 1st number is already the lowest.
    */

    for (int i = 0; i < column; i++) {
    int min = intArray[row][i];
    int index = -1;
    for (int j = i; j < column; j++) {
    if (intArray[row][j] < min) {
    min = intArray[row][j];
    index = j;
    }
    }
    if (index != -1) {
    int temp = intArray[row][i];
    intArray[row][i] = intArray[row][index];
    intArray[row][index] = temp;
    }
    }

    }//Main
    private static String print(int[][] theArray) {
    String result="";
    for(int i=0;i<row;i++) {
    for (int j=0; j<column; j++) {
    result += intArray[i][j];
    result += " ";
    }
    result +="\n";

    }
    return result;
    }

    }//class Project1

  2. #2
    Join Date
    Oct 2009
    Posts
    3

    Re: need help figure out the problem of the program

    thank you here' the code resubmitted the program in the code box
    Code:
    import javax.swing.*;
    import java.util.*;
    
    /***************************************************************
    *
    * @author S. Singh
    * A program that works with TextFileInput, String Tokenizer,
    * and a two-dimension array.
    *
    */
    
    public class Project1 {
    
        public static int[][] intArray;// declare a new 2 dimension array with type
        // int
        public static int row, column;
    /*
                                     * We need to know the row and column of the
                                     * 2-dimension array and declare two variables
                                     * with type int
                                     */
        /**
         *
         * @param args
         */
    
        public static void main(String[] args) {
            readFile("project1.txt");
    
            JOptionPane.showMessageDialog(null, "Row 0 min value: " + findMin(0)
                    + "\n Row 1 min value: " + findMin(1) + "\n Row 2 min value: "
                    + findMin(2) + "\n Row 3 min value: " + findMin(3)
                    + "\n Row 4 min value: " + findMin(4));// JOptionPane window
            // displays the minimum value in each row of the 2-D array using
            // the method findMin()
    
            JOptionPane.showMessageDialog(null, "Even numbers in the array are: "
                    + displayEven(intArray));// displays the even numbers in the
            // array through the method displayEven
    
            JOptionPane.showMessageDialog(null, "Sum of all numbers is: "
                    + getSum());// returns the sum of the 2-d array in the
            // JOptionPane
    
             
            JOptionPane.showMessageDialog(null, "After sorting each row: \n"
                            + print(intArray) + " ");
        }
       
        private static void readFile(String myFile) {
            /* this method reads from the file,
             * and populates the array
             * it also makes use of stringTokenizer to break a string into
             * tokens
             */
    
            TextFileInput in = new TextFileInput(myFile);
    
            String line = in.readLine();
    
            StringTokenizer t = new StringTokenizer(line, ",");
    
            row = Integer.parseInt(t.nextToken());
            column = Integer.parseInt(t.nextToken());
    
            intArray = new int[row][column];
    
            for (int i = 0; i < row; i++) {
                line = in.readLine();
                t = new StringTokenizer(line, ",");
                for (int j = 0; j < column; j++) {
                    intArray[i][j] = Integer.parseInt(t.nextToken());
                }
            }
        }
    
        private static int findMin(int row) {
            /* method findMin will display the minimum values
             * in each row of the two dimensional array
             */
    
            int min = intArray[row][0];
            for (int i = 1; i < intArray.length; i++) {
                if (intArray[row][i] < min)
                    min = intArray[row][i];
            }
    
            return min;
    
        }
    
        private static String displayEven(int[][] theArray) {
            /*
             * method displayEven will display only the even numbers of the array.
             * To do this, we need only set a condition such as "Whether the
             * remainder is zero" to control the output.
             */
            int[] evenArray = new int[100];
            String even = "";
            int evcounter = 0;
            for (int i = 0; i < theArray.length; i++) {
    
                for (int j = 0; j < theArray[i].length; j++) {
                    if (theArray[i][j] % 2 == 0) {
                        evenArray[evcounter] = theArray[i][j];
                        evcounter++;
                    }
    
                }
    
            }
            for (int i = 0; i < evcounter; i++) {
                even += evenArray[i] + " ";
    
            }
            return even;
        }
    
        private static int getSum() {
            /*
             * method getSum will find the sum of the array.
             */
            int sum = 0;
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < column; j++) {
                    sum += intArray[i][j];
                }
    
            }
            return sum;
        }
    
        private static void sortRow(int row) {
            /*
             * method sortRow will Sort all of the rows of the array,
             * to do this we must sort by selection, and set a condition
             * where the 1st number is already the lowest.
             */
    
            for (int i = 0; i < column; i++) {
                int min = intArray[row][i];
                int index = -1;
                for (int j = i; j < column; j++) {
                    if (intArray[row][j] < min) {
                        min = intArray[row][j];
                        index = j;
                    }
                }
                if (index != -1) {
                    int temp = intArray[row][i];
                    intArray[row][i] = intArray[row][index];
                    intArray[row][index] = temp;
                }
            }
    
        }//Main
        private static String print(int[][] theArray) {
            String result="";
            for(int i=0;i<row;i++) {
                for (int j=0; j<column; j++) {
                    result += intArray[i][j];
                    result += " ";
                }
                result +="\n";
               
            }
            return result;
        }
           
        }//class Project1

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: need help figure out the problem of the program

    So what is your question? There's no point posting up a bunch of code unless you explain what it is supposed to do, what should happen that isn't happening, or what is happening that shouldn't happen. If you get errors, post up the full error message plus stack trace if present.

    A prudent question is one-half of wisdom...
    F. Bacon
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center