在java编程中,如何从包含整数和字符串的文本文件中读取整数?
我只需要从文件中读取整数并求和。我不知道如何“跳过”字符串或绕过它们只读取整数。这是我的代码,在本章中,我仅有的选项是在java编程中,如何从包含整数和字符串的文本文件中读取整数?,java,Java,我只需要从文件中读取整数并求和。我不知道如何“跳过”字符串或绕过它们只读取整数。这是我的代码,在本章中,我仅有的选项是while(!scan.hasnetint())。这是我的代码,减去我刚才添加的行,它只适用于整数文件。该文件如下所示: 1 2 John 3 4 Black 5 Jason 7 8 每行上都有一个数字或字符串。 我不明白如何使用这种方法 我的代码: import java.util.Scanner; import java.io.File; import java.io.IO
while(!scan.hasnetint())
。这是我的代码,减去我刚才添加的行,它只适用于整数文件。该文件如下所示:
1
2
John
3
4
Black
5
Jason
7
8
每行上都有一个数字或字符串。
我不明白如何使用这种方法
我的代码:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
int number;
int temp;
int i;
boolean isPrime = true;
double count = 0.0;
double total = 0.0;
File inputFile = new File("Values.txt");
Scanner file = new Scanner(inputFile);
while (file.hasNext())
{
if (!file.hasNextInt()) { //this just added
String s = file.nextLine(); //added
} //added
else { //added
number = file.nextInt();
}
for (i = 2; i <= (number / 2); i++)
{
temp = number % i;
if (temp == 0)
{
isPrime = false;
break;
}
}
if (isPrime) {
//System.out.println(number);
total = total + number;
count = count + 1.0;
}
isPrime = true;
}
//System.out.println(total);
//System.out.println(count);
System.out.println(total / count);
System.out.println("File is Empty");
file.close();
}
}
import java.util.Scanner;
导入java.io.File;
导入java.io.IOException;
公共班机{
公共静态void main(字符串[]args)引发IOException{
整数;
内部温度;
int i;
布尔值isPrime=true;
重复计数=0.0;
双倍合计=0.0;
File inputFile=新文件(“Values.txt”);
扫描仪文件=新扫描仪(inputFile);
while(file.hasNext())
{
如果(!file.hasnetint()){//这是刚刚添加的
字符串s=file.nextLine();//已添加
}//增加
其他{//
number=文件.nextInt();
}
对于(i=2;i我认为问题在于您使用的是nextLine()
而不是next()
我只需要从文件中读取整数并求和。我不需要
知道如何“跳过”字符串或绕过它们只读取
整数
您只需要这么多代码:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
int total = 0;
File inputFile = new File("Values.txt");
Scanner file = new Scanner(inputFile);
while (file.hasNext()) {
if (!file.hasNextInt()) {
file.nextLine();// Read and ignore it
} else {
total += file.nextInt();
}
}
System.out.println("The sum of all integers: " + total);
file.close();
}
}
输出:
The sum of all integers: 30
The sum of all integers: 30
The sum of all prime integers: 17.0
The average of all prime integers: 4.25
The sum of all prime integers: 17.0
The average of all prime integers: 4.25
或者,您可以按如下方式执行:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
int total = 0;
File inputFile = new File("Values.txt");
Scanner file = new Scanner(inputFile);
while (file.hasNext()) {
try {
total += Integer.parseInt(file.nextLine());
} catch (NumberFormatException e) {
// Ignore the exception
}
}
System.out.println("The sum of all integers: " + total);
file.close();
}
}
输出:
The sum of all integers: 30
The sum of all integers: 30
The sum of all prime integers: 17.0
The average of all prime integers: 4.25
The sum of all prime integers: 17.0
The average of all prime integers: 4.25
注意:您可以使用Integer.valueOf(file.nextLine())
代替Integer.parseInt(file.nextLine())
,效果相同
[更新]
抱歉,我必须对文件中的素数求和平均值
以下是根据您的评论进行的更新:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
double total = 0;
int number = 0;
int count = 0;
boolean prime;
File inputFile = new File("Values.txt");
Scanner file = new Scanner(inputFile);
while (file.hasNext()) {
if (!file.hasNextInt()) {
file.nextLine();// Read and ignore it
} else {
number = file.nextInt();
// Check if the number is prime
prime = true;
if (number == 1 || number == -1) {
prime = false;
}
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
prime = false;
break;
}
}
if (prime) {
total += number;
count++;
}
}
}
System.out.println("The sum of all prime integers: " + total);
System.out.println("The sum of all prime integers: " + total / count);
file.close();
}
}
注意,直到数字的一半才需要检查,检查到数字的平方根就足够了。检查
最后,我建议您创建一个函数来检查素性,以便使您的程序更加模块化
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
double total = 0;
int number = 0;
int count = 0;
File inputFile = new File("Values.txt");
Scanner file = new Scanner(inputFile);
while (file.hasNext()) {
if (!file.hasNextInt()) {
file.nextLine();// Read and ignore it
} else {
number = file.nextInt();
if (isPrime(number)) {
total += number;
count++;
}
}
}
System.out.println("The sum of all prime integers: " + total);
System.out.println("The average of all prime integers: " + total / count);
file.close();
}
static boolean isPrime(int number) {
boolean prime = true;
if (number == 0 || number == 1 || number == -1)
return false;
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
prime = false;
break;
}
}
return prime;
}
}
祝你一切顺利!你到底不明白什么?你想完成什么?你在哪里需要帮助?我想你需要继续;
在String s=file.nextLine()之后
-如果这不是问题所在,那么对您的结果如何偏离您的预期进行更多的解释将非常有帮助。您的Java类无法编译。有关(i=2;我建议您将下一行读入字符串。然后尝试将该行解析为整数,如果无法将其解析为整数,则跳过该行。从文件中读取后进行判断时,复杂性会降低。请不要取消我的编辑。这些编辑有助于使您的问题更具可读性。对不起,我必须对p进行求和和和平均一个文件中的字符串,但我的问题是跳过字符串,所以你给出的是不允许的。谢谢。谢谢…这很有效…但是sc.next和sc.nextLine之间的区别是什么。如果我要真正学习,我想知道..我想nextLine会跳到下一行,继续读取整数。使用nextLine时e()
您假设文件中的每个数字或单词后都有换行符,但事实并非如此。转到下一个数字/单词比转到下一行更安全。文档可能会给您一个清晰的想法:谢谢。非常感谢。