hello
I,ve wrote a program but it doesn,t work correctly
please help me
i want to sort some strings based on the number that should be entered at first of each string for example the strings that you enter should be in this form:

12_hello

it means you should enter a number and then _ then a string
for example if you enter

5_allow
4_how
23_hi

the output should be

4_how
5_allow
23_hi

but my program can't do this
i will write my program here

please correct it for me
thanks in advance

Parvin

package sort;

import javax.swing.*;

public  class arraySort
{
 static String[] minStrSoFar = new String[1024];
 static String sortStr[] = new String[1024];
static int j;
 //***
  //this method sorts entered strings based on the number that have been entered before  ***//


  public static void selectSort(int [] enterNum,int number,String part2[])
 {
   // initialize  the variables
   int posMin=0;
   int temp;
   int k;
   String tempStr = new String ();



   for(int i = 0;i<number -1 ; i++)
   {

     posMin = findPosMin(enterNum, i, number, part2);

   if (posMin != i)
     {
       //this part sorts the part of string that is made up of numbers
       temp = enterNum[i];
       enterNum[i] = enterNum[posMin];
       enterNum[posMin] = temp;

    //this part sorts the part of string that is made up of characters
      tempStr = part2[i];
      // part2[i] = minStrSoFar[i];
    //   minStrSoFar[i] = tempStr;
    part2[i] = sortStr[i];
    sortStr[i] = tempStr;

     }//end of if
   JOptionPane.showMessageDialog(null,part2[i]);
   }//end of for

 }//end of selectSort method


 //this method find the position of minimum between entered numbers

 public static int findPosMin(int[] enterNum , int i,int number,String part2[])
 {

  minStrSoFar[i] = part2[i];
   int posMinSoFar = i;

   for(  j =i+1 ;j<number ;j++)
   {
     if(enterNum [j] <enterNum[posMinSoFar] )
     {
       posMinSoFar = j;
       minStrSoFar[i] = part2[j];
     }//end of if

   }//end of for
 sortStr[i]=minStrSoFar[i];
   return posMinSoFar;

 } //end of findPosMin method

}//end of class arraySort



package sort;

//***
  // this program sorts some strings that begins with number( based on their number)****//
import javax.swing.*;
import java.util.StringTokenizer;

public class sortString
{

  public static void main(String[] args)
  {
    //initialize the variables
    int number;
    String stringNumber = JOptionPane.showInputDialog("how many string do you want to enter?");
     number  = Integer.parseInt(stringNumber);
    String string[] = new String[number];
    String part1[] = new String [number];
    String part2[] = new String [number];
    int enterNum []= new int[number];
    int i;
    for( i = 0; i<number; i++)
    {
      string[i] = new String();
      part1 [i] = new String();
      part2[i] = new String();
      string[i] = JOptionPane.showInputDialog("enter your string");

      StringTokenizer st = new StringTokenizer(string[i],"_");

      //seperating 2 parts of the string
      part1[i] = st.nextToken();
      part2[i] =st.nextToken();
      enterNum[i] = Integer.parseInt(part1[i]);

        }//end of for
    arraySort.selectSort(enterNum,number,part2);

  }//end of main method
}//end of sortString class

Dani AI

Generated

Quick diagnosis and a simple fix for : the numeric values are being swapped correctly, but the corresponding strings are not — the code writes into auxiliary buffers instead of swapping the two string positions, so numbers and strings get out of sync. As pointed out, the tokenization step was fine; the real issue is the swap logic inside the selection routine and the use of extra global arrays that complicate things.

The minimal and safest fix is to swap the string entries in the same place you swap the integers. Replace the auxiliary-buffer approach with a direct three-line swap for the string array:

if (posMin != i) {
    int tmp = enterNum[i];
    enterNum[i] = enterNum[posMin];
    enterNum[posMin] = tmp;

    String tmpS = part2[i];
    part2[i] = part2[posMin];
    part2[posMin] = tmpS;
}

A cleaner and more maintainable approach is to pair the number and string into a small object and sort those objects. That avoids parallel arrays entirely and makes the intent obvious:

static class Entry implements Comparable<Entry> {
    final int key;
    final String rest;
    Entry(int k, String r){ key = k; rest = r; }
    public int compareTo(Entry o){ return Integer.compare(key, o.key); }
    public String toString(){ return key + "_" + rest; }
}

// create Entry[], Arrays.sort(entries), then print entries

Troubleshooting tips: remove GUI popups from inside the sorting loop (use them after sorting or use console prints while debugging); validate input before parsing (check for the separator and catch NumberFormatException); avoid large fixed-size static arrays — use exact-sized arrays or Lists. After applying the direct swap or the Entry-based sort, test with a handful of cases to confirm numbers and strings stay paired.

Recommended Answers

All 2 Replies

nobody wants to help me?

Member Avatar for Member #46692

nobody wants to help me?

Don't bump the thread. People will help you when they are ready.

You're on the right track though, you've tokenised the original string splitting it into two parts. Then you are sorting the string according to its integer. I'll have a look at it later.

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.