I have a question about read number of integers from text file and output the size, length (how many number of integers in that file). Can you guy tell me code to read from text file and code for how many numbers in that text file?
3 Replies - 315 Views - Last Post: 14 April 2012 - 01:20 PM
#1
read number of integers from file and output the size (how many numba)
Posted 13 April 2012 - 11:25 AM
Replies To: read number of integers from file and output the size (how many numba)
#2
Re: read number of integers from file and output the size (how many numba)
Posted 13 April 2012 - 11:43 AM
Write some code first yourself, and it will be easier for people to assist you.
Start out by writing code that reads a text file. For this you can use java I/O (streams).
Check out the BufferedReader for this. Once you have written the code, if you havent figured out the rest, post again. And remember to post your code, and people (including myself) will help you with out doubt.
Good luck
Start out by writing code that reads a text file. For this you can use java I/O (streams).
Check out the BufferedReader for this. Once you have written the code, if you havent figured out the rest, post again. And remember to post your code, and people (including myself) will help you with out doubt.
Good luck
This post has been edited by IamTw_: 13 April 2012 - 11:44 AM
#3
Re: read number of integers from file and output the size (how many numba)
Posted 14 April 2012 - 12:56 AM
Try working out the code yourself and then post it here, and we will be happy to help you with any problem you can be facing with it.
You basically need to read the file through a while loop until an end of file exception is reached. If the current character is an integer, then increase the value of the counter by one. Print the value of the counter outside the while loop, after it's over.
You basically need to read the file through a while loop until an end of file exception is reached. If the current character is an integer, then increase the value of the counter by one. Print the value of the counter outside the while loop, after it's over.
#4
Re: read number of integers from file and output the size (how many numba)
Posted 14 April 2012 - 01:20 PM
does you text file contains only numbers or a mix of text and numbers ?
If it contains only number it is easy
If it contains only number it is easy
Scanner scan = new Scanner(new File("file.txt"));
while(scan.hasNextInt()) {
int num = scan.nextInt(); // read next number
}
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|