3 Replies - 422 Views - Last Post: 14 April 2012 - 01:20 PM Rate Topic: -----

#1 junny013  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-April 12

read number of integers from file and output the size (how many numba)

Posted 13 April 2012 - 11:25 AM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: read number of integers from file and output the size (how many numba)

#2 IamTw_  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 238
  • Joined: 25-February 11

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 :)

This post has been edited by IamTw_: 13 April 2012 - 11:44 AM

Was This Post Helpful? 0
  • +
  • -

#3 Casper1  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 13
  • Joined: 12-April 12

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.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

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

Scanner scan = new Scanner(new File("file.txt"));
while(scan.hasNextInt()) {
    int num = scan.nextInt();   // read next number
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1