0
 package project1;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;




    public class Project1 {


        public static void main(String[] args) throws FileNotFoundException{

            //Declaring arrays
            String [] teams = new String [50];
            int [] runs = new int [50];

            //calling class lastvalue where -1 is stored
            lastValue value = new lastValue();
            int count = value.value;

            //instantiating input file
               Scanner input = new Scanner(new File("C:\\Users\\andy_\\Documents\\NetBeansProjects\\project1\\baseballteams.txt"));


             //while loop
        while (input.hasNext()) {
            count++;
           find next line
            teams[count] = input.nextLine();
            runs[count] = input.nextInt();
          }

        //print all teams
        for(int i=0; i<=teams.length; i++){
            value.value ++;
                System.out.println("The teams are:" + teams[count] + "The Runs" + runs[count]);
        }



        }


    }

I want to store the name of teams into the string array and the runs into the int array. However when I run the program it says throws mismatch error in console. I also want to displays the teams name

1 Answers1

0

Without seeing the file myself, your error is probably due to a buffering error. When the Scanner reads input.nextInt() it reads only to the end of a 0-9 digit and does not include the return character after. In you while loop try clearing out the buffer afterwards like so:

while (input.hasNext()) {
    count++;
    teams[count] = input.nextLine();
    runs[count] = input.nextInt();
    input.nextLine(); //clearing buffer
}

Here's a reference.

youassassin
  • 188
  • 9