Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Changing String data to Integer data

Posted on 1998-11-19
7
Medium Priority
?
200 Views
Last Modified: 2010-03-30
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
Comment
Question by:afsac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 5

Accepted Solution

by:
fontaine earned 200 total points
ID: 1227775
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
 

Author Comment

by:afsac
ID: 1227776
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
 
LVL 5

Expert Comment

by:fontaine
ID: 1227777
> 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:afsac
ID: 1227778
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
 

Author Comment

by:afsac
ID: 1227779
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
 
LVL 5

Expert Comment

by:fontaine
ID: 1227780
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
 

Author Comment

by:afsac
ID: 1227781
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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question