Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-13-2008, 06:38 AM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
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
Code:
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 }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-13-2008, 06:53 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,040
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-13-2008, 08:26 AM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
please write code to explain more
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-13-2008, 08:40 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,040
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-13-2008, 01:13 PM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
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
Code:
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); }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-13-2008, 01:18 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,040
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-13-2008, 07:23 PM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
it is work but how to divid this code into 2 methods the frist sort names and the second sort grades
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 05-13-2008, 07:47 PM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
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
}
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 05-14-2008, 05:29 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,040
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 05-14-2008, 06:12 AM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
thanks it need some modify and finished at last
thanks alot for you
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 05-14-2008, 06:24 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,040
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 05-18-2008, 07:44 PM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
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");
}
}
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 05-21-2008, 05:26 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,040
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 05-21-2008, 05:36 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,040
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort a list using Bubble sort algorithm Java Tip Algorithms 3 04-29-2008 09:04 PM
Arrays bunbun New To Java 1 04-09-2008 03:24 AM
new to arrays jimJohnson New To Java 1 04-08-2008 03:45 PM
2D-Arrays kbyrne New To Java 1 02-07-2008 11:08 PM
arrays help Warren New To Java 6 11-23-2007 08:23 PM


All times are GMT +3. The time now is 08:25 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org