# InputMismatchException

• 12-17-2011, 07:24 AM
InputMismatchException
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
• 12-17-2011, 10:33 AM
DeNiS_M
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
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 :)
• 12-17-2011, 01:57 PM
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;

/**
*
*/
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");
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++)
{

{

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
• 12-17-2011, 02:01 PM
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
• 12-17-2011, 02:39 PM
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");
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);
{

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;
}
}
• 12-17-2011, 03:10 PM
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;

/**
*
*/
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");
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);
{

sandq[ns][nq]=rndChar();
if(sandq[ns][nq]==solitions[nq])
puan=puan+20;
}
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