Results 1 to 15 of 15
- 01-22-2011, 11:25 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
need to input values from a text file into an array and count values
hi guys im new to java and i need to input int from a text file into an array and count the values together
heres what i have got so far.
as you can see from the code below the array is called guestNumbers and the int i want to store is i3 then i want to add the values stored in guestNumbers and then divide the result by the length of guestNumbers
Java Code:Scanner cc = new Scanner( new File("CC.txt") ); Scanner sn = new Scanner( new File("sn.txt") ); Scanner ng = new Scanner( new File("gn.txt") ); String line ; String line2 ; String line3 ; System.out.println("course code"+" " + "Student Number" +" "+"Number of guests"); int count =1; guestNumbers = new double[count]; while( cc.hasNext() && sn.hasNext() && ng.hasNext() ){ line = cc.nextLine(); line2 = sn.nextLine(); line3 = ng.nextLine(); int i=Integer.parseInt(line); int i2=Integer.parseInt(line2); int i3=Integer.parseInt(line3); System.out.println(i + " " + i2 + " " + i3); count++; }
- 01-22-2011, 11:50 AM #2
Member
- Join Date
- Nov 2010
- Location
- New Delhi
- Posts
- 50
- Rep Power
- 0
File fFile = new File("c:\\sld32" + ".txt");
fr = new int[272];
try {
FileInputStream fstream = new FileInputStream(fFile);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(
new InputStreamReader(in));
String strLine;
// Read File Line By Line
int i = 0;
while ((strLine = br.readLine()) != null) {
// Print the content on the console
// System.out.println (strLine);
fr[i] = Integer.parseInt(strLine.trim());
i++;
}
System.out.println("integer conversion done& value r in int");
}// try end
catch (Exception e) {// Catch exception if any
System.err.println("Error: xyz" + e.getMessage());
}
i think this will helpful!!!
Good luck!!
- 01-22-2011, 12:20 PM #3
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
you declared
when the count is 1. means your array is only 1 element? is that really what you want? If possible, use an ArrayList to store your dataJava Code:guestNumbers = new double[count];
- 01-22-2011, 12:28 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
thanks
thanks i have altered the code to fit into my program but im just getting 0 from the array what seems to be happening is that the int i istnt coming out of the while loop so i is allways equal to 0 and the array in position 0 is also equal to 0
Java Code:File fFile = new File("gn.txt"); int[] fr = new int[272]; try { FileInputStream fstream = new FileInputStream(fFile); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader( new InputStreamReader(in)); String strLine; // Read File Line By Line int i = 0; int t= fr[i]; while ((strLine = br.readLine()) != null) { // Print the content on the console // System.out.println (strLine); fr[i] = Integer.parseInt(strLine.trim()); i++; } System.out.println("total = "+ t); }// try end catch (Exception e) {// Catch exception if any System.err.println("Error: xyz" + e.getMessage()); }
- 01-22-2011, 12:32 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
hi javahater what im aiming at is the array guestNumbers to be the length of the number of lines in gn.txt does a list allow this and how would you implement it?
- 01-22-2011, 12:32 PM #6
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
why did you change your code to use BufferedReader? Using Scanner is fine.
- 01-22-2011, 12:36 PM #7
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
I myself would use an ArrayList. However for your case, I am assuming you are doing your homework and can't use ArrayList ?? here are some ways (which I don't like), but since you are learning only....
1) set an initial count to your array when you declare it
2) count the number of lines first, then you will know how many integers are there. then you can declare the array with that size. Then go through the file again..... ....
- 01-22-2011, 12:41 PM #8
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
well i was just trying to follow the example that baloda as close as posible
- 01-22-2011, 12:55 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
i take it you meen some thing like this when you say count the number of lines first
int count=0;
while(ng.hasNext() ){
count=count++;
}
System.out.println(count);
my problem is that inside the while loop count may change but outside count stays the same
- 01-22-2011, 12:58 PM #10
Member
- Join Date
- Nov 2010
- Location
- New Delhi
- Posts
- 50
- Rep Power
- 0
first check if ur file has enough elements i.e elements in more then one line
- 01-22-2011, 01:07 PM #11
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
it is at the moment the file has 4 lines users can input new lines and that side of the program is fully working i can output the lines to the screen which i do and that part works
but i cant get the counter to work to initilise the array guestNumbers
- 01-22-2011, 01:19 PM #12
Member
- Join Date
- Nov 2010
- Location
- New Delhi
- Posts
- 50
- Rep Power
- 0
as far i can understand u can do like this
suppose
fr[i]=aaa;
then
guestNumbers [j]= i where guestName is aaa;
- 01-22-2011, 01:29 PM #13
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
- 01-22-2011, 01:53 PM #14
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
right guys got the counter to work thanks now i just need to enter the values into the array
- 01-22-2011, 02:36 PM #15
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
right now i have this as you can see im outputting a element from the array to the screen but no matter what element i say to output what shows on screen is 1.0 this is the last element that the array should be holding but inbetween there are supposed to be 2 elements with different numbers
Java Code:Scanner cc = new Scanner( new File("CC.txt") ); Scanner sn = new Scanner( new File("sn.txt") ); Scanner ng = new Scanner( new File("gn.txt") ); String line ; String line2 ; String line3 ; System.out.println("course code"+" " + "Student Number" +" "+"Number of guests"); int lineCount = 0; BufferedReader br2 = new BufferedReader(new FileReader("gn.txt")); while ((line = br2.readLine()) != null) { lineCount++; } System.out.println(lineCount); double guestNumbers[] = new double[lineCount]; while( cc.hasNext() && sn.hasNext() && ng.hasNext() ){ line = cc.nextLine(); line2 = sn.nextLine(); line3 = ng.nextLine(); int i=Integer.parseInt(line); int i2=Integer.parseInt(line2); double i3=Double.parseDouble(line3); System.out.println(i + " " + i2 + " " + i3); for(int c=0; c< lineCount; c++) { guestNumbers[(c)]=i3; } } System.out.println(guestNumbers[0]); System.out.println(guestNumbers[1]); System.out.println(guestNumbers[2]); System.out.println(guestNumbers[3]); cc.close(); sn.close(); ng.close();
my output from the program is
4
321 123321 1.0
321 654123 2.0
111 652145 3.0
324 545545 1.0
1.0
1.0
1.0
1.0
but should be
4
321 123321 1.0
321 654123 2.0
111 652145 3.0
324 545545 1.0
1.0
2.0
3.0
1.0
Similar Threads
-
Java 2D array get values from text fields
By raverz1tildawn in forum Java 2DReplies: 3Last Post: 01-08-2011, 12:56 AM -
count character in text file as input file
By aNNuur in forum New To JavaReplies: 7Last Post: 03-25-2010, 04:01 PM -
How to Read data from text file and calculate the values?
By janeansley in forum New To JavaReplies: 40Last Post: 07-04-2008, 08:41 AM -
[SOLVED] How to read a file and compare Array values
By DonCash in forum Advanced JavaReplies: 2Last Post: 04-02-2008, 02:22 PM -
[SOLVED] getting values from a text file
By dav9999 in forum New To JavaReplies: 8Last Post: 04-01-2008, 01:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks