Solved

Changing String data to Integer data

Posted on 1998-11-19
7
193 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
  • 4
  • 3
7 Comments
 
LVL 5

Accepted Solution

by:
fontaine earned 50 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

810 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