+5 votes
by
Wrote a method which should read the 150 numbers from a file and put them into an array.
package com.keller;
import java.util.*;
import java.io.*;

public class MyCalc {
public static void main(String[] args) {
MyCalc cal = new MyCalc();
File file = new File("input.txt");
int[] A = cal.getData(file);
}
public int[] getData(File file) throws FileNotFoundException {
Scanner scan = new Scanner(file);
int[] dataArray = new int[150];
int i = 0;
while(scan.hasNextInt()) {
dataArray[i] = scan.nextInt();
i++;
}
return dataArray;
}
}


Но эта программа не работает, выдаёт ошибку:
spoiler
Exception in thread "main" java.lang.NoClassDefFoundError: bin/com/keller/MyCalc (wrong name: com/keller/MyCalc)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)

Почему она не работает, что это за ошибки?
by
In the file input.txt each number on a new line.
by
try to remove the package

4 Answers

0 votes
by
 
Best answer
$ cat com/keller/MyCalc.java 
package com.keller;
import java.util.*;
import java.io.*;

public class MyCalc {
public static void main(String[] args) throws IOException {
MyCalc cal = new MyCalc();
File file = new File("input.txt");
int[] A = cal.getData(file);
}
public int[] getData(File file) throws FileNotFoundException {
Scanner scan = new Scanner(file);
int[] dataArray = new int[150];
int i = 0;
while(scan.hasNextInt()) {
dataArray[i] = scan.nextInt();
i++;
}
return dataArray;
}
}
$ javac com/keller/MyCalc.java
$ java com/keller/MyCalc


Это если компилируешь из терминала. Все работает. Покажи, как путаешься запускать.
0 votes
by
The answer is in fact already given, but I advise you not to use the scanner, except that for small number of data(tests). His performance made me very happy. Here for example is an example with BAF.reader click
But if there is a certain structure, then it is best to use stringtokenizer, it's kind stringstreamera in C++
0 votes
by
I have the style of questions that identify the author. Enough to post puzzles, decide for yourself.
by
He asks "do(show me) how to do this and that" - simple words, write for me code. It's something he did, not working and asked for help to find the error.
We all were at school and "study" for this toasters and stackoverflow exist.
by
What! I am also studying java and where do you think I take puzzles?
0 votes
by
with java 7 it is better to use NIO.2
...