扫描仪返回一个元素未找到错误,但没有hasNext阻止?

时间:2013-12-28 18:20:49

标签: java csv

所以我有一个正在制作摔跤支架的程序,我让它从CSV文件中获取输入。

Alex,Brown,School1,140,10,2,12,10
Bill,Bob,School2,140,8,4,9,3
Ted,Red,School1,140,12,5,5,9
Randy,Ted,School2,140,17,7,10,8
Sam,Mark,School2,140,21,4,10,8
Noel,Frank,School2,140,7,8,10,8

然而,当我让扫描仪通过时,它会完成所有操作,并且由于某种原因,调试输出在名字之间打印出零,然后不会停止,即使while循环设置为停止当hasNext()方法返回false时。

我的代码:

import java.util.ArrayList;
import java.util.Collections;
import java.lang.Object;

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

public class Bracket_Creator{


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

        String first_name = "";
        String last_name = "";
        String school = "";
        int weight = 0;
        int confer_win = 0;
        int confer_loss = 0;
        int overall_wins = 0;
        int overall_loss = 0;
        ArrayList<Wrestler> list = new ArrayList<Wrestler>();
        int i = 0;
        boolean flag = true;

        //new scanner instance
        Scanner scanner = new Scanner(new File("wrestlers.csv"));

        //the seperator for the file
        scanner.useDelimiter(",");
        while(scanner.hasNext()){
            first_name = scanner.next();
            last_name = scanner.next();
            school = scanner.next();
            weight = scanner.nextInt();
            confer_win = scanner.nextInt();
            confer_loss = scanner.nextInt();
            overall_wins = scanner.nextInt();
            overall_loss = scanner.nextInt();
            list.add(new Wrestler(first_name,last_name,school,weight,
                confer_win,confer_loss,overall_wins,overall_loss));

            System.out.println((list.get(i).get_first_name()));
            i++;


        }//end while
        scanner.close();



        //make the percentages for the wrestlers
        for (i = 0; i < list.size(); i++){
            (list.get(i)).determine_conf_percentage((list.get(i)).get_confer_wins(), (list.get(i)).get_confer_losses());
            (list.get(i)).determine_overall_percentage((list.get(i)).get_overall_wins(), (list.get(i)).get_overall_losses());

        }//end for loop
        //set rank
        while ( flag )
        {
            flag= false;    //set flag to false awaiting a possible swap
            for( i=0;  i < list.size() -1;  i++ )
            {
                   if ((list.get(i)).get_confer_percentage() < (list.get(i+1)).get_confer_percentage() )  
                   {

                        Collections.swap(list, i, i+1);
                        flag = true;

                    }//end if
            }//end for 

        }//end while

        //set rank value
        for (i = 0; i< list.size(); i++){
            (list.get(i)).set_rank(i + 1);
        }//end for loop

        for (i = 0; i< list.size(); i++){
            System.out.println((list.get(i)).get_first_name());
        }
            System.out.println("");

        /*
        *
        *
        *Create the Bracket!
        *
        *
        */
        int j = 1; //J here is for the end of the list, every time you go through
        //add one to J in the loop to keep on coming in from the other end of
        //the list
        for (i = 0; i< (list.size()/2); i++){
            if (i == 0) {//first pass thru the list
                System.out.println((list.get(i)).get_first_name());
                System.out.println((list.get(list.size()-j)).get_first_name());
                System.out.println();
                j++;
                }//end if

            else{
                System.out.println((list.get(i)).get_first_name());
                System.out.println((list.get(list.size()-j)).get_first_name());
                System.out.println();
                j++;

                }//end else

        }//end for




    }//end main


}//end bracket_creator class

这是Wrestler类代码,供参考:

public class Wrestler {

   private String first_name = "";
   private String last_name = "";
   private String school = "";
   private int weight_class = 0;
   private int confer_wins = 0;
   private int confer_losses = 0;
   private int overall_wins = 0;
   private int overall_losses = 0;
   private double confer_percentage = 0;
   private double overall_percentage = 0;
   private int rank;

   /* Constructors! */

   public Wrestler(String init_first_name, String init_last_name, 
   String init_school, int init_weight_class, int init_confer_wins, 
   int init_confer_losses, int init_overall_wins, int init_overall_losses) {

      first_name = init_first_name;
      last_name = init_last_name;
      school = init_school;
      weight_class = init_weight_class;
      confer_wins = init_overall_wins;
      confer_losses = init_confer_losses;
      overall_wins = init_overall_wins;
      overall_losses = init_overall_losses;
      confer_percentage = 0;
      overall_percentage = 0;
      rank = 0;


   }//end wresler constructor




   /*
   *
   *
   *  setters 
   *
   */

    public void set_first_name(String new_first_name){

      first_name = new_first_name; 

    }//end first name setter

    public void set_last_name(String new_last_name){

      last_name = new_last_name; 

    }//end last name setter

    public void set_school(String new_school){

      school = new_school;

    }//end school setter

    public void set_weight_class(int new_weight_class){

      weight_class = new_weight_class; 

    }//end weight class setter

    public void set_conf_wins(int new_conf_wins){

      confer_wins = new_conf_wins; 

    }//end conference wins setter

    public void set_conf_losses(int new_conf_losses){

      confer_losses = new_conf_losses; 

    }//end conference losses setter

    public void set_over_wins(int new_over_wins){

      overall_wins = new_over_wins; 

    }//end over wins setter

    public void set_over_losses(int new_over_losses){

      overall_losses = new_over_losses; 

    }//end over losses setter

    public void set_confer_percentage(double new_confer_percentage){

       confer_percentage = new_confer_percentage;

    }//end conference percentage setter

    public void set_overall_percentage(double new_overall_percentage){

       overall_percentage = new_overall_percentage;

    }//end overall percentage setter

    public void set_rank(int new_rank){

      rank = new_rank; 

    }//end rank setter

    /*
    *
    *
    *  Getters
    *
    */

    public String get_first_name(){

        return first_name;
    }//end first name

    public String get_last_name(){

        return first_name;
    } //end last name

    public String get_school(){
        return school;
    }

    public int get_weight_class(){

        return weight_class;
    }//end weight class

    public int get_confer_wins(){

        return confer_wins;
    }

    public int get_confer_losses(){

        return confer_losses;
    }

    public int get_overall_wins(){
        return overall_wins;
    }

    public int get_overall_losses(){
        return overall_losses;
    }

    public double get_confer_percentage(){
        return confer_percentage;
    }

    public double get_overall_percentage(){
        return overall_percentage;
    }

    public int get_rank(){
        return rank;
    }

    public void determine_conf_percentage(int confer_wins, int confer_losses){

      double determined_percentage = 0;

      if (confer_losses + confer_wins < 4){

          set_confer_percentage(determined_percentage);
      }
      else {
        //note: the 1.0 converts the ints to doubles.
        determined_percentage =  100 * ((1.0*confer_losses) / ((1.0*confer_wins) + (1.0 * confer_losses)));
        set_confer_percentage(determined_percentage);
      }     

    }// end determine_conf_percetnage

    public void determine_overall_percentage(int overall_wins, int overall_losses){

      double determined_percentage = 0;

      if (overall_losses + overall_wins < 4){

          set_overall_percentage(determined_percentage);
      }
      else {
        //note: the 1.0 converts the ints to doubles.
        determined_percentage = 100 * ((1.0*overall_losses) / ((1.0*overall_wins) + (1.0 * overall_losses)));
        set_overall_percentage(determined_percentage);
      }     

    }// end determine_conf_percetnage





}//end Wrestlerpublic class Wrestler {

   private String first_name = "";
   private String last_name = "";
   private String school = "";
   private int weight_class = 0;
   private int confer_wins = 0;
   private int confer_losses = 0;
   private int overall_wins = 0;
   private int overall_losses = 0;
   private double confer_percentage = 0;
   private double overall_percentage = 0;
   private int rank;

   /* Constructors! */

   public Wrestler(String init_first_name, String init_last_name, 
   String init_school, int init_weight_class, int init_confer_wins, 
   int init_confer_losses, int init_overall_wins, int init_overall_losses) {

      first_name = init_first_name;
      last_name = init_last_name;
      school = init_school;
      weight_class = init_weight_class;
      confer_wins = init_overall_wins;
      confer_losses = init_confer_losses;
      overall_wins = init_overall_wins;
      overall_losses = init_overall_losses;
      confer_percentage = 0;
      overall_percentage = 0;
      rank = 0;


   }//end wresler constructor




   /*
   *
   *
   *  setters 
   *
   */

    public void set_first_name(String new_first_name){

      first_name = new_first_name; 

    }//end first name setter

    public void set_last_name(String new_last_name){

      last_name = new_last_name; 

    }//end last name setter

    public void set_school(String new_school){

      school = new_school;

    }//end school setter

    public void set_weight_class(int new_weight_class){

      weight_class = new_weight_class; 

    }//end weight class setter

    public void set_conf_wins(int new_conf_wins){

      confer_wins = new_conf_wins; 

    }//end conference wins setter

    public void set_conf_losses(int new_conf_losses){

      confer_losses = new_conf_losses; 

    }//end conference losses setter

    public void set_over_wins(int new_over_wins){

      overall_wins = new_over_wins; 

    }//end over wins setter

    public void set_over_losses(int new_over_losses){

      overall_losses = new_over_losses; 

    }//end over losses setter

    public void set_confer_percentage(double new_confer_percentage){

       confer_percentage = new_confer_percentage;

    }//end conference percentage setter

    public void set_overall_percentage(double new_overall_percentage){

       overall_percentage = new_overall_percentage;

    }//end overall percentage setter

    public void set_rank(int new_rank){

      rank = new_rank; 

    }//end rank setter

    /*
    *
    *
    *  Getters
    *
    */

    public String get_first_name(){

        return first_name;
    }//end first name

    public String get_last_name(){

        return first_name;
    } //end last name

    public String get_school(){
        return school;
    }

    public int get_weight_class(){

        return weight_class;
    }//end weight class

    public int get_confer_wins(){

        return confer_wins;
    }

    public int get_confer_losses(){

        return confer_losses;
    }

    public int get_overall_wins(){
        return overall_wins;
    }

    public int get_overall_losses(){
        return overall_losses;
    }

    public double get_confer_percentage(){
        return confer_percentage;
    }

    public double get_overall_percentage(){
        return overall_percentage;
    }

    public int get_rank(){
        return rank;
    }

    public void determine_conf_percentage(int confer_wins, int confer_losses){

      double determined_percentage = 0;

      if (confer_losses + confer_wins < 4){

          set_confer_percentage(determined_percentage);
      }
      else {
        //note: the 1.0 converts the ints to doubles.
        determined_percentage =  100 * ((1.0*confer_losses) / ((1.0*confer_wins) + (1.0 * confer_losses)));
        set_confer_percentage(determined_percentage);
      }     

    }// end determine_conf_percetnage

    public void determine_overall_percentage(int overall_wins, int overall_losses){

      double determined_percentage = 0;

      if (overall_losses + overall_wins < 4){

          set_overall_percentage(determined_percentage);
      }
      else {
        //note: the 1.0 converts the ints to doubles.
        determined_percentage = 100 * ((1.0*overall_losses) / ((1.0*overall_wins) + (1.0 * overall_losses)));
        set_overall_percentage(determined_percentage);
      }     

    }// end determine_conf_percetnage





}//end Wrestler

4 个答案:

答案 0 :(得分:0)

每次调用scanner.next()之前都需要检查hasNext() 所以你的     first_name = scanner.next()

语句永远不会失败,但在执行此语句后,您不会检查scanner.hasNext()

答案 1 :(得分:0)

您在csv

的末尾有一个额外的0
Alex,Brown,School1,140,10,2,12,10,0

这不应该被循环中的任何东西读取。

在第一次循环迭代中,它不会被读取,但在第二次循环时它将被

读取
first_name = scanner.next();

因为分隔符是,。这一直持续到你仍然有

的最后一次迭代
0

在缓冲区中,因此hasNext()将返回true。这将读取它

first_name = scanner.next();

然后

中没有任何内容
last_name = scanner.next();

所以抛出了异常。

你可以做的就是添加一个简单的

scanner.nextInt()

在循环结束时。

答案 2 :(得分:0)

这看起来像是问题

        //the seperator for the file
        scanner.useDelimiter(",");
        while(scanner.hasNext()){
            first_name = scanner.next();
            last_name = scanner.next();
            school = scanner.next();
            weight = scanner.nextInt();
            confer_win = scanner.nextInt();
            confer_loss = scanner.nextInt();
            overall_wins = scanner.nextInt();
            overall_loss = scanner.nextInt();
            list.add(new Wrestler(first_name,last_name,school,weight,
                confer_win,confer_loss,overall_wins,overall_loss));

            System.out.println((list.get(i).get_first_name()));
            i++;

hasNext()只会检查是否存在其他分隔符,而不是所有9个下一次扫描都能正常工作。

答案 3 :(得分:0)

我刚刚这样做了:

import java.util.ArrayList;
import java.util.Collections;
import java.lang.Object;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.BufferedReader;
import java.io.FileReader;  
import java.io.IOException;
import java.util.Scanner;

public class Bracket_Creator{


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

        String first_name = "";
        String last_name = "";
        String school = "";
        int weight = 0;
        int confer_win = 0;
        int confer_loss = 0;
        int overall_wins = 0;
        int overall_loss = 0;
        ArrayList<Wrestler> list = new ArrayList<Wrestler>();
        int i = 0;
        boolean flag = true;

        BufferedReader br = null;
        String line = "";
        String splitBy = ",";

        //new scanner instance

        try {
            br = new BufferedReader(new FileReader("wrestlers.csv"));
            while ((line = br.readLine()) != null){

                String[] attributes = line.split(splitBy);

                list.add(new Wrestler(attributes[0],attributes[1],attributes[2],
                    Integer.parseInt(attributes[3]),
                    Integer.parseInt(attributes[4]),
                    Integer.parseInt(attributes[5]),
                    Integer.parseInt(attributes[6]),
                    Integer.parseInt(attributes[7])));



            }//end while

        }//end try
        catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null){
                try {
                    br.close();
                } catch (IOException e){
                    e.printStackTrace();
                }
            }
        }

        //make the percentages for the wrestlers
        for (i = 0; i < list.size(); i++){
            (list.get(i)).determine_conf_percentage((list.get(i)).get_confer_wins(), (list.get(i)).get_confer_losses());
            (list.get(i)).determine_overall_percentage((list.get(i)).get_overall_wins(), (list.get(i)).get_overall_losses());

        }//end for loop
        //set rank
        while ( flag )
        {
            flag= false;    //set flag to false awaiting a possible swap
            for( i=0;  i < list.size() -1;  i++ )
            {
                   if ((list.get(i)).get_confer_percentage() < (list.get(i+1)).get_confer_percentage() )  
                   {

                        Collections.swap(list, i, i+1);
                        flag = true;

                    }//end if
            }//end for 

        }//end while

        //set rank value
        for (i = 0; i< list.size(); i++){
            (list.get(i)).set_rank(i + 1);
        }//end for loop

        for (i = 0; i< list.size(); i++){
            System.out.println((list.get(i)).get_first_name());
        }
            System.out.println("");

        /*
        *
        *
        *Create the Bracket!
        *
        *
        */
        int j = 1; //J here is for the end of the list, every time you go through
        //add one to J in the loop to keep on coming in from the other end of
        //the list
        for (i = 0; i< (list.size()/2); i++){
            if (i == 0) {//first pass thru the list
                System.out.println((list.get(i)).get_first_name());
                System.out.println((list.get(list.size()-j)).get_first_name());
                System.out.println();
                j++;
                }//end if

            else{
                System.out.println((list.get(i)).get_first_name());
                System.out.println((list.get(list.size()-j)).get_first_name());
                System.out.println();
                j++;

                }//end else

        }//end for




    }//end main


}//end bracket_creator class

工作了,我有一个异常处理程序。