如何从包含多种数据类型的文本文件中进行输入

发布于 2024-10-01 05:23:30 字数 1949 浏览 2 评论 0原文

Java新手,我有一个问题,我已经把头撞在墙上几个小时了。

基本上,我需要读取包含字符串、整数和双精度数据类型的文本文件。 (文件见下文)

文本格式为两行。第一行只是产品名称,第二行是代表工厂编号的整数,然后是 4 位小数,代表四个工厂之一的销售额,单位为百万美元。

CrossTrainMax
1 15.1 12.8 3.14 2.3
AirGlider
3 12.2 4.6 6.5 8.3
AquaWalker
2 3.82 1.75 7.6 6.38
SuperHike
1 9 11.2 7.5 8.4

第一个值是产品名称。个位数整数是生产该产品的工厂编号。小数是该产品的销售额(以百万美元为单位),由空格分隔。

我的计划是将产品名称作为字符串读入,然后使用整数值作为 SWITCH 语句。 (每个工厂的利润百分比不同,所以我认为解决这个问题的最佳方法是计算 switch 语句中的总利润。)

在获得每个产品的总销售额后,我将不得不显示产品销量最高的用户。

所以我的问题是:

a) 如果我尝试使用 NextLine 逐行读取多种数据类型,如何读取? 我知道如何读取所有整数、所有双精度数或所有字符串,但是如果它们都不同,最好的方法是什么?

到目前为止,我的尝试导致了不匹配异常。

b) 我是否最好将所有值作为字符串读取并使用正则表达式将它们解析为变量?

c) 如果我要根据最高利润对销售额进行排序,我是否需要将每个产品的总销售额存储在单独的变量中?如果我使用循环读取文件的输入,是否可以执行此操作? (我们还没有讨论数组)

这是我一直在玩的代码的开头。我仍然停留在阅读单独数据类型的问题上。任何反馈/批评都是值得赞赏的。

import java.util.Scanner; // Needed for scanner class
import java.io.*; // Needed for the File and IOException

public class SalesProcessor
 {
  public static void main(String[] args) throws IOException
   {
   int x;
   double sales = 0;
   double total = 0;
   double profit = 0;



    // Create scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter the filename: ");
    String filename = keyboard.nextLine();

    // Open the file
    File file = new File(filename);
    Scanner inputFile = new Scanner(file);
    String line = inputFile.nextLine( );

    for (x = 1; x <= 5; x++)

      { 

  while (line != "END")
   {
    int plant = inputFile.nextInt();

      switch (plant)
       {
       case 1:

       while (inputFile.hasNext())
        {
        sales = inputFile.nextDouble();
        total = total + sales;



        }
        profit = sales * .06;
        System.out.println(total);
        System.out.println(profit);
        System.out.println(profit);
       break;
       }



    } 
   }

  }
 }

Java newbie here, I have a problem that I have been banging my head on the wall for several hours now.

Basically, I need to read a text file that contains String, Integer, and Double datatypes. (see below for the file)

The text is formatted in two lines. The first line is only the product name, and the second line is an integer representing the factory number, and then 4 decimals that represent the sales in one of four factories in millions of dollars.

CrossTrainMax
1 15.1 12.8 3.14 2.3
AirGlider
3 12.2 4.6 6.5 8.3
AquaWalker
2 3.82 1.75 7.6 6.38
SuperHike
1 9 11.2 7.5 8.4

The first value is the name of a product. The single digit integer is a factory number where it is produced. The decimal number are the sales of that product in millions of dollars, delimited by a white space.

My plan is to read in the product name as a string, and then use the integer value as a SWITCH statement.
(The % of profit is different at each factory, so I thought the best way to go about solving this way to calculate the total profit inside the switch statement.)

After I get the total sales per product, I will then have to display to the user which product has the highest sales.

So my questions are:

a) How can I read in multiple data types if I am trying to do it line by line using NextLine?
I know how to read in all integers, all doubles, or all strings, but what is the best way to go about it if they are all different?

My attempt so far has resulted in mismatch exceptions.

b) Am I better off reading all of the values in as strings and using regular expressions to parse them into variables?

c) If I am going to be sorting the Sales based on the highest profit, will I need to store each products total sales in a separate variable? Is it possible to do this if I am reading in the input of the file using loops? (We have not covered arrays yet)

Here is the beginning of the code I have been playing with. I am still stuck on the problem of reading the separate data types in. Any feedback/ criticism is appreciated.

import java.util.Scanner; // Needed for scanner class
import java.io.*; // Needed for the File and IOException

public class SalesProcessor
 {
  public static void main(String[] args) throws IOException
   {
   int x;
   double sales = 0;
   double total = 0;
   double profit = 0;



    // Create scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter the filename: ");
    String filename = keyboard.nextLine();

    // Open the file
    File file = new File(filename);
    Scanner inputFile = new Scanner(file);
    String line = inputFile.nextLine( );

    for (x = 1; x <= 5; x++)

      { 

  while (line != "END")
   {
    int plant = inputFile.nextInt();

      switch (plant)
       {
       case 1:

       while (inputFile.hasNext())
        {
        sales = inputFile.nextDouble();
        total = total + sales;



        }
        profit = sales * .06;
        System.out.println(total);
        System.out.println(profit);
        System.out.println(profit);
       break;
       }



    } 
   }

  }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在你怀里撒娇 2024-10-08 05:23:30

一般来说,每个朋友都需要使用java tokenizer。

In general, you need to use the java tokenizer per a friend.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文