InputMismatchException but I'm sure the data is correct.

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 639 Views - Last Post: 14 October 2011 - 11:54 AM Rate Topic: -----

#1 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

InputMismatchException but I'm sure the data is correct.

Posted 14 October 2011 - 04:13 AM

So I'm almost 100% positive my code should run as expected and I should be able to do this no problem. I guess I just need someone to read through and see what's up here.


// Written by Mike Zrimsek.
//
// Solves CS 141, Fall 2011, Homework 7.
//
import java.io.*;
import java.util.Scanner;
/////////////////////////////////////////////////////////////////////////
class Hw07
{
//-----------------------------------------------------------------------
   public static void main (String [] args) throws Exception
   {
      Scanner kb = new Scanner(System.in);

      System.out.print("\nEnter name of a file of Students: ");
      String filename = kb.nextLine();

      Section sec = new Section(filename);

      System.out.println("\nIn forward order:");
      sec.print(System.out);

      System.out.println("\nThat Section contains " +
                         sec.howMany() +
                         " Student(s).");

      System.out.println("\nThat Section contains " +
                         sec.nPassing() +
                         " passing Student(s).");

      System.out.println("\nThe highest grade is " +
                         sec.highestGrade() + ".");
   } 
//-----------------------------------------------------------------------
} // end class Hw07
/////////////////////////////////////////////////////////////////////////
class Section
{
   private Student [] a;
   private int used;
   private static final int INIT_SIZE = 20;
//-----------------------------------------------------------------------
   public Section ( String filename ) throws Exception
   {
      Scanner sc = new Scanner(new File(filename));

      a = new Student[INIT_SIZE];
      used = 0;

      while ( sc.hasNext() )
      {
         Student s = Student.read(null,sc);

         if ( used == a.length )
         {
            Student[] newA = new Student[2*a.length+1];
            for ( int i = 0 ; i < used ; i++ ) newA[i] = a[i];
            newA[used] = s;
            used++;
            a = newA;
         }
         else
         {
            a[used] = s;
            used++;
         }
      }
   }
//-----------------------------------------------------------------------
   public void print ( PrintStream ps )
   {
      for ( int i = 0 ; i < used ; i++ )
      {
         ps.println(a[i]);
      }
   }
//-----------------------------------------------------------------------
   public int howMany() { return used; }
//-----------------------------------------------------------------------
   public double average ()
   {
      int sum = 0;

      for ( int i = 0 ; i < used ; i++ )
      {
         Student s = a[i];
         sum += s.getGrade();
      }

      return 1.0 * sum / used;
   }
//-----------------------------------------------------------------------
   public int nPassing()
   {
      int sum = 0;
      int grade = 0;
      for(int i = 0; i < a.length; i++)
      {
         grade = a[i].getGrade();
         if(grade >= 60) sum++;
      }
      return sum;
   }
//-----------------------------------------------------------------------
   public int highestGrade()
   {
      int max = 0;
      int grade = 0;
      if(used == 0) throw new Error
         ("Attempt to find the maximum of an empty Section");
      for(int i = 0; i < a.length; i++)
      {
         grade = a[i].getGrade();
         if(grade > max) grade = max;
      }
      return max;
   }
//-----------------------------------------------------------------------
} // end class Section
/////////////////////////////////////////////////////////////////////////
class Student
{
   private String name;
   private int grade;
//-----------------------------------------------------------------------
   public Student ( String name, int grade )
   {
      this.name = name;
      this.grade = grade;
   }
//-----------------------------------------------------------------------
   public String toString ()
   {
      return name + " (" + grade + ")";
   }
//-----------------------------------------------------------------------
   public String getName() { return name; }
//-----------------------------------------------------------------------
   public int getGrade() { return grade; }
//-----------------------------------------------------------------------
   public void setGrade( int newGrade ) { grade = newGrade; }
//-----------------------------------------------------------------------
   public static Student read ( PrintStream ps, Scanner sc ) 
   {
      if ( ps != null ) ps.println("Reading a Student record ...");
      if ( ps != null ) ps.print("Enter the name: ");
      String name = sc.nextLine();
      if ( ps != null ) ps.print("Enter the grade: ");
      int grade = sc.nextInt(); //sc.nextLine();
      return new Student(name,grade);
   }
//-----------------------------------------------------------------------
} // end class Student
/////////////////////////////////////////////////////////////////////////



Error message when I run Student1.txt:
java.lang.NullPointerException
	at Section.nPassing(Hw07.java:100)
	at Hw07.main(Hw07.java:28)



Oddly, the above input file actually prints out the correct output, as I would expect, it just throws this error still.

Error message when I run Student3.txt and Student10.txt:

java.util.InputMismatchException
	at java.util.Scanner.throwFor(Scanner.java:840)
	at java.util.Scanner.next(Scanner.java:1461)
	at java.util.Scanner.nextInt(Scanner.java:2091)
	at java.util.Scanner.nextInt(Scanner.java:2050)
	at Student.read(Hw07.java:150)
	at Section.<init>(Hw07.java:53)
	at Hw07.main(Hw07.java:19)



Also, I realize the formatting is completely ridiculous, but that's the way my professor wants it and apparently he knows best. I realize it's late. I've been trying to debug my code for hours now and Dreamincode is really my last resort. Maybe someone can point me in the right direction here. Any help will be greatly appreciated.

For the input files, there are 3 different ones.

Student1.txt
Kevin
75

Student3.txt
Kevin
53
Susan
82
Nguyen
75

Student10.txt
Sally
100
Rico
82
Sue
95
Bill
43
Rob
67
Meng
94
Pam
82
Harvey
72
Rich
45
Debra
88

Is This A Good Question/Topic? 0
  • +

Replies To: InputMismatchException but I'm sure the data is correct.

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: InputMismatchException but I'm sure the data is correct.

Posted 14 October 2011 - 04:32 AM

Your first case is because of this loop:
for(int i = 0; i < a.length; i++)
the loop should only go to the used not the complete array.

Your next problem is because of this line
 int grade = sc.nextInt(); //sc.nextLine();
Than sc.nextLine() should not be commented, it should be there to move pointer to the next line before reading another student info
Was This Post Helpful? 0
  • +
  • -

#3 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: InputMismatchException but I'm sure the data is correct.

Posted 14 October 2011 - 04:44 AM

View Postsmohd, on 14 October 2011 - 04:32 AM, said:

Your first case is because of this loop:
for(int i = 0; i < a.length; i++)
the loop should only go to the used not the complete array.

Your next problem is because of this line
 int grade = sc.nextInt(); //sc.nextLine();
Than sc.nextLine() should not be commented, it should be there to move pointer to the next line before reading another student info

I'm not sure which of the loops you were referring to. Which method?

EDIT: Both is what you meant. My bad.

the only error I get now is this:

java.util.NoSuchElementException: No line found
	at java.util.Scanner.nextLine(Scanner.java:1516)
	at Student.read(Hw07.java:150)
	at Section.<init>(Hw07.java:53)
	at Hw07.main(Hw07.java:19)


Might this be related to the nextLine() call?

This post has been edited by zim1985: 14 October 2011 - 04:46 AM

Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: InputMismatchException but I'm sure the data is correct.

Posted 14 October 2011 - 05:11 AM

Now at which file this problem is happening? Because your class mates has the same code and was working with no such error.
Was This Post Helpful? 2
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,793
  • Joined: 27-December 08

Re: InputMismatchException but I'm sure the data is correct.

Posted 14 October 2011 - 06:48 AM

Post your revised code.
Was This Post Helpful? 0
  • +
  • -

#6 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: InputMismatchException but I'm sure the data is correct.

Posted 14 October 2011 - 11:07 AM

View Postsmohd, on 14 October 2011 - 05:11 AM, said:

Now at which file this problem is happening? Because your class mates has the same code and was working with no such error.

I was having the same errors at all the files, but now that I've seen the other code that is identical to what mine should be, I've found a couple of things to fix.

EDIT: So I fixed what I thought needed to be fixed...turns out I'm still getting the same error, regardless of the file I input, I always get this error at the sc.nextLine().


java.util.NoSuchElementException: No line found
	at java.util.Scanner.nextLine(Scanner.java:1516)
	at Student.read(Hw07.java:148)
	at Section.<init>(Hw07.java:51)
	at Hw07.main(Hw07.java:17)


This post has been edited by zim1985: 14 October 2011 - 11:17 AM

Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: InputMismatchException but I'm sure the data is correct.

Posted 14 October 2011 - 11:17 AM

So now you get it working?
Also I am confused now, because your friend(from other two threads with the same code posted) said the code comes from instructor! so why you changed it...... by the way is it working now?
Was This Post Helpful? 0
  • +
  • -

#8 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: InputMismatchException but I'm sure the data is correct.

Posted 14 October 2011 - 11:20 AM

Revised code:

//
// Solves CS 141, Fall 2011, Homework 7.
//
import java.io.*;
import java.util.Scanner;
/////////////////////////////////////////////////////////////////////////
class Hw07
{
//-----------------------------------------------------------------------
   public static void main (String [] args) throws Exception
   {
      Scanner kb = new Scanner(System.in);
 
      System.out.print("\nEnter name of a file of Students: ");
      String filename = kb.nextLine();
 
      Section sec = new Section(filename);
 
      System.out.println("\nIn forward order:");
      sec.print(System.out);
 
      System.out.println("\nThat Section contains " +
                         sec.howMany() +
                         " Student(s).");
 
      System.out.println("\nThat Section contains " +
                         sec.nPassing() +
                         " passing Student(s).");

      System.out.println("\nThe highest grade is " +
                         sec.highestGrade() + ".");
   }
//-----------------------------------------------------------------------
} // end class Hw07
/////////////////////////////////////////////////////////////////////////
class Section
{
   private Student [] a;
   private int used;
   private static final int INIT_SIZE = 20;
//-----------------------------------------------------------------------
   public Section ( String filename ) throws Exception
   {
      Scanner sc = new Scanner(new File(filename));
 
      a = new Student[INIT_SIZE];
      used = 0;
 
      while ( sc.hasNext() )
      {
         Student s = Student.read(null,sc);
 
         if ( used == a.length )
         {
            Student[] newA = new Student[2*a.length+1];
            for ( int i = 0 ; i < used ; i++ ) newA[i] = a[i];
            newA[used] = s;
            used++;
            a = newA;
         }
         else
         {
            a[used] = s;
            used++;
         }
      }
   }
//-----------------------------------------------------------------------
   public void print ( PrintStream ps )
   {
      for ( int i = 0 ; i < used ; i++ )
      {
         ps.println(a[i]);
      }
   }
//-----------------------------------------------------------------------
   public int howMany() { return used; }
//-----------------------------------------------------------------------
   public double average ()
   {
      int sum = 0;
 
      for ( int i = 0 ; i < used ; i++ )
      {
         Student s = a[i];
         sum += s.getGrade();
      }
 
      return 1.0 * sum / used;
   }
//-----------------------------------------------------------------------
   public int nPassing()
   {
      int passing = 0;
      for(int i = 0; i <= a.length; i++)
         if( a[i].getGrade() >= 60 ) passing++;
      return passing;
   }
//-----------------------------------------------------------------------
   public int highestGrade()
   {
      int high =a[0].getGrade();
      if (used == 0)
         throw new Error
            ("Attempt to find the maximum of an empty Section");
      else
      {
         for(int i=0; i<a.length;i++)
         {
            if (a[i].getGrade() > high)
               high = a[i].getGrade();
         }
      return high;
      }
   }
//-----------------------------------------------------------------------
} // end class Section
/////////////////////////////////////////////////////////////////////////
class Student
{
   private String name;
   private int grade;
//-----------------------------------------------------------------------
   public Student ( String name, int grade )
   {
      this.name = name;
      this.grade = grade;
   }
//-----------------------------------------------------------------------
   public String toString ()
   {
      return name + " (" + grade + ")";
   }
//-----------------------------------------------------------------------
   public String getName() { return name; }
//-----------------------------------------------------------------------
   public int getGrade() { return grade; }
//-----------------------------------------------------------------------
   public void setGrade( int newGrade ) { grade = newGrade; }
//-----------------------------------------------------------------------
   public static Student read ( PrintStream ps, Scanner sc )
   {
      if ( ps != null ) ps.println("Reading a Student record ...");
      if ( ps != null ) ps.print("Enter the name: ");
      String name = sc.nextLine();
      if ( ps != null ) ps.print("Enter the grade: ");
      int grade = sc.nextInt(); sc.nextLine();
      return new Student(name,grade);
   }
//-----------------------------------------------------------------------
} // end class Student
/////////////////////////////////////////////////////////////////////////


Was This Post Helpful? 0
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: InputMismatchException but I'm sure the data is correct.

Posted 14 October 2011 - 11:23 AM

Glad you figure it out....
Was This Post Helpful? 0
  • +
  • -

#10 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: InputMismatchException but I'm sure the data is correct.

Posted 14 October 2011 - 11:28 AM

View Postsmohd, on 14 October 2011 - 11:23 AM, said:

Glad you figure it out....

No...I thought I did, but the code the other guy had was exactly the same as mine and it doesn't run correctly. I literally copied and pasted it and made sure the files were exactly the same. Something is just wrong here.
Was This Post Helpful? 0
  • +
  • -

#11 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: InputMismatchException but I'm sure the data is correct.

Posted 14 October 2011 - 11:32 AM

What error are you getting buddy?
Was This Post Helpful? 0
  • +
  • -

#12 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: InputMismatchException but I'm sure the data is correct.

Posted 14 October 2011 - 11:36 AM

View PostFuzzyness, on 14 October 2011 - 11:32 AM, said:

What error are you getting buddy?

java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1516)
    at Student.read(Hw07.java:148)
    at Section.<init>(Hw07.java:51)
    at Hw07.main(Hw07.java:17)



It persists across all my files. I'm not sure what's the issue...
Was This Post Helpful? 0
  • +
  • -

#13 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: InputMismatchException but I'm sure the data is correct.

Posted 14 October 2011 - 11:42 AM

Instead of throwing an exception on those Constructors, in the Section class try putting it in an try catch statement.
try{
   /*
    * All your code ehre
    */
}catch(Exception e)
{ JOptionpane.shoewMessageDialog(null, e.printStackTrace());}


Was This Post Helpful? 0
  • +
  • -

#14 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: InputMismatchException but I'm sure the data is correct.

Posted 14 October 2011 - 11:44 AM

View PostFuzzyness, on 14 October 2011 - 11:42 AM, said:

Instead of throwing an exception on those Constructors, in the Section class try putting it in an try catch statement.
try{
   /*
    * All your code ehre
    */
}catch(Exception e)
{ JOptionpane.shoewMessageDialog(null, e.printStackTrace());}


My professor won't let us use try-catch until we actually learn it in class, regardless of the fact that I know how to use it fine.
Was This Post Helpful? 0
  • +
  • -

#15 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: InputMismatchException but I'm sure the data is correct.

Posted 14 October 2011 - 11:50 AM

Alright, have you tried changing your throw Exception to throw FileNotFoundException
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2