简体   繁体   English

Java从文本文件读取整数和字符串,然后使用整数和字符串

[英]Java reading integers and strings from text file and then using the integers and strings

I am attempting to read in some data from a text file, this goes in the format: 我试图从文本文件中读取一些数据,格式如下:

String (Monday)
String (M141)
Integer (16)
Integer (6)
Double (10.5)
Double (10.5)
Integer (20)
Integer (20)
Integer (20)
Integer (30)

I have managed to read the file and tried to use nextLine to display these, this worked but after using next line to print out these values I need to manipulate the data I need to add up (30, 20, 20, 20, 10.5, 10.5) and times this by (16) to get a final value but I cannot figure out a way to do this, I tried to use an array to store them then manipulate them but had no luck. 我已经设法读取文件并尝试使用nextLine来显示这些内容,但是在使用下一行打印出这些值之后,我需要操纵需要累加的数据(30、20、20、20、10.5, 10.5),然后将其乘以(16)以获得最终值,但是我找不到解决方法,我尝试使用数组存储它们,然后操纵它们,但没有运气。 There is more than one set of data with the same format but different integers. 有不止一组具有相同格式但不同整数的数据。 Please help me understand how to go about this, I've been trying for over a day. 我已经尝试了一天多,请帮助我了解如何解决此问题。 Here is my code so far: 到目前为止,这是我的代码:

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

public class Main {

    static Scanner console = new Scanner(System.in);


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

        Scanner reader = new Scanner(new File("C:\\data.txt"));

        System.out.println(reader.nextLine());
        System.out.println(reader.nextLine());
        System.out.println(reader.nextInt());
        System.out.println(reader.nextInt());
        System.out.println(reader.nextDouble());
        System.out.println(reader.nextDouble());
        System.out.println(reader.nextInt());
        System.out.println(reader.nextInt());
        System.out.println(reader.nextInt());
        System.out.println(reader.nextInt());

    }
}

EDIT: I need to add up the (10.5) (10.5) (20) (20) (20) (30) and times it by 16. 编辑:我需要将(10.5)(10.5)(20)(20)(20)(30)加起来并乘以16。

If you're having trouble getting the numbers themselves look up Java Substring. 如果您自己无法获取数字,请查找Java Substring。 It's very helpful. 这非常有帮助。 And also search Java IndexOf. 并搜索Java IndexOf。 These two methods will help you a lot. 这两种方法对您有很大帮助。 For example String a = "Bob(10)"; 例如String a =“ Bob(10)”; String x = a.substring(a.indexof("("),a.indexOf(")")); 字符串x = a.substring(a.indexof(“(”),a.indexOf(“)”)));

It sounds as if the file is written in lines try 听起来好像文件是用行写的try

Scanner reader = new Scanner(new File("C:\\data.txt"));

String monday = reader.nextLine();
// splits up the string into pieces, seperated at the space
String[] parts = monday.split(" ");
String day = parts[0];

// you may have to use Integer.parseInt() here I don't remember
int hourMutiplier = parts[2];
int people = parts[3];
double wages = 0;
for( int i = 4; i < people + 3; i++){
  wages += parts[i];  
}
double finalWages = wages * hourmutiplier;
...

or something like that, syntax might be a little off. 或类似的东西,语法可能会有点差。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM