java - Multiple inputs with arrays using String - Names -
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, not work , don't know do. need continue run until stop , continue more of program.
import javax.swing.joptionpane; import java.util.arrays; public class report { public static void main(string[] args) { int numtutors = 10; string[] listnames = gettutornames(); } 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: "); if (firstname.equals("")) && lastname.equals("")) { break; // loop end } listnames[x] = lastname + ", " + firstname; } return listnames; } }
well, first. intellij didn't format code correctly when edited it, , discovered hit-list of errors. bear in mind - code won't compile, let alone run, until these fixed.
int numtutors
comes out of nowhere. if want define it, outside of method call , set appropriate value.public static void main(string[] args) { int numtutors = 10; string[] listnames = gettutornames(numtutors); }
these declarations invalid:
string = firstname; string = lastname;
you need sort of variable name in between
string
,=
.you're not matching contract you're passing in
gettutornames
- either pass in or accept must change. i'm thinking it's latter.you can't use
==
comparestring
. have use.equals()
. leads me to...your break outside of loop. move inside of loop.
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: "); if (firstname.equals(" "))&&lastname.equals(" ")){ break; // loop end } }
..and that leads me to...
you don't put values anywhere through loop! you're running same code ten times! place them array.
// after check see if firstname , lastname blank listnames[x] = firstname + lastname; // don't know want them here.
there no
.add()
array. above how enter elements array.your
return
outside of method block entirely. move method.
now, these issues find. work on compilation issues first, 1 may talk errors in code logic. if can, snag quiet moment , ensure understand variable declaration , string
comparison. strongly recommend reading material found in the java wiki tag.
Comments
Post a Comment