-2

I am unable to compile and execute the java Code.This is the program about Implementing Interface for multiple inheritance This is the Code Below:

//Interface Implementation
import java.util.Scanner;
interface Sports
{
int sportswt = 12;
}
class Student
{
int rollno;
String name;
Scanner sc = new Scanner(System.in);
void getData()
{
    System.out.println("Name and Roll no :");
    rollno = sc.nextInt();
    name = sc.nextLine();
}
}
class Marks extends Student implements Sports
{
 Scanner sc = new Scanner(System.in);
 int m1 = sc.nextInt();
 int m2 = sc.nextInt();
 System.out.println("Name is : " + name + "\nRoll no is : " + rollno);
 System.out.println("Marks1 : " +m1+ "\nMarks2 : " +m2+"\nTotal is : " + 
 (m1+m2+sportswt)); 
 }
  class InterfaceImplementation6
  {
   public static void main(String[] args){
    Marks mobj = new Marks();
    mobj.getData();
   }
  }

Can someone please Help !! I am getting this error:

enter image description here

Vince
  • 14,470
  • 7
  • 39
  • 84
Mr_Null_Byte
  • 39
  • 1
  • 6
  • 2
    That's not a compilation error. And don't post it in a picture, post it as text in your question. – Sotirios Delimanolis Sep 07 '17 at 17:58
  • So what's the error ? – Mr_Null_Byte Sep 07 '17 at 17:59
  • @user6369925 That's a runtime error, see [Runtime VS Compile time](https://stackoverflow.com/questions/846103/runtime-vs-compile-time), and you should post the error as text rather than a picture, as already suggested. Also, format your code properly. Without a properly formatted post, you'll probably get ignored or downvoted. – Vince Sep 07 '17 at 18:10
  • The code above doesn't compile, the Marks class has code inlined on the class itself, was that intended to be code inside of an override `getData()` method? – rastadrian Sep 07 '17 at 18:13

1 Answers1

0

The main problem was regarding using nextLine() rather than just next().

I refactored a little bit the code to make it more legible:

import java.util.Scanner;

interface Sports {
    int sportswt = 12;
}

class Student {
    int roll;
    String name;

    void getData() {
        System.out.println("Name and roll number:");
        Scanner sc = new Scanner(System.in);
        name = sc.next(); //if you first ask for name, read the name first
        roll = sc.nextInt();
    }
}

class Marks extends Student implements Sports {

    @Override
    void getData() {
        super.getData();
        Scanner sc = new Scanner(System.in);
        int mark1 = sc.nextInt(); //be more descriptive on your variable names
        int mark2 = sc.nextInt();
        int total = mark1 + mark2 + sportswt; //try to extract operations out of the println() function, so is easier to understand
        System.out.println("Name is : " + name + "\nRoll no is : " + roll);
        System.out.println("Marks1 : " + mark1 + "\nMarks2 : " + mark2 + "\nTotal is : " + total);
    }
}

class InterfaceImplementation6 {

    public static void main(String[] args) {
        Marks mobj = new Marks();
        mobj.getData();
    }
}
rastadrian
  • 1,025
  • 1
  • 10
  • 17