to Parse/format Incomming value from text into a type or from type to text for output.

Hi ,
I have two methods:
toText() and FromText(). Currently they work fine when the variable x (which is used for parsing/formatting) is null.

//IntegerConvert.java
String x = null;
public String  toText() // convert type to text
{
   String y = "";
    if (x == null)
   y = Integer.toString(input.asInt());
return y;

}
public int FromText(String y) //convert text to type
{
int result;
  if (x == null) {
                try {
                 
                    int value = Integer.parseInt(text);
                    result= value;
                }
                catch (NumberFormatException e) {
                      result = -1;
                }
            }
return result;
}
This works fine but I do not know how to parse if x = "some value"?.
I did solve for DateCOnvert.java by using SimpleDateFormat(x) when x ="some value". But I do not know how to do for Integer and Boolea n values when x is specified.

Any help is appreciated.
Thanks.
maithriaAsked:
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.

Ajay-SinghCommented:
> But I do not know how to do for Integer and Boolea n values when x is specified.
I did not understand the question? As far I understand, you want to know the data-type of the string (int, float, boolean, long etc) and parse the text accordingly? If yes, how are you going to distinguish been int and long, float and double?
maithriaAuthor Commented:
I distinguish different datatypes according to my schema file, wherein I mention a record and specifying fields of differnet datatype.
So if datatype = "Integer", then the values in the field corresponding to Integer datatype will be sent to IntegerConvert.java, so the Input I mentioned in toText() is actaully a datavalue of the Integer Field.
So I do know the datatype of the string, But I do not know how to parse the datatype if the format is specified(variable X). Right now I am not formatting the value of my input.
if (x == null)
   y = Integer.toString(input.asInt()); If x = "some value". then Do I need to use java.text.NumberFormat to format the values according to X?
I hope I answered your question
gkishorejiCommented:
u want to do string comparision.. then do

x.equals("some text")

this will return true if they are equal.
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Ajay-SinghCommented:
If X is NumberFormat, you can call parse() to get the number out of it,

if (x == null)
  y = Integer.parseInt(text);
else
  y = x.parse(text).intValue();
CEHJCommented:
maithria - is this an exercise? If not, you're probably doing some wheel-reinvention here ...
maithriaAuthor Commented:
sorry for not replying earlier.
I wrote something like this:


String x = "#,##"; //pattern to format my input
public String  toText() // convert type to text according to the format specified and return  the string
{
   String y = "";
    if (x == null)
   y = Integer.toString(input.asInt());
  else
{
  DecimalFormat df = new DecimalFormat(x);
  y = df.format(input.asInt());
}
return y;
}

public int FromText(String y) //convert text to type according to the format and parse
{
int result;
  if (x == null) {
                try {
                 
                    int value = Integer.parseInt(text);
                    result= value;
                }
                catch (NumberFormatException e) {
                      result = -1;
                }
            }
else // if  x = some pattern value
{
          try
      {
             DecimalFormat df = new DecimalFormat(x);
             Number num = df.parse(y);
            result = num.intValue();
      }
    catch (NumberFormatException e) {
                      result = -1;
                }
}
return result;
}

If my Input is:
100
200
3
40000
and my pattern is x = "#,##"
Then the output I am getting  is :
1,00
2,00
3
40,0000
My question is should'nt all the output match the format "#,##" , so why is it that I am getting 40,000 here?, when Exactly the exception gets thrown?.
Ajay-SinghCommented:
> so why is it that I am getting 40,000 here?, when Exactly the exception gets thrown?.
There is no exception, If you apply pattern #,## to 40000, it sould be 40,000.

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
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.