Solved

Changing String data to Integer data

Posted on 1998-11-19
7
191 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoreUp challenge 14 48
sumDigits  challenge 7 62
What is JNDI datasource in spring 1 38
unix in java example 9 43
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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now