5 Replies - 293 Views - Last Post: 16 August 2012 - 05:42 AM Rate Topic: -----

#1 RCharles  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 12-August 12

how to get the return value from another function?

Posted 16 August 2012 - 01:42 AM

Im doing a selection sort code in netbeans. But I do not know how can i use the return of 1 function to be used to another.

   private int[] Splitter(String unSortedElements){
           //Use split method to put the String into String array separated with " " delimeter
           String[] stringParts = unSortedElements.split(" ");
           
           //Convert String array to int array
           int[] inputs = new int[stringParts.length];
           for (int i=0; i < stringParts.length; i++) {
               inputs[i] = Integer.parseInt(stringParts[i]);
               //System.out.println(inputs[i]);
           }
           return inputs;
    }

   private int[] doSelectionSort(int[] unSortedElements){
        int i,j,minIndex, tmp;
        int n = unSortedElements.length;
        int arrselection[] = Splitter(inputsjTextField.toString());
        
        
           for(i=0; i<n; i++){
               minIndex = i;
               for(j=i+1; j<n; j++ ){
                   if(unSortedElements[j] < unSortedElements[minIndex]){
                        minIndex = j;
                   }
                   if(minIndex != i){
                       tmp = unSortedElements[i];
                       unSortedElements[i] = unSortedElements[minIndex];
                       unSortedElements[minIndex] = tmp;
                   }
                       
               }
           }
           return arrselection;
    }



I am stuck here:

   private void executejButtonActionPerformed(java.awt.event.ActionEvent evt) {
              int arr4[] = doSelectionSort();
              sortedjTextField.setText(Arrays.toString(arr4));



What parameter should i pass inside the doSelectionSort??

Is This A Good Question/Topic? 0
  • +

Replies To: how to get the return value from another function?

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: how to get the return value from another function?

Posted 16 August 2012 - 02:39 AM

Look at what the definition says:
private int[] doSelectionSort(int[] unSortedElements){

It tells you to pass unsorted elements in an array of integers. So you will have your array of unsorted integers which you want to sort, then pass it and a sorted one will be returned.

But I see your doSelectionSort a little confusing. Why are you passing values which you then take it from a text field.
 int arrselection[] = Splitter(inputsjTextField.toString());

Looks like that is the one required to be your unSortedElements, because you didnt do anything with your arrselection array and just return it.
Also inputsjTextField.toString() looks like it was to be inputsjTextField.getText() If inputsjTextField is a text field.

So probably the value returned by Splitter() is the one you need to sort there, so you have just confused yourself by having arrselection.

This post has been edited by smohd: 16 August 2012 - 02:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 RCharles  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 12-August 12

Re: how to get the return value from another function?

Posted 16 August 2012 - 02:53 AM

I tried doing this in execute button:

private void executejButtonActionPerformed(java.awt.event.ActionEvent evt) {
    int arr4[] = splitter(inputsjTextField.getText());
    doSelectionSort(arr4);
    sortedjTextField.setText(Arrays.toString(arr4));
}


but i got error: java.lang.NumberFormatException: For input string: "javax.swing.JTextField
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: how to get the return value from another function?

Posted 16 August 2012 - 03:15 AM

No, that error you were getting when you said: inputsjTextField.toString().
Also arr4 is not sorted, the sorted one is the returned array from doSelectionSort(). Again that method wont give you the desired output if you didnt take into consideration what I have told you before.
Do changes which I have told you then post your updated code with your error message if you are still getting any.
Was This Post Helpful? 1
  • +
  • -

#5 RCharles  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 12-August 12

Re: how to get the return value from another function?

Posted 16 August 2012 - 05:35 AM

fixed now..

by changes:

    private int[] doSelectionSort(String unSortedElements){
        int i,j,minIndex, tmp;
        int arrselection[] = splitter(unSortedElements);
        int n = arrselection.length;
        
        
           for(i=0; i<n; i++){
               minIndex = i;
               for(j=i+1; j<n; j++ ){
                   if(arrselection[j] < arrselection[minIndex]){
                        minIndex = j;
                   }
                   if(minIndex != i){
                       tmp = arrselection[i];
                       arrselection[i] = arrselection[minIndex];
                       arrselection[minIndex] = tmp;
                   }
                       
               }
           }
           return arrselection;
    }



for output:

private void executejButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
       String unSortedElements;
            unSortedElements = inputsjTextField.getText();
            int[] sorted = doSelectionSort(unSortedElements);
            sortedjTextField.setText(Arrays.toString(sorted));
        }


Was This Post Helpful? 0
  • +
  • -

#6 RCharles  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 12-August 12

Re: how to get the return value from another function?

Posted 16 August 2012 - 05:42 AM

View Postsmohd, on 16 August 2012 - 03:15 AM, said:

No, that error you were getting when you said: inputsjTextField.toString().
Also arr4 is not sorted, the sorted one is the returned array from doSelectionSort(). Again that method wont give you the desired output if you didnt take into consideration what I have told you before.
Do changes which I have told you then post your updated code with your error message if you are still getting any.


Thanks smohd
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1