简体   繁体   English

从文件中读取字符串和原始数据类型并创建对象

[英]Reading strings and primitive data types from a file and creating objects

I'm reading from a file and using a scanner to read the inputs but am having difficulty understanding how to apply next() and nextLine() to assign data from the text to variables to be sent to my rectangle class constructor for printing these objects to the console. 我正在从文件中读取内容,并使用扫描仪读取输入内容,但难以理解如何应用next()和nextLine()将文本中的数据分配给变量,以发送给我的矩形类构造函数以打印这些对象到控制台。 I get parsing the input (nextInt...etc.), but when strings and primitive data mix on the same line on multiple lines of a scanner read text file how do you handle this? 我可以解析输入(nextInt ...等),但是当字符串和原始数据在扫描仪的多行中的同一行上混合时,读取文本文件该如何处理? Do I create multiple scanner loops for ints and String(s) to loop through it all to ensure the read data gets assigned to the array elements correctly? 是否为int和String创建多个扫描程序循环以遍历所有循环,以确保将读取的数据正确分配给数组元素?

TLDR: How do I read multiple strings and primitives from one .txt file, populate that data into my class fields to then create an array of objects and print *'s to the console to symbolize my created "rectangular" objects from said .txt file? TLDR:如何从一个.txt文件中读取多个字符串和原语,然后将该数据填充到我的类字段中,然后创建一个对象数组,并在控制台上打印*,以表示从上述.txt文件中创建的“矩形”对象文件?

Check out this related question: Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods 看看这个相关的问题: 扫描仪在使用next(),nextInt()或其他nextFoo()方法后跳过nextLine()

I honestly didn't understand your explanation of the text input, but it might be because the Scanner#nextInt method does not consume the last newline ('\\n') character. 老实说,我不理解您对文本输入的解释,但这可能是因为Scanner#nextInt方法不使用最后一个换行符('\\ n')。 If that's the case, a workaround is to call another nextLine() to consume it. 在这种情况下,一种解决方法是调用另一个nextLine()来使用它。

Also, Scanner#next do ignore whitespaces, but it doesn't ignore primitives, such as booleans or integers, as they are valid tokens. 另外,Scanner#next确实会忽略空格,但不会忽略原始类型,例如布尔值或整数,因为它们是有效的标记。

When you get your string with nextLine() you have to split it with: 当您使用nextLine()获得字符串时,必须使用以下方法进行拆分:

  • something like String#substing(int begin, int end) more information 类似String#substing(int begin,int end)的更多信息
  • or you use String#split(" ") and get a string array, which contains in index i the ith word. 或者使用String#split(“”)并获取一个字符串数组,该数组在索引i中包含第i个单词。 more information 更多信息
  • I think it is also possible to get your Data with nextInt() and your Reader will only give you the first piece of data. 我认为也可以通过nextInt()获取您的数据,而您的Reader只会给您第一条数据。 With another nextInt() the second number, and so on. 与另一个nextInt()第二个数字,依此类推。 But I'm not sure about this solution (But you can try it, through) 但是我不确定这个解决方案(但是您可以尝试一下)

After that you can convert it to a datatyp you want to have. 之后,您可以将其转换为您想要的数据类型。 For example if you want a double you can use Double.parseDouble(stringVar) . 例如,如果要double ,可以使用Double.parseDouble(stringVar)

At Last you create your objects and you only have to find a way to print it. 最后,您创建了对象,而您只需要找到一种打印它的方法。

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

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