E
E
electrokeller2014-04-15 21:17:23
Java
electrokeller, 2014-04-15 21:17:23

How to correctly read numbers from a file in java?

Wrote a method that should read 150 numbers from a file and place them in 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;
  }
}

But this program does not work, it gives an error:
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)

Why is it not working, what are these errors?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
L
logder, 2014-04-15
@electrokeller

$ 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

This is if you compile from the terminal. Everything is working. Show how confused you are to launch.

V
vans239, 2014-04-15
@vans239

I already determine the author by the style of the questions. Stop posting puzzles, solve for yourself.

V
VovaOne, 2016-03-02
@VovaOne

with java 7 it's better to use NIO.2

T
Therapyx, 2016-09-14
@Therapyx

In fact, the answer has already been given, but I advise you not to use the scanner, except perhaps only for a small amount of data (tests). Its performance makes me very unhappy. For example, here is an example with a click
buff reader . And if there is a certain structure, then it is best to use stringtokenizer, this is something similar to stringstreamera in c ++

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question