簡體   English   中英

從Java中的txt文件讀取

[英]Reading from a txt file in Java

我現在正在上一門C課程,但是我也想用Java練習我的工作。 我無法從Java文件中讀取文件(我想從文件中獲取不同類型(雙精度,整數等)並將其存儲在某些變量中。我知道在C語言中,代碼如下:

int main(void) {

    FILE* fp;
    char name[29];
    int qty;
    char item[20];
    float price;

    fp= fopen("input.txt","r");
    if (fp == NULL) {
        printf("Couldn't open the file.");
        return;
    }

    while (fscanf(fp, "%s %d %s %.2f\n", name, &qty, item, &price) != NULL) {
        // do something
    }

    return 0;
}

但是在Java中呢? 到目前為止,我已經做到了,但是沒有用。 我確實得到了輸出,但是格式不是我想要的。

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

public class Main_Class {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner scan = null;
    try {
        scan = new Scanner(new File("input.txt"));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    while (scan.hasNextLine()) {
        String name = "";
        int quantity = 0;
        String item_name = "";
        double price = 0.0;

        if(scan.hasNext()) {
            name = scan.next();
        } 
        if(scan.hasNextInt()) {
            quantity = scan.nextInt();
        } 
        if(scan.hasNext()) {
            item_name = scan.next();
        } 
        if(scan.hasNextDouble()) {
            price = scan.nextDouble();
        }
        System.out.println(name + " " + quantity + " " + item_name + " " +    price);
    }
}

}

編輯:抱歉,我忘了包括txt文件的內容。 我希望我的輸出就是這樣。

Smith 3 Sweater $22.50
Reich 3 Umbrella $12.50
Smith 1 Microwave $230.00
Lazlo 1 Mirror $60.00
Flintstone 5 Plate $10.00
Lazlo 1 Fridge $1200.00
Stevenson 2 Chair $350.00
Smith 10 Candle $3.50
Stevenson 1 Table $500.00
Flintstone 5 Bowl $7.00
Stevenson 2 Clock $30.00
Lazlo 3 Vase $40.00
Stevenson 1 Couch $800.00

我的輸出(在Java中,由於它們很長,因此我將包括其中一些):

Smith 3 Sweater 0.0
$22.50 0 Reich 3.0
Umbrella 0 $12.50 0.0
Smith 1 Microwave 0.0
$230.00 0 Lazlo 1.0
Mirror 0 $60.00 0.0
Flintstone 5 Plate 0.0
$10.00 0 Lazlo 1.0
if(scan.hasNextDouble())
{
    price = scan.nextDouble();
}

更改為

 price = Double.parseDouble(scan.next().substring(1));

並改變

 System.out.println(name + " " + quantity + " " + item_name + " " +    price);

這個到

 System.out.println(name + " " + quantity + " " + item_name + " $" +    price);

如果您只關心打印數據,則不妨將所有內容作為字符串讀取,並將代碼簡化為:

while (scan.hasNextLine()) {
    String name, quantity, item_name, price;

    if(scan.hasNext()) {
        name = scan.next();
    }
    else ...

    if(scan.hasNext()) {
        quantity = scan.nextInt();
    } 
    else ...

    if(scan.hasNext()) {
        item_name = scan.next();
    }
    else ...

    if(scan.hasNext()) {
        price = scan.nextDouble();
    }
    else ...

    System.out.println(name + "\t" + quantity + "\t" + item_name + "t" +    price);
}

制表符'\\ t'使輸出看起來更好一點。 在其他位置,可以設置默認值,以防萬一。

如果您實際上關心某個東西是int還是double,則可以使用parse方法,即

int int_quantity = Integer.parseInt( quantity );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM