Results 1 to 6 of 6
  1. #1
    jihad is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default InputMismatchException

    Java Code:
    public class Assigment9 {
    
       public static Scanner cso=new Scanner(System.in);
        public static void main(String[] args) 
        {examresult();
           
        }
        public static void examresult()
        {
            System.out.println("pls enter number of student");
            int student=nt();
            System.out.println("pls enter number of questions");
            int answers=nt();
            char[][] sandq=new char[student][answers];
            System.out.println("pls enter answers");
            int[] solitions=new int[answers];
            for(int nm:solitions){solitions[nm]=nt();}
            System.out.println("pls enter answer of students");
            for(int ns=1;ns<sandq.length;ns++)
            {for(int nq=1;nq<sandq[student].length;nq++)
            {
                sandq[ns][nq]=Ans();//<<------------------------- here something wrong
            }
            }
            System.out.println(""+sandq);
        }
        public static int nt()
        {int a=cso.nextInt();
        return a;
        }
        public static char Ans()
        {
           String anss=cso.next();
           char ansc=anss.charAt(0);
            return ansc;
        }
    it gave eror that is InputMismatchException
    Last edited by pbrockway2; 12-17-2011 at 08:45 AM. Reason: code tags added

  2. #2
    DeNiS_M is offline Member
    Join Date
    Sep 2011
    Location
    Athens Greece
    Posts
    29
    Rep Power
    0

    Default Re: InputMismatchException

    Hello

    There are a few (logical)errors in your code

    First in the for loop int line 17.
    What you want to do is to actually fill the solitions array with some numbers.What you actually doing is iterating into an empty array.

    Second in the for loops in lines 19, 20 you should begin counting from 0 not from 1.Also in the inner for loop
    Java Code:
    for(int nq=1;nq<sandq[student].length;nq++)
    you want the questions for each student not just the one with the student value.

    Last but not least if you want to vie the contents of the sandq array you should print it row by row.

    Hope i helped :)

  3. #3
    jihad is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: InputMismatchException

    I made some change on my code here we go
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package assigment.pkg9;
    import java.util.Scanner;

    /**
    *
    * @author jihad
    */
    public class Assigment9 {

    public static Scanner cso=new Scanner(System.in);
    public static void main(String[] args)
    {examresult();

    }
    public static void examresult()
    {
    System.out.println("pls enter number of student");
    int student=input_int();
    System.out.println("pls enter number of questions");
    int answers=input_int();
    char[][] sandq=new char[student][answers];
    System.out.println("pls enter answers");
    char[] solitions=new char[answers];
    for(int nm=0;nm<solitions.length;nm++)
    {
    solitions[nm]=input_char();
    }
    System.out.println("pls enter answer of students");//(*)
    int puan=0;
    for(int ns=0;ns<sandq.length;ns++)
    {

    for(int nq=0;nq<sandq[answers].length;nq++)
    {

    sandq[ns][nq]=input_char();
    }
    }
    }
    public static int input_int()
    {
    int a=cso.nextInt();
    return a;
    }
    public static char input_char()
    {
    String anss=cso.next();
    char ansc=anss.charAt(0);
    return ansc;
    }
    }
    there are still somthing that is wrong
    here is the my hw may be you can understand my code easily thanks for everything

    application will give score for multiple choice questions.Firstly,you
    will ask user the number of students and questions in the exam.Then
    answers of questions should be entered by user one by one.Since entering
    all input takes to much time when you test your application,you can fill
    this array with random characters between A and E using random method from
    Math class,or you can use Random class.Next step is taking key answers
    from user and checking students result.As output your application at least
    should display number of correct answers and result of student.Be careful
    if there are 10 questions in the exam you will multiply each correct
    answer by 10,but if there are 20 question,you will multiply it by 5.

    it gave that exeption
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    I leave a sign there

  4. #4
    jihad is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: InputMismatchException

    I didnt write to check the answers wheter true or not I will write it but here I couldnt handle when I wrote in for loop the lenght of sandq array as the answers variable it gave exeption
    not :my english is not good if you write by more basic english it will be good for me
    Last edited by jihad; 12-17-2011 at 03:27 PM.

  5. #5
    jihad is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: InputMismatchException

    I made change little bit more here

    package assigment.pkg9;
    import java.util.Scanner;
    public class Assigment9 {

    public static Scanner cso=new Scanner(System.in);
    public static void main(String[] args)
    {examresult();

    }
    public static void examresult()
    {
    System.out.println("pls enter number of student");
    int student=input_int();
    System.out.println("pls enter number of questions");
    int answers=input_int();
    char[][] sandq=new char[student][answers];
    System.out.println("pls enter answers");
    char[] solitions=new char[answers];
    for(int nm=0;nm<solitions.length;nm++)
    {
    solitions[nm]=input_char();
    }
    System.out.println("pls enter answer of students");
    int puan=0;int ogrenci=0;
    for(int ns=0;ns<student;ns++)
    {ogrenci++;
    System.out.printf("\n %d student \n",ogrenci);
    for(int nq=0;nq<answers;nq++)
    {

    sandq[ns][nq]=input_char();
    }
    }
    }
    public static int input_int()
    {
    int a=cso.nextInt();
    return a;
    }
    public static char input_char()
    {
    String anss=cso.next();
    char ansc=anss.charAt(0);
    return ansc;
    }
    }

  6. #6
    jihad is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: InputMismatchException

    I finish the hw if this topic unnessery tou can delete moderator
    but here my codes
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package assigment.pkg9;
    import java.util.Scanner;

    /**
    *
    * @author jihad
    */
    public class Assigment9 {

    public static Scanner cso=new Scanner(System.in);
    public static void main(String[] args)
    {
    examresult();

    }
    public static void examresult()
    {
    System.out.println("pls enter number of student");
    int student=input_int();
    System.out.println("pls enter number of questions");
    int answers=input_int();
    char[][] sandq=new char[student][answers];
    System.out.println("pls enter answers");
    char[] solitions=new char[answers];
    for(int nm=0;nm<solitions.length;nm++)
    {
    solitions[nm]=input_char();
    }
    System.out.println("pls enter answer of students");
    int puan=0;int ogrenci=0;int[] studentPoints=new int [student];
    for(int ns=0;ns<student;ns++)
    {ogrenci++;
    System.out.printf("\n %d student \n",ogrenci);
    for(int nq=0;nq<answers;nq++)
    {

    sandq[ns][nq]=rndChar();
    if(sandq[ns][nq]==solitions[nq])
    if(answers==5)
    puan=puan+20;
    if(answers==10)puan=puan+10;
    }
    studentPoints[ns]=puan;
    puan=0;
    }ogrenci=0;
    for(int ns=0;ns<studentPoints.length;ns++)
    {
    ogrenci++;
    System.out.printf("\n %d students point is %d",ogrenci,studentPoints[ns]);
    }
    }
    public static int input_int()
    {
    int a=cso.nextInt();
    return a;
    }
    public static char input_char()
    {
    String anss=cso.next();
    char ansc=anss.charAt(0);
    return ansc;
    }
    private static char rndChar () {
    int rnd = (int) (Math.random() * 5); // or use Random or whatever
    char base = (rnd < 6) ? 'a':'b';
    return (char) (base + rnd %5);

    }

    }
    I used some turkish word as variable I hope it wont be problem

Similar Threads

  1. Replies: 2
    Last Post: 02-11-2011, 10:03 AM
  2. try-catch InputMismatchException in a while loop
    By themulator in forum New To Java
    Replies: 17
    Last Post: 10-12-2010, 05:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •