我是java的初学者并且正在做练习,我需要链接文本文件以便我的代码通读。
这是代码:
import javax.swing.*;
import java.util.*;
import java.io.*;
public class Java116 {
public static void main(String[] args) throws IOException {
String[] namn = new String[100];
double[] perMån = new double[100];
double[] perMinDag = new double[100];
double[] perMinÖvr = new double[100];
double[] friaDag = new double[100];
double[] friÖvr = new double[100];
Scanner fil = new Scanner(new File("mobildata.txt"));
int n = 0;
while (fil.hasNext()) {
namn [n] = fil.next();
perMån[n] = fil.nextDouble();
perMinDag[n] = fil.nextDouble();
perMinÖvr[n] = fil.nextDouble();
friaDag[n] = fil.nextDouble();
friÖvr[n] = fil.nextDouble();
n++;
}
while (true) {
String s =JOptionPane.showInputDialog("Hur många minuter ringer du per månad \n" +
"Under dagrid respettive övrig tid? ");
if (s == null)
break;
Scanner input = new Scanner (s);
double minDag = input.nextDouble();
double minÖvr = input.nextDouble();
double billigast = Double.MAX_VALUE;
int index = 0;
for (int i=0; i<n; i++) {
double antalDag = Math.max(minDag-friaDag[i], 0);
double antalÖvr = Math.max(minÖvr-friÖvr[i], 0);
double kostnad = perMån[i] + antalDag*perMinDag[i] + antalÖvr*perMinÖvr[i];
if (kostnad < billigast) {
billigast = kostnad;
index = i;
}
}
String resultat = String. format("%.2f", billigast);
JOptionPane.showMessageDialog(null,
namn[index]+ "är billigast.\n" +"kostnad: " + resultat + "kr / månad" );
}
}
}
我得到的错误是:
Exception in thread "main" java.io.FileNotFoundException: mobildata.txt (The
system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:611)
at Java116.main(Java116.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
我已将文本文件添加到java文件中,其中使用Intellij
时我的所有其他文件都位于该文件中非常感谢提前!
答案 0 :(得分:0)
使用Intellij时经常出现此问题 - 您是否检查了文件的确切路径?您使用的是绝对路径还是相对路径?当您不在Intellij中运行时,此程序是否运行? 尝试将EXACT路径放在这一行:
Scanner fil = new Scanner(new File("mobildata.txt"));
请回答这些问题,然后我可以进一步提供帮助。
答案 1 :(得分:0)