English 中文(简体)
Java:从现有文本档案和产出到另一个档案的阅读分类
原标题:Java: Read integers from an existing text file and output to another file
  • 时间:2011-03-04 23:33:29
  •  标签:
  • java

www.un.org/Depts/DGACM/index_spanish.htm BAR CHART

撰写“ Java”方案,现称销售现有文本档案中5家商店的销售数字(推船)。 托拉斯新档案的编号和产出

该方案应显示比较每家仓库销售的条码。 通过展示星号,在条形图中创造每一条条条条。 每个星号应为销售100美元。

www.un.org/Depts/DGACM/index_spanish.htm 以下是从销售中读到的批量:

1000
1200
1800
800
1900

www.un.org/Depts/DGACM/index_spanish.htm 产出档案(仓库报告.txt)应照此办理:

Store 1: **********
Store 2: ************
Store 3: ******************
Store 4: ********
Store 5: *******************

我只想到第5章:Intro to Loops and files, of Intro to Java。 答案必须是极简单明了的<>。 我的案文中,我们倾向于如下,因此请不要使用这些词语:t[ ]、缓冲Writer、fstream、length、外印......。

因此,我必须使用最简单的法典,例如:

Scanner inputFile = new Scanner (file)
File inputFile = new File (sales.text)
PrintWriter outputFile = new PrintWriter (storeReport.txt)

我们可能使用 lo,同时使用 lo和nes。

以下是我迄今为止撰写的法典。 I m 正在编辑错误。 请帮助! 感谢!

import java.util.Scanner;

import java.io.*;

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

   {   

        int store1,store2,store3,store4,store5;
        int bar1,bar2,bar3,bar4,bar5;

        Scanner inputFile = new Scanner(System.in); 

        File inputFile = new File("sales.txt");


        File outputFile = new File("storeReport.txt");  

        PrintWriter outputFile = new PrintWriter("storeReport.txt");


        outputFile.println("SALES BAR CHART
");            

        bar1 = store1/100;
        outputFile.println("store 1 : ");
        for(int i = 1; i <= bar1; i++)
            outputFile.println("*");

        bar2 = store2/100;
        outputFile.println("
Store 2 : ");
        for(int i = 1; i <= bar2; i++)
            outputFile.println("*");

        bar3 = store3/100;
        outputFile.println("
Store 3 : ");
        for(int i = 1; i <= bar3; i++)
            outputFile.println("*");

        bar4 = store4/100;
        outputFile.println("
Store 4 : ");
        for(int i = 1; i <= bar4; i++)
            outputFile.println("*");

        bar5 = store5/100;
        outputFile.println("
Store 5 : ");
        for(int i = 1; i <= bar5; i++)
            outputFile.println("*");

        inputFile.close();

        outputFile.close();     

        System.out.println("Data written to the storeReport.txt");
        System.exit(0);

    }
}
问题回答

确实,你将2个变量称为投入工具:

Scanner inputFile = new Scanner(System.in);           
File inputFile = new File("sales.txt"); 

Try renaming the first one to inputScanner. Same deal for outputFile.

您的代码似乎能够成功地将预期产出编成>.txt-ifstore1,store2。 等到<代码>sales.txt,它们没有编号。 你没有告诉 Java读从档案和变数中的数字;他们从档案中自动获得数据。 您重新获得的汇编错误可能与使用原封不动的变量有关(即没有包含任何数据)。 重读你书中包含文件input的部分内容,以发现你如何从档案中把数据输入你的变量。

我将考虑这样一种方法。

static void printBar(int sales) {
    int bar = sales / 100;
    for(int i = 0; i < bar; i++) outFile.print("*");
    outFile.println(""); // flush and finish line
}

你可以有效地利用这些投入。 您将不得不把其外表变量设定为固定类别变量,或将其作为参数通过至<条码>。

Hints:

  1. 你们不可能有两种地方变量,其名称相同。 尤其看你对<代码>inputFile的声明。

  2. 您需要审查<代码>Scanner和File各类别的目的。 阅读“javadocs”,研究他们宣布的方法和建筑商。

  3. 声明bar1 = 贮存1/100; in the present edition of the Code will (once You ve set other wrong) 给你一个编辑错误,说store1尚未开始。 在使用这些变量之前,必须先采用当地变量。

  4. 创建<代码>Scanner 物体实际上没有读过任何东西。 你们必须呼吁采取办法,使其发挥作用。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...