【问题标题】:Extracting integers from a string and store them in separate variables in java with substring and lastIndexOf(" ") methods使用 substring 和 lastIndexOf(" ") 方法从字符串中提取整数并将它们存储在 java 中的单独变量中
【发布时间】:2018-08-18 17:53:29
【问题描述】:

我遇到了这个特定程序的问题。长话短说,这个程序应该做的是从文本文件中获取输入,在本例中是投球手的姓名和得分,并从中读取数据。我遇到的问题是尝试从文件中提取分数并将它们存储在单独的变量中。 `

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

public class lab5{
    public static void main (String args[]) throws IOException {

            String bowler;

            String name;


           int score1 = 0, score2 = 0, score3 = 0;
             int game = 0;
             int average = 0;

          //Scanner scan = new Scanner (new FileReader("bowl1.txt."));
            Scanner scan = new Scanner (new File ("bowl1.txt"));

               while (scan.hasNext()){
                   bowler = scan.nextLine();
                System.out.println("\n" + bowler);

                String charCheck = "";
                String indent = "";

               int count = bowler.length() - 1;


                 while (count <= bowler.length()){

                   indent = bowler.lastIndexOf(" ");



                  charCheck = bowler.substring(0,indent);

                  System.out.println(charCheck);

                 count++; 


                   score1 = Integer.parseInt(charCheck);
                     score2 = Integer.parseInt(charCheck);
                   score3 = Integer.parseInt(charCheck);                          
                 }//end while 

                    System.out.println(score1 + score2 + score3); 


         }//end fileScan while 

     }//end main

    }//end class

抱歉,我的代码有点草率,但我仍在学习一些 java 的基础知识。基本上,这里的想法是我需要使用 Substring 和 lastIndexOf(" ") 方法从文件中读取分数,然后使用 Integer.parseInt() 将它们转换为整数。

(文件名为“bowl1.txt”,这是上面的数据)

阿特基森,达伦 188 218 177
巴恩斯,克里斯 202 194 195
安东尼·多兰 203 193 225
伊斯顿,查尔斯 255 213 190

任何关于我所缺少的帮助或提示将不胜感激。

【问题讨论】:

标签: java file integer store


【解决方案1】:

当您调用bowler.substring(0, indent) 时,您实际上是对投球手的姓名进行子字符串化,因此它不包含分数。

如果您只想获取字符串的分数部分,请使用bowler.substring(indent + 1, bowler.length)。您仍然必须将其分解为 3 个不同的字符串并将它们中的每一个转换为整数。

无论如何,更好的方法是将每一行拆分为字符串数组,使用String#split 打印第一个字符串,因为它是投球手的名字,然后将最后 3 个转换为整数。您还可以使用Scanner#nextInt()Scanner#nextString() 方法,因为文件的结构是已知的。

【讨论】:

  • 谢谢。我自己会使用拆分方法,但无论出于何种原因,我都需要走更复杂的路线。
【解决方案2】:

引入了 Bowler 类来存储每一行​​数据。您现在可以使用投球手对象列表继续您的过程。 String.split() 给出了一个基于指定字符标记的字符串数组。此处已使用空间进行拆分。

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

public class lab5 {
    public static void main(String args[]) throws IOException {
        Scanner scan = new Scanner(new File("C:\\TEMP\\bowl1.txt"));
        String bowler;
        List<Bowler> bowlers = new ArrayList<Bowler>();
        while (scan.hasNext()) {
            bowler = scan.nextLine();
            if (!bowler.trim().equals("")) {
                System.out.println("\n" + bowler);
                String[] tokens = bowler.split(" ");
                Bowler b = new Bowler(tokens[0], tokens[1], Integer.parseInt(tokens[2]), Integer.parseInt(tokens[3]),
                        Integer.parseInt(tokens[4]));
                bowlers.add(b);
            }
        } // end fileScan while
        for (Bowler bow : bowlers) {
            System.out.println(bow.score1 + ", " + bow.score2 + ", " + bow.score3);
        }
    }// end main

}// end class

class Bowler {
    String firstName;
    String lastName;
    int score1;
    int score2;
    int score3;

    public Bowler(String firstName, String lastName, int score1, int score2, int score3) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.score1 = score1;
        this.score2 = score2;
        this.score3 = score3;
    }

}

【讨论】:

    【解决方案3】:

    好的,这是一个简单的方法......它只使用 lastIndexOf(" ") 和 substring("") 方法完成,如你所说。但是上面提到的Narayana Ganesh有很多有效和更简单的方法......这种方法也可以更多 如果你想精确的话......

    导入 java.io.; 导入 java.util.;

    public class test2
    {
    public static void main(String[] args) 
    {
        String bowler;
        String name;
        int score1 = 0, score2 = 0, score3 = 0;
        int game = 0;
        int average = 0;
    
        String filename = "bowl1.txt";
        File textfile = new File(filename);
    
        try{
    
            Scanner scan = new Scanner(textfile);
    
            while (scan.hasNextLine()){
    
                bowler = scan.nextLine();
                System.out.println("\n" + bowler);
                String text1 = "";
                String text2 = "";
                int index=0;
    
                int length = bowler.length();
    
                index = bowler.lastIndexOf(" ");
                text1 = bowler.substring(0,index);
                text2 = bowler.substring(index+1,length);
                score1 = Integer.parseInt(text2);
    
                length = text1.length();    
                index = text1.lastIndexOf(" ");
                text2 = text1.substring(index+1,length);
                text1 = text1.substring(0,index);
                score2 = Integer.parseInt(text2);
    
                length = text1.length();
                index = text1.lastIndexOf(" ");
                text2 = text1.substring(index+1,length);
                text1 = text1.substring(0,index);
                score3 = Integer.parseInt(text2);           
    
                System.out.println(score1);
                System.out.println(score2);
                System.out.println(score3);
                System.out.println("Total:"+(score1 + score2 + score3)); 
            }
        }catch(FileNotFoundException e){};
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-07
      • 2021-12-12
      • 1970-01-01
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多