3 Replies - 110 Views - Last Post: 05 February 2012 - 03:49 AM Rate Topic: -----

Topic Sponsor:

#1 Roshanx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-February 12

HELLO GUYS! I am getting a problem on files in java.

Posted 05 February 2012 - 01:43 AM

BELOW HAVE ATTACHED 2 FILES FOR JAVA.ON RUNNING THE SECOND ONE, I HAVE AM GETTING THE ERROR BELOW.1)FILE TO BE CREATED IN JAVA

INPUT FOR READING(NEW FILE)
import java.util.*;
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.Scanner;

public class tutorialOutput {
	private static  Formatter outfile;
	private static  Scanner sn;
	
	public static void main(String[] args){
		try{
			outfile = new Formatter("student.txt");
		}
		catch (SecurityException se) {
			System.out.println("You do have access to this file");
			System.exit(1);
		}
		catch (FileNotFoundException fnfe) {
			System.out.println("Error Creating File");
			System.exit(1);
		}
		sn=new Scanner(System.in);
		
		
		System.out.println("Enter number of students:");
		
		int num=sn.nextInt();
		String dummy = sn.nextLine();
		for (int i=0;i<num;i++){
			
			System.out.println("Enter first name:");
			String fname=sn.nextLine();
			
			System.out.println("Enter last name:");
			String lname=sn.nextLine();
			
			System.out.println("Enter gender:");
			String gender=sn.nextLine();
			
			System.out.println("Entermarks in Physics:");
			int Physics=sn.nextInt();
			
			System.out.println("Enter marks in Chemistry:");
			int Chemistry=sn.nextInt();
			
			System.out.println("Enter marks in Maths:");
			int Maths=sn.nextInt();
			
			System.out.println("Enter pnone number:");
			String phone=sn.next();
			outfile.format("%s %s %s %d %d %d %s\n", fname, lname, gender, Physics, Chemistry, Maths, phone);
		}
		sn.close();
		if (outfile!=null){
			outfile.close();
		}				
	}

}


import java.io.*;
import java.util.*;
public class tutorial {

	/**
	 * @param args
	 */
	private static Formatter outfile1 ;
	private static Formatter outfile2 ;
	private static Scanner kb;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try{
	kb=new Scanner("student.txt");
		}
		
			catch (SecurityException se){
			System.out.println ("…..");
			}
		int Physics=0, Chemistry = 0, Maths=0;  
		int averageMarks=0; 
		int passCount=0;
		String phone="",gender="",fname = "",lname="",dummy="";
		
		
	
		try{
		outfile1=new Formatter("passstudent.txt");
		}
		catch (FileNotFoundException fnfe){
			System.out.println("…..");
			System.exit(1);
			}
			catch (SecurityException se){
			System.out.println ("…..");
			System.exit(1);
			}
		
		try{
		outfile2=new Formatter("Failed.txt");
		}
		catch (FileNotFoundException fnfe){
			System.out.println("sds");
			System.exit(1);
			}
			catch (SecurityException se){
				System.out.println ("…..");
				System.exit(1);
			}
		
		while(kb.hasNext()){
			Physics=kb.nextInt();
			Chemistry=kb.nextInt();
			Maths=kb.nextInt();
			gender=kb.next();
			fname=kb.next();
			lname=kb.next();
			phone=kb.next();
			averageMarks=kb.nextInt();
			dummy=kb.next();
		
			
			if (Physics>=40){
				passCount++;
			}
			if (Chemistry>=40){
				passCount++;
			}
			if (Maths>=40){
				passCount++;
			}
		}

			
			if (passCount==3){
				averageMarks=(Physics+Chemistry+Maths)/3;
				outfile1.format("%s %s %s %s %d",fname, lname,gender, phone, averageMarks);
			}
			else{
				outfile2.format("%s %s %s %s %d",fname, lname,gender, phone, passCount);
			}
			
			
	}
}




ERROR:
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at tutorial.main(tutorial.java:53)



Mentor Edit: Made another edit to remove duplicate code and put classes in different code tags from errors

This post has been edited by smohd: 05 February 2012 - 03:40 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: HELLO GUYS! I am getting a problem on files in java.

#2 no2pencil  Icon User is online

  • 2 girls, 1 club
  • member icon

Reputation: 3061
  • View blog
  • Posts: 22,962
  • Joined: 10-May 07

Re: HELLO GUYS! I am getting a problem on files in java.

Posted 05 February 2012 - 01:46 AM

Moved from 'Other Languages' to Java, & added code tags.
Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • Ready for water skiing!
  • member icon

Reputation: 1067
  • View blog
  • Posts: 2,701
  • Joined: 10-September 10

Re: HELLO GUYS! I am getting a problem on files in java.

Posted 05 February 2012 - 02:55 AM

The contents of the data file do not match the program's calls to read the file, either in type or quantity - probably quantity. Can you show the contents of the data file? Just a couple lines of the file are necessary.
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: HELLO GUYS! I am getting a problem on files in java.

Posted 05 February 2012 - 03:49 AM

Adding to comments, look at this peace of code:
		while(kb.hasNext()){
			Physics=kb.nextInt();
			Chemistry=kb.nextInt();
			Maths=kb.nextInt();
			gender=kb.next();
			fname=kb.next();
			lname=kb.next();
			phone=kb.next();
			averageMarks=kb.nextInt();
			dummy=kb.next();

You look if the source has next token and then read a lot of tokens before checking again!! This is a bad practice.
Also your problem is that you are trying to read a token as integer using nextInt() which is not an int. That is why you get that error message. May be, as said, we can see the content of the file and compare your reading if it has matching tokens.

This post has been edited by smohd: 05 February 2012 - 03:52 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1