• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic