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

New Topic/Question
Reply



MultiQuote





|