About java coding
Question is to print the name and number you entered in order either asked by name or number.
package roaster1;
import java.util.*;
public class roaster1 {
@SuppressWarnings("resource")
public static void main(String[] args){
String [] last = new String[5];
int [] grade = new int[5];
int count=0;
int selection = 0;
char in='Y';
int a,b;
Scanner keyboard = new Scanner(System. in );
//System.out.println("Please enter your grade>>");
//grade[count]= keyboard.nextInt();
while( count < 5 && in !='N'){
System. out .println("Please enter your Last Name>>");
last[count]= keyboard.next();
//count= last.length;
System. out .println("Please enter grade from 1-100>>");
a= keyboard.nextInt();
if ( a >=0 && a<=100){
grade[count]=a;
}
else {
System. out .println("Please enter grade Again from 1-100>>");
b=keyboard.nextInt();
grade[count]=b;
++count;
}
System. out .println("Continue? Enter Y for Yes and N for NO?");
in= keyboard.next().toUpperCase().charAt(0);
}//While end
do {
System. out .println("");
System. out .println("Please choose an option");
System. out .println("1. Arrange By Student Name");
System. out .println("2. Arrange By Student Grade");
System. out .println("3. Exit Program");
System. out .println("");
selection = keyboard.nextInt();
switch(selection){
case 1:
case 2:
Bubble(last,grade,selection,count);
//Print(last, grade,count);
}//end switch
}while (selection !=3);//end do
}//main ends
private static void Bubble(String[] name, int[] grade, int Check,
int count) {
String nme=null;
int grd = 0;
if ( Check==1){
for ( int j=1; j< count; j++){
for( int i=1; i<= count-1; i++){
if (name[i-1].compareTo(name[i])>0){
nme = name[i-1];
grd = grade[i-1];
name[i-1]=name[i];
grade[i-1]=grade[i];
name[i]=nme;
grade[i]=grd;
}// if end
}//end for inner loop.b
}//end for outer loop.
System. out .println("Name\tGrade");
for (int i=0; i< count; i++){
System. out .println(name[ i]+""+grade[i]);
}
}//if for check end
else{
for (int j=1; j< count; j++){
for(int i=1; i<= count-1; i++){
if (grade[i-1] < grade[i]){
nme = name[i-1];
grd = grade[i-1];
name[i-1]= name[i];
grade[i-1]= grade[i];
name[i]= nme;
grade[i]=grd;
}//end for else for outer loop.
}//end for else for inner loop
}//end for else if last part for swap.
System. out .println("Name\tGrade");
for (int i=0; i< count; i++){
System. out .println(name[ i]+""+grade[i]);
}
}//else end
}//bubble class end
}//roaster class end.