Question:
Input file ints to array JAVA?
Giovanni
2014-04-05 12:20:01 UTC
I have an assignment on sorting, i kno i can get the sorting down but im having an issue with inputing the 512 ints in a file into an array. the instructor provided us with a file with 4 equal sets of ints. i tried to make my array of size [scan.nextInt()] and it cuts off the last 21 ints. and skips the first int. how can i get all of the integers in the text file into my array? this is what i have so far. if i hard code the array to size 50000 and then try to print the array it compiles but errors when running it.


System.out.println("Please Enter text file in this format, XXXXX.txt :");
String file =fileName.nextLine();
Scanner scan = new Scanner(new File(file));
int [] data = new int[scan.nextInt()]; <-------here it skips first int and then last 20 ints
int count= data.length;
for (int i=0; i {
data[i]=scan.nextInt();

}
System.out.print(Arrays.toString(data));
first 4 ints in output are: 501, 257, 390, 478...., supposed to be 492,501,390....
and last ints are: ....88, 83, 79, 0 and supposed to be :88 83 79 77 76 72 71 71 66 57 56 48 48 41 33 30 23 23 18 17 15 13 9....it replace last ints with 0. why ? and how do i fix this.
Three answers:
Leo D
2014-04-05 13:09:21 UTC
1. Why don't teachers teach how to use proper piping with standard input/output? :/ Why ugly up your output and make your program less friendly with unnecessary prompts, and waste time trying to retrieve resources within your program instead of letting the OS take care of that for you?



2. What's the format of your file? Is it supposed to be comma separated values? Remember that by default, scanner scans *space*-separated values. If you are going to use comma-separated values, you need to set the delimiter to a comma as so:



    Scanner scan = new Scanner(new File(file)).useDelimiter(",");



It's always usually a good idea to set the delimiter, because the space-character is rarely a useful delimiter, except maybe when you're parsing through the passage of a book or something.



3. You write "here it skips first int and then last 20 ints "



This isn't skipping the first integer. Instead, it's returning it, and it's being used to allocate the integer array. If you still need it, you should assign it to some variable, actually this is a good idea because then you don't need to set "count" to data.length;



Do this instead:



    int count = scan.nextInt();

    int[] data = new int[count];

    for (int i = 0; (i < (count - 1)); i++)



Why is it (count - 1) though? It's generally a strange condition. You would normally loop while (scan.hasNextInt()) instead, but I suppose that it's because you're using an array. The condition I just mentioned would be better if your queue was in a linked list instead.



4. What do you mean by "then last 20 ints"? How does a statement that you called in the beginning of an input stream affect the end of the stream?



I may have to see the actual file that you're working with and the full program.
?
2014-04-05 13:02:05 UTC
You should do your count in order to create the array.

http://pastebin.com/LXawVybQ



It replaces the last int with 0, probally because you have data.length-1 you never fill in the last value.



Are you sure it cuts off the last 21? not the last 11? Because the way im thinking it's probally setting your data size to 501(11 less than you need)



of coarse if you're sure that all the text files are 512 you could just hard code it to 512
John
2014-04-05 15:45:56 UTC
try this...

import java.util.*;

import java.io.*;



public class Program {

public static void main(String[] args) {

String pathAndFile = read("Please Enter text file in this format, XXXXX.txt : ");

try {

int[] data = load(pathAndFile, 512);

System.out.println( Arrays.toString(data));

Arrays.sort(data);

System.out.println( Arrays.toString(data));

} catch (FileNotFoundException ex) {

ex.printStackTrace();

}

System.exit(0);

}



private static String read(String message) {

System.out.print(message);

Scanner keyboard = new Scanner(System.in);

return keyboard.nextLine();

}



private static int[] load(String pathAndFile, int size) throws FileNotFoundException {

int[] data = new int[size];

Scanner fileIn = new Scanner(new FileReader(pathAndFile));

for (int i = 0; fileIn.hasNext() && i < size; i++) {

data[i] = fileIn.nextInt();

}

fileIn.close();

return data;

}

}



// data.txt

501

257

390

478

...

88

83

79

77


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...