943,931 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1191
  • Java RSS
Mar 30th, 2006
0

urgent help needed

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
parvin1987 is offline Offline
7 posts
since Mar 2006
Mar 30th, 2006
0

Re: urgent help needed

nobody wants to help me?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
parvin2 is offline Offline
3 posts
since Feb 2006
Mar 30th, 2006
0

Re: urgent help needed

Quote originally posted by parvin2 ...
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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: plz i need help
Next Thread in Java Forum Timeline: Hey check out my prog. It has an error can u find it?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC