1

keep on getting error 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)
File order: string string int int

import java.util.Scanner; 
import java.io.*;

class PlayerStats 
{       
    public String name;   
    public String team;   
    public int games_ply; 
    public int goals_mde;
}

public class Program3
{
    public static void main(String[] args)
    {    

    PlayerStats[] players = new PlayerStats[100];
    int nPlayers;
    int opt;
    Scanner in = new Scanner (System.in
    nPlayers = loadPlayers (players);
    } 

    private static int loadPlayers (PlayerStats[] players)
    {   
        int nPlayers = 0;   
        try
        {   
        File file = new File ("/temp/Program3/Player.txt");
        Scanner inFile = new Scanner (file);
        do
        {
                players[ nPlayers ] = new PlayerStats();
            players[ nPlayers ].name = inFile.next();
            players[ nPlayers ].team = inFile.next();
            players[ nPlayers ].games_ply = inFile.nextInt();
            players[ nPlayers ].goals_mde = inFile.nextInt();
            ++nPlayers;
        } while ( players [nPlayers-1].goals_mde != 0);
    --nPlayers;
       }
       catch (IOException ioe)
       {
           System.out.print("\n\n\t\tFile access error!");
       nPlayers = 0;
       }
       return nPlayers;
    }
}
3
  • It looks like one of those ints is not really an int. Commented Sep 24, 2013 at 8:51
  • Can you provide file content? Commented Sep 24, 2013 at 8:57
  • file content - string string int int - example: LebronJames MiamiaHeat 88 356 Commented Sep 24, 2013 at 16:01

2 Answers 2

0

I guess you should use ObjectOutputStream to write your serialized Players object to the file, and use ObjectInputStream(new FileInputStream(new File(players.txt))).readObject to read the object, and don't forget to cast it to Players

Sign up to request clarification or add additional context in comments.

Comments

0

check whether your string have spaces or not, if yes then your code won't work as .next() will read till spaces

then try this

String line = inFile.nextLine();
String [] tokens = line.split("\s+");
players[ nPlayers ].name = tokens[0];
players[ nPlayers ].team = tokens[1];
players[ nPlayers ].games_ply = Integer.parseInt(tokens[2]);
players[ nPlayers ].goals_mde = Integer.parseInt(tokens[3]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.