The Artima Developer Community
Sponsored Link

Java Answers Forum
Converting a String to an Integer

4 replies on 1 page. Most recent reply: Nov 12, 2002 12:21 PM by Mike

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Mike

Posts: 4
Nickname: mikey65
Registered: Nov, 2002

Converting a String to an Integer Posted: Nov 12, 2002 4:40 AM
Reply to this message Reply
Advertisement
Hey,

I have a basic ish knowledge of java and I am currently making a calculator using the Java programming language. I have thought to make a string variable for the number inputs so when the user keeps pressing '1' the string makes up a string of 1's. I see this as the best way.

The problem is that when I come to add the numbers up I see that I have to convert the string to an integer or other number based variable. I have tried the following method, it seems to work and puts the value in the integer but I can't seem to add the itegers up. I have stripped the code down a lot to make it a lot more simpler to understand.

Thanks for any help in advanced.


import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class calcv2 extends Applet{

String tempnumber="111";
int numstorea=1;
int numstoreb=2;


public void paint (Graphics g){

g.drawString("Calculator Version 1" ,275,20);

numstoreb = Integer.valueOf(tempnumber);

total = numstorea+numstoreb;

g.drawString("Total = " +total ,275,60);
}
}


Mike

Posts: 4
Nickname: mikey65
Registered: Nov, 2002

Re: Converting a String to an Integer Posted: Nov 12, 2002 4:44 AM
Reply to this message Reply
Where I declared the integer variables at the top I have tried declaring them with and without pre-set values.

And also there was a 'total' variable declared but I accedentially took it out when stripping the code down.

Dante T. Salvador

Posts: 19
Nickname: anteng
Registered: Nov, 2002

Re: Converting a String to an Integer Posted: Nov 12, 2002 8:59 AM
Reply to this message Reply
String strNumber = "14159";
Integer intNumber = Integer.valueOf(strNumber);

Mike

Posts: 4
Nickname: mikey65
Registered: Nov, 2002

Re: Converting a String to an Integer Posted: Nov 12, 2002 11:36 AM
Reply to this message Reply
Thanks for that but the real problem is when this happens. Check the image below or copy and paste it into your borwser.

http://mysite.freeserve.com/mike65/java_pic1.jpg

Mike

Posts: 4
Nickname: mikey65
Registered: Nov, 2002

Re: Converting a String to an Integer Posted: Nov 12, 2002 12:21 PM
Reply to this message Reply
It's ok I got it working. I had to use numstore = Integer.parseInt(strtempnum); and not the other one I used before.

Flat View: This topic has 4 replies on 1 page
Topic: Pointing at the correct JAR File. Previous Topic   Next Topic Topic: NEWBIE ALERT: Problem with displaying shape on my GUI

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use