Solved

Changing String data to Integer data

Posted on 1998-11-19
7
195 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 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
Technology Partners: 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!

 

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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaFX TableView not displaying correctly 3 105
ejb on wildfly 5 46
jsp insert to database example 2 61
collection output issue 9 62
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 …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

756 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