| | |
urgent help needed
![]() |
•
•
Join Date: Mar 2006
Posts: 7
Reputation:
Solved Threads: 0
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
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
•
•
•
•
Originally Posted by parvin2
nobody wants to help me?
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.
*Voted best profile in the world*
![]() |
Similar Threads
- Urgent Advice needed (Community Introductions)
- help please urgent review needed (Website Reviews)
- java newbie (Java)
- Urgent computer help needed – I can’t see my task bar! (Windows NT / 2000 / XP)
Other Threads in the Java Forum
- Previous Thread: plz i need help
- Next Thread: Hey check out my prog. It has an error can u find it?
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image inetaddress integer integration intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






