Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem on reading file in Java program

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem on reading file in Java program

    Hey all,

    I am trying to import a file of information into my java program and I am having a difficult problem with the loop to read the file.

    The file I am importing has the following structure.

    Name
    number1
    number2
    number3
    number4
    number5
    Name
    number1
    number2
    number3
    number4
    number5
    Name
    .
    .
    .

    You get the picture,

    When the imput file has only one set of data (i.e, Nmae, number1number2, number3) the code is perfect. When I use a loop using the do (blah) while file.hasnext(). This works fine.

    BUT BUT BUT.

    When I add a second set of data I get an error when I import the file. The program compiles correctly but doesn'y read the second set of data. Is there an issue with changing the data ina string or somthing?

    Please see my code below.

    import java.util.*;
    import java.io.*;
     
    public class Marks
    {
       public static void main(String[] args)
       throws IOException
      {
          boolean nonExamHurdlePass;
          int numberOfFails = 0;
          String StudentName;
          double progressMark;
          int assignmentOneMark;
          double assignmentTwoMark;
          int labMark;
          int writtenMark;
          double totalMarks;
          double topMark = 0;
          double bottomMark =100;
     
     
          //ask user for file name
          System.out.println("Please entrer the name of the file containing the students marks details:");
          Scanner kb = new Scanner(System.in);
          String filename = kb.nextLine();
          File sourceFile = new File(filename);
          Scanner fileScanner = new Scanner(sourceFile);
     
          do
          {
          //reads the file
          StudentName = fileScanner.nextLine();;
          progressMark = fileScanner.nextDouble();
          assignmentOneMark = fileScanner.nextInt();
          assignmentTwoMark = fileScanner.nextDouble();
          labMark = fileScanner.nextInt();
          writtenMark = fileScanner.nextInt();
     
          //calculate toal marks
          totalMarks = progressMark + assignmentOneMark + assignmentTwoMark + labMark + writtenMark;
          //Calculate top marks
          if(totalMarks > topMark)
          {
             topMark = totalMarks;
          }
          if(totalMarks < bottomMark)
          {
             bottomMark = totalMarks;
          }
     
          //System.out.println(totalMarks);
     
     
          //see if student passed non exam hurdle and store
          if((progressMark < 2.5 && assignmentOneMark < 5) && (assignmentTwoMark < 5 && labMark < 8))
          {
             nonExamHurdlePass = true;
          }
          else
          {
             nonExamHurdlePass = false;
          }
     
          //calculate results for studnet
     
          if(totalMarks >= 50 && nonExamHurdlePass)
          {
             System.out.println(StudentName + " gets a Pass! Well Done");
          }
          if(totalMarks < 50)
          {
             System.out.println(StudentName + " failed the course. Oh No.");
             numberOfFails++;
          }
          if(totalMarks >= 50 && !nonExamHurdlePass)
          {
             System.out.println(StudentName + " gets a SAH-A. Give assignment again");
          }
          if(totalMarks >=50 && writtenMark <45)
          {
             System.out.println(StudentName + " gets a SAH-E. Give exam again");
          }
          System.out.println("");
     
          }
          while (fileScanner.hasNextLine());
     
     
     
          System.out.println("The top mark in the class is " + topMark);
          System.out.println("The bottom mark in the class is " + bottomMark);
          System.out.println("The Number of Students that failed the course is " + numberOfFails);
     
     
          ///System.out.println(writtenMark);
     
       }
    }


    Has anybody got any ideas? The error actually comes when running the program?

    Thanks!

    Also, yes this is for an assignment so if anybody out there is in my course please only use this as a guide and not copy it. That would only create headaches for everybody including yourself.
    Last edited by nathanernest; April 12th, 2009 at 08:28 AM.


  2. #2
    Junior Member
    Join Date
    Apr 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having a problem reading a file :(

    Just quickly, I think the issue is when the file stops reading the first time. The error is the following:

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:857)
    at java.util.Scanner.next(Scanner.java:1478)
    at java.util.Scanner.nextDouble(Scanner.java:2404)
    at Marks.main(Marks.java:33)

    This is the same error as if the file itself was't even there so i guess its somthing to do with the varible types?

    Wahhh!! :-( :-(

    Also sorry about the look of the code, the forums didnt take my indents
    Last edited by nathanernest; April 12th, 2009 at 07:58 AM.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Having a problem reading a file :(

    This looks to me as if the second set of data is in a different format or not in exactly the same format?

    The scanner is looking for nextDouble which it can't find. Thats what is throwing the error...

    Could you please attach both data files so I can compile your code and correct the problem?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. [SOLVED] Program to read from created file
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 7th, 2009, 03:51 AM
  2. Problem with overwriting a file using IO package
    By marksquall in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: March 28th, 2009, 06:37 AM
  3. [SOLVED] Problem in reading a file from java class
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: March 23rd, 2009, 09:31 AM
  4. Replies: 1
    Last Post: February 28th, 2009, 09:05 PM
  5. Uploaded file getting saved to Bin directory
    By jazz2k8 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 13th, 2008, 01:59 PM