read data from files ...

Hello experts,

   How do I read from a text file such as:

myData.txt
--------------------
0, Data, hello, world, 3
1, Data1, meow, woof
-----------------------

  if the input is numeric value -> do A
  if the input is String -> do B

  many thanks.
LVL 1
meow00Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hoomanvCommented:
read lines one by one
the use StringTokenizer to extract tokens in the line with whitespace and ',' as delimiter
the try to convert each token to integer like Integer.parseInt(token)
if it throws an NumberFormatException so its not an int so it may be somthing else i.e String
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank SAssociate Director - Product EngineeringCommented:
>> if the input is numeric value

You can parse a String into an integer:

int i = Integer.parseInt ( theString ) ;

If it throws a NumberFormatException, you know it was not a valid integer.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Mayank SAssociate Director - Product EngineeringCommented:
Sorry, yours wasn't there when I was typing mine :)
0
hoomanvCommented:
never mind ;-)
0
meow00Author Commented:
how do I read line by line ?
0
Mayank SAssociate Director - Product EngineeringCommented:
As explained in the link which CEHJ posted.
0
meow00Author Commented:
sorry ... how do I use StringTokenizer ? may i have an example ?
0
hoomanvCommented:
StringTokenizer st = new StringTokenizer(line, " ,");
while(st.hasMoreTokens()) {
    String token = st.nextToken();
}
0
CEHJCommented:
You can avoid all those conversion/tokenizing problems by using a Scanner:

      Scanner in = null;
      try {
            in = new Scanner(new FileInputStream("data.txt"));
            in.useDelimiter("[\\s,]");
            while (in.hasNext()) {
                  if (in.hasNextInt()) {
                        doNumber(in.nextInt());
                  }
                  else if(in.hasNext()) {
                        doText(in.next());
                  }
            }
      }
      finally {
            in.close();
      }
0
Mayank SAssociate Director - Product EngineeringCommented:
Or just use split ():

String[] array = line.split ( " " ) ;
int i = Integer.parseInt ( array[0] ) ;
0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.