Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

read data from files ...

Posted on 2006-04-21
12
Medium Priority
?
243 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:meow00
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 16509102
0
 
LVL 14

Accepted Solution

by:
hoomanv earned 600 total points
ID: 16509133
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
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 400 total points
ID: 16509155
>> 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 16509159
Sorry, yours wasn't there when I was typing mine :)
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16509165
never mind ;-)
0
 
LVL 1

Author Comment

by:meow00
ID: 16509187
how do I read line by line ?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16509208
As explained in the link which CEHJ posted.
0
 
LVL 1

Author Comment

by:meow00
ID: 16509244
sorry ... how do I use StringTokenizer ? may i have an example ?
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16509250
StringTokenizer st = new StringTokenizer(line, " ,");
while(st.hasMoreTokens()) {
    String token = st.nextToken();
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16509300
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16509381
Or just use split ():

String[] array = line.split ( " " ) ;
int i = Integer.parseInt ( array[0] ) ;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16509876
:-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question