java - Sorting arrays...can't seem to get it -
i need write program have user enter list of tutor names. 10 peer tutors may hired. then, program present each name, based on list alphabetized last name. have far. i've been figuring out how sort array hours , cant seem figure out. if can explain me in easy terms i'm doing wrong, won't make same mistakes again, appreciate it.
here errors i'm getting:
errors: peertutorreport.java:11: error: method tutornames in class peertutorreport cannot applied given types; int[] numtutors = tutornames(); ^ required: int found: no arguments reason: actual , formal argument lists differ in length peertutorreport.java:46: error: cannot find symbol string[] listnames = new string[numtutor]; ^ symbol: variable numtutor location: class peertutorreport peertutorreport.java:48: error: cannot find symbol (x = 0; x <= listnames.length; x++) { ^ symbol: variable x location: class peertutorreport peertutorreport.java:48: error: cannot find symbol (x = 0; x <= listnames.length; x++) { ^ symbol: variable x location: class peertutorreport peertutorreport.java:48: error: cannot find symbol (x = 0; x <= listnames.length; x++) { ^ symbol: variable x location: class peertutorreport peertutorreport.java:49: error: cannot find symbol numtutors[x] = joptionpane.showmessagedialog(" tutor last name , first name listed in alphabetically order"+(x+1)+ " " + "for example: 'smith, john'"); ^
here's code. first method works no problem, second method... i'm having trouble with.
import javax.swing.joptionpane; import java.util.arrays; public class peertutorreport { public static void main(string[] args) { string[] listnames = gettutornames(); int[] numtutors = tutornames(); } public static string[] gettutornames() { string firstname; string lastname; string[] listnames = new string[10]; (int x = 0; x < listnames.length; x++) { firstname = joptionpane.showinputdialog(null, "enter tutor's first name: "); lastname = joptionpane.showinputdialog(null, "enter tutor's last name: "); listnames[x] = lastname + ", " + firstname; } return listnames; } public static string[] tutornames(int numtutors) { string[] listnames = new string[numtutor]; (x = 0; x <= listnames.length; x++) { numtutors[x] = joptionpane.showmessagedialog( "tutor last name , first name listed in alphabetically order" + (x + 1) + " " + "for example: 'smith, john'"); arrays.sort(listnames); } return listnames; } }
edit: let's @ main
method too:
public static void main(string[] args) { string[] listnames = gettutornames(); int[] numtutors = tutornames(); }
- you're not using names you've fetched in
gettutornames
. why ask them if you're not going pass them else? - the
tutornames
method hasint
parameter, you're not passing arguments - the
tutornames
method declared returnstring[]
, you're trying assign resultint[]
variable.
note none of these problems sorting - they're more basic that. suggest stop , very closely @ of code.
edit: okay, let's @ other method:
public static string[] tutornames(int numtutors) { string[] listnames = new string[numtutor]; (x = 0; x <= listnames.length; x++) { numtutors[x] = joptionpane.showmessagedialog("..."); arrays.sort(listnames); } return listnames; }
you're trying assign numtutors[x]
despite fact numtutors
int
. that's not going work. neither new string[numtutor]
numtutor
variable doesn't exist.
you're sorting array (which never populated) on every iteration... why?
edit: wrote answer before question corrected...
well, here's first problem:
for (int x = 0; x < listnames.length; x++) { firstname = joptionpane.showinputdialog(null, "enter tutor's first name: "); lastname = joptionpane.showinputdialog(null, "enter tutor's last name: "); }
you're asking 10 pairs of names... storing them in firstname
, lastname
variable each time. listnames
variable never touched. want like:
for (int x = 0; x < listnames.length; x++) { string firstname = joptionpane.showinputdialog(null, "enter tutor's first name: "); string lastname = joptionpane.showinputdialog(null, "enter tutor's last name: "); listnames[x] = lastname + ", " + firstname; }
note how i've moved variable declarations firstname
, lastname
inside loop - you're not using them outside loop anyway, there's no benefit in declaring them earlier. in general, prefer restrict scope of local variables far can.
or ask single name per tutor:
for (int x = 0; x < listnames.length; x++) { listnames[x] = joptionpane.showinputdialog(null, "enter tutor's name: "); }
Comments
Post a Comment