第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Java 程序永遠(yuǎn)不會(huì)完成打印計(jì)數(shù)

Java 程序永遠(yuǎn)不會(huì)完成打印計(jì)數(shù)

蝴蝶不菲 2023-06-04 16:57:01
我正在嘗試讀取文件并計(jì)算文件中的行數(shù)。每行都有一個(gè)電影標(biāo)題。我注釋掉了增加計(jì)數(shù)的行。如果我取消注釋計(jì)數(shù)的遞增,程序就會(huì)掛起并且永遠(yuǎn)不會(huì)打印計(jì)數(shù)。我究竟做錯(cuò)了什么?如果我只打印 while 循環(huán)中的每一行,它就可以正常工作。但如果我嘗試增加計(jì)數(shù)則不會(huì)。謝謝。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 回答

?
萬(wàn)千封印

TA貢獻(xiàn)1891條經(jīng)驗(yàn) 獲得超3個(gè)贊

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

  • 將內(nèi)容附加到StringBuilder

  • String用的內(nèi)容做一個(gè)StringBuilder

  • 得到想要的數(shù)據(jù)

這是一個(gè)小演示,對(duì)我有用。

//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.");

?}

這是我的內(nèi)容movies.txt

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

注意:String按照您想要的方式拆分內(nèi)容

  • 如果你正確分割它movies.length會(huì)給你電影的數(shù)量

輸出

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

查看完整回答
反對(duì) 回復(fù) 2023-06-04
?
手掌心

TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個(gè)贊

在這里,我為您準(zhǔn)備了工作代碼。它正在從 filereader 讀取文件,而 st 是等于每一行的參數(shù)。在循環(huán)中,它將遍歷每一行并計(jì)算行數(shù)。


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);

    }


}


查看完整回答
反對(duì) 回復(fù) 2023-06-04
?
弒天下

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個(gè)贊

試試下面的代碼


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.");

        }

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-06-04
?
肥皂起泡泡

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊

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

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

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


查看完整回答
反對(duì) 回復(fù) 2023-06-04
  • 4 回答
  • 0 關(guān)注
  • 200 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)