|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

05-13-2008, 06:38 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 31
|
|
|
How to sort 2 arrays together
Hi, i have 2 arrays the frist is to stroe students names and the second is for students grades i want to sort the names in alphabetical order and the grades moves to its names how to do this
import java.util.*;
public class Main {
static Scanner console= new Scanner(System.in);
public static void main(String[] args)
{
String []names=new String[10];
for(int i =0;i<names.length;i++)
{
System.out.print("Please Enter Student name ");
System.out.println();
names[i] =console.next();
}
int []grade=new int[10];
for(int i =0;i<grade.length;i++)
{
System.out.print("Please Enter Student name ");
System.out.println();
grade[i] =console.nextInt();
}
}
// Now I want to define a method that sort the names
// and then moves the grade to its name
}
|
|

05-13-2008, 06:53 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,040
|
|
|
Use Arrays.sort() to sort an one array, keep the track if index and find the corresponding element of the next.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-13-2008, 08:26 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 31
|
|
|
please write code to explain more
|
|

05-13-2008, 08:40 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,040
|
|
Best things is make an attempt to do something. Anyway here is a simple code, keep in mind this is not the only way to do this.
public class ArrayComparing {
Scanner console= new Scanner(System.in);
String[] names;
String[] temp;
int[] grade;
public static void main(String[] args) {
new ArrayComparing().getUserData();
}
private void getUserData() {
names = new String[3];
for(int i = 0; i < names.length; i++) {
System.out.print("Please Enter Student name: ");
names[i] =console.nextLine();
temp[i] = names[i];
}
grade = new int[3];
for(int i =0;i<grade.length;i++) {
System.out.print("Please Enter Student marks: ");
grade[i] =console.nextInt();
}
sortArray(names);
}
private void sortArray(String[] arrayToSort) {
Arrays.sort(arrayToSort);
getIndex(arrayToSort);
}
private void getIndex(String[] sortedArray) {
for(int x = 0; x < sortedArray.length; x++) {
for(int y = 0; y < names.length; y++) {
if(sortedArray[x].equals(temp[y])) {
System.out.println(sortedArray[x] + " " + grade[y]);
}
}
}
}
}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-13-2008, 01:13 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 31
|
|
what is your opion of this code and is there another way to do this?
this only the method used to sort the two arrayes
public void sort(String[] names, int[] grade){
boolean swapped;
do{
swapped = false;
for (int i = 0; i < names.length-1; i++) {
if(names[i].compareToIgnoreCase(names[i+1])>0){
String temp = names[i+1];
names[i+1] = names[i];
names[i] = temp;
int temp1 = grade[i+1];
grade[i+1] = grade[i];
grade[i] = temp1;
swapped=true;
}
}
}while(swapped);
}
|
|

05-13-2008, 01:18 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,040
|
|
Ok, did you test that your code and get the correct answer? If so that's fine.
Just look at my code. Used fewer number of lines to do this. At the time that is the way I get into my mind. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-13-2008, 07:23 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 31
|
|
|
it is work but how to divid this code into 2 methods the frist sort names and the second sort grades
|
|

05-13-2008, 07:47 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 31
|
|
|
now this the final code i want
import java.util.*;
public class Main {
static Scanner console= new Scanner(System.in);
public static void main(String[] args)
{
String []names=new String[2];
for(int i =0;i<names.length;i++)
{
System.out.print("Please Enter Student name ");
System.out.println();
names[i] =console.next();
}
int []grade=new int[2];
for(int i =0;i<grade.length;i++)
{
System.out.print("Please Enter Student grade ");
System.out.println();
grade[i] =console.nextInt();
}
sortname(names,grade);
// here should be acall to amethod to prtint the arrays after sorting
// i donnot know how to return the sorted arrays to make the print
}
public static void sortname(String[] names, int[] grade){
boolean swapped;
do{
swapped = false;
for (int i = 0; i < names.length-1; i++)
{
if(names[i].compareToIgnoreCase(names[i+1])>0)
{
String temp = names[i+1];
names[i+1] = names[i];
names[i] = temp;
// here after sort the name we make acall to the second mehthod
sortgrade(names,grade,i);
swapped=true;
}
}
}while(swapped);
}
public static void sortgrade(String []names,int[] grade ,int i)
{ // this method to sort the garde arry
if(names[i].compareToIgnoreCase(names[i+1])>0)
{
int temp1 = grade[i+1];
grade[i+1] = grade[i];
grade[i] = temp1;
}
}
// here will be the print method
}
|
|

05-14-2008, 05:29 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,040
|
|
|
Check the output and see it's sorted there.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-14-2008, 06:12 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 31
|
|
|
thanks it need some modify and finished at last
thanks alot for you
|
|

05-14-2008, 06:24 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,040
|
|
Ok try it and let me here if you like, I see what happened in your code.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-18-2008, 07:44 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 31
|
|
|
import java.util.*;
public class Main {
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
String []names=new String[2];
for(int i =0;i<names.length;i++)
{
System.out.print("Please Enter Student name ");
System.out.println();
names[i] =console.next();
}
int []grade=new int[2];
for(int i =0;i<grade.length;i++)
{
System.out.print("Please Enter Student grade ");
System.out.println();
grade[i] = console.nextInt();
}
sortname(names,grade);
print (names,grade);
}
public static void sortname(String[] names, int[] grade){
boolean swapped;
do{
swapped = false;
for (int i = 0; i < names.length-1; i++)
{
if(names[i].compareToIgnoreCase(names[i+1])>0)
{
String temp = names[i+1];
names[i+1] = names[i];
names[i] = temp;
sortgrade(names, grade,i);
swapped=true;
}
}
}while(swapped);
}
public static void sortgrade(String []names,int[] grade ,int i)
{ int temp1 = grade[i+1];
grade[i+1] = grade[i];
grade[i] = temp1;
}
public static void print(String []names,int[] grade )
{
for (int i = 0; i < names.length; i++)
System.out.print(names[i]+" "+grade[i]+"\n");
}
}
|
|

05-21-2008, 05:26 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,040
|
|
|
Yep, your attempt is not bad. How you going to handle more than two entries at a time. I mean using this code only can sorted two students.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-21-2008, 05:36 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,040
|
|
|
Always I try to pointed out, try to use minimum number of variables. In your sortgrade() method you have not refer string array names. If you are not use it, better to avoid declaring such things.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|