Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

Changing String data to Integer data

I have the following code that reads the contents of
a label array:

for (int xx=0;xx<=k1 - 1;xx++){
   System.out.println("Wants = "+QtyField[xx].getText() + "   On hand = "+OnHandField[xx].getText());

I need to compare these to pieces of data which are actually
numbers.  How can I change the string data to int data so
I can compare and perform math functions with this data?

Thanx!

Don
0
afsac
Asked:
afsac
  • 4
  • 3
1 Solution
 
fontaineCommented:
Example:

// the String to convert
String intString = "12345";

// the integer variable that will contain the converted intString
int  value = 0;

try {
    value = Integer.parseInt(intString);
} catch(NumberFormatException e) {
// the String does not hold a valid integer. Have a try with intString="azerty" for example...
}



0
 
afsacAuthor Commented:
WOW! What quick response, I thought I was the only one up at this time!  Thank you
very much for your quick response. I'll apply it now and move on to my next problem!

Thanx!

Don
0
 
fontaineCommented:
> I thought I was the only one up at this time!

I am leaving in Belgium. Here, it is 1:10 PM. This explains that... :-)
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!

 
afsacAuthor Commented:
WOW! What quick response, I thought I was the only one up at this time!  Thank you
very much for your quick response. I'll apply it now and move on to my next problem!

Thanx!

Don
0
 
afsacAuthor Commented:
I have the following code in place:

int QtyValue=0;
int OnHandValue=0;
try{
   System.out.println("Tring to process " + QtyField[0].getText());
   QtyValue=Integer.parseInt(QtyField[0].getText());
   System.out.println("QtyValue = " + QtyValue);
}catch(NumberFormatException e){
  System.err.println("IOException: " + e);
}

It returns the following error:
Tring to process 1  
IOException: java.lang.NumberFormatException: 1  
QtyValue = 0

Any ideas?
Don
0
 
fontaineCommented:
Be sure that the String to convert is well "1", and not "1 ", for example. I suggest you
trim the String before trying to convert, so that the white spaces enventually around the
number do not interfere.

// a trick: whenever you want to display a String, surround it by stars, so that it appears
// straight if there are white spaces around it.
System.out.println("Tring to process: *" + QtyField[0].getText() + "*");

QtyValue=Integer.parseInt(QtyField[0].getText().trim());
0
 
afsacAuthor Commented:
That was exactly the problem! Thank you so much!
If you have the time or interest, I have a 200 pointer question out there.
If you would like to take a stab at it It could really help me out.  I've been working with
Java about 8-10 months.  I'm working on a big client/server project that reads,
deletes, and modifies data in an Oracle database. Its been great fun but these little
things like what you have helped me with, really slow me down.  But I have learned
alot, especially this morning. Thank you very much.

Don
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now