Java array to input student names according to their number -
i need create array prompts professor input how many students in class. prompts them input names until number of students met. have wrong hoping insight.
system.out.println("please enter number of students in class: "); int numberofstudents = console.nextint(); string [] studentname = new string [numberofstudents]; (int i=0; i<studentname.length; i++) { system.out.println("enter name of student " + (i+1) + " in class. "); studentname[i] = console.nextline(); system.out.println("student name entered: " + studentname[i]); }
edit: changed code bit, array. loop intending have go through each number , assign student name it. last line of code gives me error saying confusing indentation.
edit 2: after proofreading question , code myself i've noticed basic mistakes , have dealt them, 1 last question. right while code works, when asks me input name, skips student 1, leaves blank moves onto student 2. shown in screenshot http://puu.sh/8fl8e.png
you weren't far...
scanner console = new scanner (system.in); system.out.println("please enter number of students in class: "); int numberofstudents = console.nextint(); string [] studentname = new string [numberofstudents]; (int i=0; i<studentname.length; i++){ system.out.println("enter name of student" + (i+1) + "in class. "); studentname[i] = console.next(); }
to test code:
(int i=0; i<studentname.length; i++){ system.out.println(studentname[i]); }
edit: answer second edit: use
console.next();
instead of
console.nextline();
Comments
Post a Comment