Home > front end >  I am getting NameFormatException in my code and a certain part of code is being skipped?
I am getting NameFormatException in my code and a certain part of code is being skipped?

Time:01-29

I have written a program to accept data of two student, but when I run this code the 'name' part is skipped the second time and it start from subjects and gives NameformatException? First run of code is normal but when it goes for second run the name part is skipped and I have to directly enter the Integer value and if I don't I get NumberFormatException.

import java.util.Scanner;

    @SuppressWarnings("serial")
    class NegativeValueException extends Exception {
        
        public  NegativeValueException() {
            
        
    }
        }
    @SuppressWarnings("serial")
    class ValueOutOfRange extends Exception{
        public ValueOutOfRange() {
            System.out.println("Value out of range Exception");
        }
    }
public class Student1 {
    
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        for(int i=0;i<2;i  ) {
            String[] name = new String[2];
            int sub1=0;
            int sub2=0;
            int sub3=0;
            double avg;

            
            
            try {
            
                System.out.println("Enter the data of Student " (i 1) " : ");
                
                
                name[i] = sc.nextLine();
                if(name[i].equals(null))
                    break;
                else
                if(sc.hasNextInt()) 
                    sub1=sc.nextInt();
                else 
                    throw new NumberFormatException();
                    
                
                if(sc.hasNextInt())
                    sub2=sc.nextInt();
                else
                    throw new NumberFormatException();
                
                
                if(sc.hasNextInt())
                    sub3=sc.nextInt();
                else
                    throw new NumberFormatException();
                
                
                if(sub1<0)throw new NegativeValueException();
                if(sub1>100)throw new ValueOutOfRange();
                
                if(sub2<0)throw new NegativeValueException();
                if(sub2>100)throw new ValueOutOfRange();
                
                if(sub3<0)throw new NegativeValueException();
                if(sub3>100)throw new ValueOutOfRange();
                
                avg = (sub1 sub2 sub3)/3;
                System.out.println("Name : " name[i]);
                System.out.println("Marks in subject 1 : " sub1);
                System.out.println("Marks in subject 2 : " sub2);
                System.out.println("Marks in subject 3 : " sub3);
                System.out.println("Average Marks : " avg);
                
                }catch(NumberFormatException e) {
                System.out.println(e);
            }catch(NegativeValueException e) {
                System.out.println(e);
            }catch(ValueOutOfRange e) {
                System.out.println(e);
            }
            
            
        }
        
        sc.close();
    }

}

CodePudding user response:

As @Gurkirat has already pointed out the error, I am just adding my answer as an improvement to your code.

import java.util.Scanner;

    @SuppressWarnings("serial")
    class NegativeValueException extends Exception {
        public  NegativeValueException() {
            System.out.println("Negative Value Exception");       
        }
    }
    @SuppressWarnings("serial")

    class ValueOutOfRange extends Exception {
        public ValueOutOfRange() {
            System.out.println("Value out of range Exception");
        }
    }

    public class Student1 {
        public static void main(String[] args) {
            String[] name = new String[2];
            for(int i=0; i<2; i  ) {
                int sub1, sub2, sub3;
                double avg;

                try (Scanner sc = new Scanner(System.in)) {
                    System.out.println("Enter the data of Student "  (i 1)  " : ");
                    name[i] = sc.nextLine();
                    sub1 = sc.nextInt();
                    sub2 = sc.nextInt();
                    sub3 = sc.nextInt();
                    sc.next(); //Added this to solve your problem.

                    if(sub1 < 0 || sub2 < 0 || sub3 < 0)
                        throw new NegativeValueException();
                    if(sub1 > 100 || sub2 > 100 || sub3 > 100)
                        throw new ValueOutOfRange();
                
                    avg = (sub1 sub2 sub3)/3;
                    System.out.println("Name : "   name[i]   
                    "\nMarks in subject 1 : "   sub1  
                    "\nMarks in subject 2 : "   sub2  
                    "\nMarks in subject 3 : " sub3  
                    "\nAverage Marks : "   avg);
                
                 } catch(ValueOutOfRange | NegativeValueException e) {
                    System.out.println(e);
                 }
            }
        
    }

}

CodePudding user response:

The problem comes after reading the first cycle of inputs because after your last scan.nextInt() method of Scanner class, the cursor remains on the same line. so your enter is taken as input when you use nextLine() for name and when you enter name, it shows NFE. [https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo]

To resolve this, what you can do is that after taking your last integer input, consume the left over line by using the nextLine(), then your code works fine as expected,

import java.util.Scanner;

@SuppressWarnings("serial")
class NegativeValueException extends Exception {

    public NegativeValueException() {

    }
}

@SuppressWarnings("serial")
class ValueOutOfRange extends Exception {
    public ValueOutOfRange() {
        System.out.println("Value out of range Exception");
    }
}

public class Student1 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 2; i  ) {
            String[] name = new String[2];
            int sub1 = 0;
            int sub2 = 0;
            int sub3 = 0;
            double avg;

            try {

                System.out.println("Enter the data of Student "   (i   1)   " : ");

                name[i] = sc.nextLine();
//              sc.next();
                if (name[i].equals(null))
                    break;
                
                 if (sc.hasNextInt())
                    sub1 = sc.nextInt();
                else
                    throw new NumberFormatException();

                if (sc.hasNextInt())
                    sub2 = sc.nextInt();
                else
                    throw new NumberFormatException();

                if (sc.hasNextInt())
                    sub3 = sc.nextInt();
                else
                    throw new NumberFormatException();

                if (sub1 < 0)
                    throw new NegativeValueException();
                if (sub1 > 100)
                    throw new ValueOutOfRange();

                if (sub2 < 0)
                    throw new NegativeValueException();
                if (sub2 > 100)
                    throw new ValueOutOfRange();

                if (sub3 < 0)
                    throw new NegativeValueException();
                if (sub3 > 100)
                    throw new ValueOutOfRange();

                avg = (sub1   sub2   sub3) / 3;
                System.out.println("Name : "   name[i]);
                System.out.println("Marks in subject 1 : "   sub1);
                System.out.println("Marks in subject 2 : "   sub2);
                System.out.println("Marks in subject 3 : "   sub3);
                System.out.println("Average Marks : "   avg);
                sc.nextLine(); // add this 
            } catch (NumberFormatException e) {
                System.out.println(e);
            } catch (NegativeValueException e) {
                System.out.println(e);
            } catch (ValueOutOfRange e) {
                System.out.println(e);
            }

        }

        sc.close();
    }

}

and the output is

Enter the data of Student 1 : 
john
12
32
41
Name : john
Marks in subject 1 : 12
Marks in subject 2 : 32
Marks in subject 3 : 41
Average Marks : 28.0
Enter the data of Student 2 : 
jack
12
43
55
Name : jack
Marks in subject 1 : 12
Marks in subject 2 : 43
Marks in subject 3 : 55
Average Marks : 36.0
  •  Tags:  
  • Related