-1

I am learning about Arrays and had gotten this assignment. Whenever I run the code, the value which is supposed to go into String array goes into Int. It skips over the name[i]=sc.nextLine(); completely. Any Help will be appreciated.

import java.util.*;
public class Marks_Remarks {
public static void main(String []Args){
    Scanner sc= new Scanner (System.in);
    System.out.println("Enter how many students");
    int n= sc.nextInt();
    String name[]= new String[n];
    int roll[]= new int[n];
    int sub1[]= new int[n];
    int sub2[]= new int[n];
    int sub3[]= new int[n];

    for (int i=0;i<n;i++){
        System.out.println("Enter Name");
        name[i]= sc.nextLine();

        System.out.println("Enter Roll No.");
        roll[i]= sc.nextInt();

        System.out.println("Enter marks in Three Subjects");
        sub1[i]= sc.nextInt();
        sub2[i]= sc.nextInt();
        sub3[i]= sc.nextInt();
    }
    int avg; String remark;
    for (int k=0;k<n;k++){
        avg= (sub1[k]+ sub2[k]+ sub3[k])/3;
        if (avg>=85)
            remark= "Excellent";
        else if(avg>=75 && avg<=84)
            remark= "Distinction";
        else if(avg>=60 && avg<=74)
            remark= "First Class";
        else if(avg>=40 && avg<=59)
            remark="Pass";
        else
            remark="Poor";

        System.out.println("Name: "+ name[k]+ "\tRoll No.: "+ roll[k]+ " \tAverage: "+ avg+ " \tRemark: " + remark);
        avg=0;
    }
}
}
LppEdd
  • 16,731
  • 6
  • 53
  • 100

1 Answers1

0

Use name[i]=sc.next(); instead of : name[i]=sc.nextLine(); It may help you.

Aymen TAGHLISSIA
  • 1,044
  • 3
  • 23