亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java 程序永遠不會完成打印計數

Java 程序永遠不會完成打印計數

蝴蝶不菲 2023-06-04 16:57:01
我正在嘗試讀取文件并計算文件中的行數。每行都有一個電影標題。我注釋掉了增加計數的行。如果我取消注釋計數的遞增,程序就會掛起并且永遠不會打印計數。我究竟做錯了什么?如果我只打印 while 循環中的每一行,它就可以正常工作。但如果我嘗試增加計數則不會。謝謝。import java.io.FileNotFoundException;import java.util.Scanner;import java.io.File;public class GuessTheMovie {    public static void main(String[] args) throws Exception {        try {            File file = new File("movies.txt");            Scanner scanner = new Scanner(file);            // open movies file and count the number of titles in the file            int count = 0;            while (scanner.hasNextLine()) {                //count += 1;                System.out.println(scanner.nextLine());            }            System.out.println(count);            // create String array of movies such that the size is equal to the number of movies in file.            //String [] movies = []        } catch (Exception e) {            System.out.println("Could not find file.");        }    }}
查看完整描述

4 回答

?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

假設您不知道格式如何,movies.txt我建議您執行以下操作:

  • 將內容附加到StringBuilder

  • String用的內容做一個StringBuilder

  • 得到想要的數據

這是一個小演示,對我有用。

//make sure you are using the "relative path" to find the movies.txt file(as follows)

?try (BufferedReader br = new BufferedReader(new FileReader("./movies.txt"))) {

? ? ? ?StringBuilder sb = new StringBuilder();

? ? ? ?String responseLine = null;

? ? ? ?while ((responseLine = br.readLine()) != null) {

? ? ? ? ? ? ? ? ? ? ?sb.append(responseLine.trim());

? ? ? ? }

? ? ? ? System.out.println(sb);

? ? ? ?//By now you should have all movies & titles to your StringBuilder instance then


? ? ? ? String temp = sb.toString();

? ? ? ? String movies[] = temp.split(" ");//split the string at "spaces"

? ? ? ? System.out.println("First Element: "+movies[0]);

? ? ? ? System.out.println("Second Element: "+movies[1]);

? ? ? ? System.out.println("Third Element: "+movies[2]);

? ? ? ? } catch (Exception e) {

? ? ? ? System.out.println("Could not find file.");

?}

這是我的內容movies.txt

http://img1.sycdn.imooc.com/647c51f80001923901220057.jpg

注意:String按照您想要的方式拆分內容

  • 如果你正確分割它movies.length會給你電影的數量

輸出

http://img1.sycdn.imooc.com/647c5204000162d501830043.jpg

查看完整回答
反對 回復 2023-06-04
?
手掌心

TA貢獻1942條經驗 獲得超3個贊

在這里,我為您準備了工作代碼。它正在從 filereader 讀取文件,而 st 是等于每一行的參數。在循環中,它將遍歷每一行并計算行數。


import java.io.FileNotFoundException;

import java.io.FileReader;

import java.util.Scanner;

import java.io.BufferedReader;

import java.io.File;


public class FileRead {

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

         File file = new File("C:\\Users\\Name\\Desktop\\test.txt");

         String st; 


         BufferedReader br = new BufferedReader(new FileReader(file)); 

         int count = 0;

         while ((st = br.readLine()) != null) {


             count++;

         }

         System.out.println(count);

    }


}


查看完整回答
反對 回復 2023-06-04
?
弒天下

TA貢獻1818條經驗 獲得超8個贊

試試下面的代碼


import java.io.FileNotFoundException;

import java.util.Scanner;

import java.io.File;


public class GuessTheMovie {


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

        try {

            File file = new File("movies.txt");

            Scanner scanner = new Scanner(file);


            // open movies file and count the number of titles in the file

            int count = 0;

            while (scanner.hasNextLine()) {

                count += 1;

                scanner.nextLine();

            }

            System.out.println(count);

            // create String array of movies such that the size is equal to the number of movies in file.

            //String [] movies = []

        } catch (Exception e) {

            System.out.println("Could not find file.");

        }

    }

}


查看完整回答
反對 回復 2023-06-04
?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

如果您想嘗試,可以使用更簡單的方式與文件交互:

List<String> lines = Files.readAllLines(Paths.get(path), Charset.defaultCharset());

之后,您可以與準備好的所有行列表進行交互,或者使用lines.size().


查看完整回答
反對 回復 2023-06-04
  • 4 回答
  • 0 關注
  • 203 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號