This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes determining an input's data type.(part 2) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "determining an input Watch "determining an input New topic
Author

determining an input's data type.(part 2)

Teresa Juan
Greenhorn

Joined: Jan 20, 2004
Posts: 2
i've come up with this program from reading and researching.. (coz i'm just a newbie in java and my program's kinda lame..) i can't seem to solve the problem here.. it has one error the last time i compiled it.. can someone pls help me with this? please.. tnx..
here's the program's conditions:
--> the program will ask the user for 10 input.. after the the user input something, the program needs to determine if the input's data type is an integer, a float, a character or a string..

import java.awt.*;
import java.io.*;
import java.lang.*;
public class LexicalAnalyzer
{
public static boolean Letter(String s)
{
for (int e = 0; e < s.length()-1; e++)
{
char f = s.charAt(e);

if (Character.isLetter(f))
{if (s.length() == 1)
return true; }}
}
public static void main(String args[]) throws IOException
{

String anything, k;
double p = 0;
int a, q = 0, b;
char i, c;


//inputkoh
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));

for(a=1 ; a<11 ; a++)
{
System.out.print("Input anything: ");
anything = input.readLine();

for (b = 0; b < anything.length; b++)
{c = anything.charAt(b);
if (Character.isDigit(c) && c != '.')
{p = Double.parseDouble(anything);
break; }
else if(c == '.')
{q = Integer.parseInt(anything);
break; } }

if (q == 0)
System.out.print("Input is Real");
else if (p == 0)
System.out.print("Input is an Integer");

if (Letter(anything))
System.out.print("Input is a Character");

else
System.out.print("Input is a String");

}}

[ January 22, 2004: Message edited by: Teresa Juan ]
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Teresa, i don't know how you are trying to do this, because the idea itself is not clear at all... let me give you some examples:
Let us suppose the user entered the following as an arguments: 1 a 3.5
What do you think the data types of the following are?
There is no way to tell the answer for this question, here is why:
a) '1' could be an integer or a char or even a string
b) 'a' could be a character or a string
c) '3.5' this is definitely a float or perhaps String!!!
Now you see where i am going at. Untill you find a way to distinguish between these types, there is no way to tell a human what is what, not to mention the compiler.
[ January 22, 2004: Message edited by: Vicken Karaoghlanian ]

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Vicken raises very valid points. Like he discusses, 5 can be a double, an int, a char or even a String.
However, if for academic argument and for fun you wanted to set up and apply a hierarchy such that an argument�s type is determined in the following matter:
1. If an argument is �capable� of being an int, it is considered an integer (even though it can also be a valid (Java type) double, String, or possibly a char)
2. If an argument is �capable� of being a double, and is not capable of being an int, then it is considered a real (even though it can also be a valid String, and possible a valid int or char)
3. If an argument is �capable� of being a character, and is not capable of being an int (or double), then it is considered a char (even though it is capable of also being a String).
4. If an argument is not �capable� of being an int, double, or char, then it is considered a String.
If you accepted that hierarchy or selection criteria -- and that�s a key point, you have a set of selection rules laid out that are somewhat arbitrary as far as the Java language is concerned -- then you could determine an arguments �type� using the following code:

Again, the key thing here is the �contract� or selection rules are very specific and are not really code/language orientated. � in other words you are determining an argument�s �type� as we define the various types, not as the Java Programming language does. They are just rules we made up.
Keep in mind, this code prevents an argument like 2.0 or even 2. from being considered an integer since the decimal point implies a certain level or precision (beyond simply being an integer) and therefore the Integer.parseInt() will throw the NumberFormatException. I suppose if you wanted to consider 2.0 and 2. to be integers, (as all mathematicians and scientists in the world cringe as such a thought) you could change code in the try block that parses for the Double to:


I�d be curious as to what practical application this could be applied to, but it is a little fun to play with.
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Originally posted by Mark Vender:
I�d be curious as to what practical application this could be applied to, but it is a little fun to play with.

I think what Teresa is trying to do is to implement a parser. I remember doing that back in my university days, I implemented PASCAL compiler (parser to be more specific) using JAVA for my 'compiler' coarse project, and let me tell you it isn�t an easy task.
Teresa if that is what you are trying to do, I�ll be glad to help you (if I can of coarse). Meanwhile good luck, and have fun.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: determining an input's data type.(part 2)