The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


The only thing I can suggest...

Posted by Hiran on January 24, 2002 at 7:28 PM

The only thing I can think of is to check each value that you read in and see if it contains only numerical data or not. For example:

/** This is a method that takes in a string value and checks whether it contains only numerical data or not. It returns a true if the string contains only numerical data, and false otherwise */
private boolean checkData(String value)
boolean isInt = false;
int numCharInt = 0;
char c = 'a';
for (int i=0; i {
c = value.charAt(i);
for (int j=48; j<58; j++)
if (c == (char) j)
numCharInt ++;
if (numCharInt == value.length())
isInt = true;

Some notes:
1) The integer variable numCharInt holds the number of characters in the string that are actually integers
2) You don't have to intialize the character variable c to 'a'
3) 48 - 57 are the ASCII value equivalents of the numbers 0 - 9
I hope this helps. I haven't tested this, so I'm not entirely sure if it'll work. You might have to work out a few bugs, but this is the overall idea of how I would do what you want to do.

> I need to read from a file that contains both text and numbers and store them as individual values in an array......If they are numbers I need to use them to plot graphs(y axis) and the text (dates: "oct31") will serve as the values for the x axis........

> I have code that reads the entire file and stores it as text but I need to serve some of the values as integers.


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us