Categories

JAVA DATEBASE
Technology Network Community
Oracle Database
Fusion Middleware
Development Tools
Java
Desktop
Server & Storage Systems
Enterprise Management
Berkeley DB Family
Cloud Computing
Big Data
Business Intelligence
Architecture
Migration and Modernization
E-Business Suite
Siebel
PeopleSoft Enterprise
JD Edwards World
Industries
JD Edwards EnterpriseOne
User Productivity Kit Pro (UPK) and Tutor
Governance, Risk & Compliance (GRC)
Master Data Management (MDM)
Oracle CRM On Demand
On Demand: SaaS and Managed Applications
AutoVue Enterprise Visualization
Primavera
ATG
Agile PLM
Endeca Experience Management
Fusion Applications
Archived Forums

 



Tags

New To Java


Applet, input string value, convert to int, increase by one....


We have to write a program that will take a string of digits, ex 875, into a JTextField. When you hit enter it will display in the Area incremented by one,  I can get it to display the value but I cant figure out how to add the value, as an integer and have it increment by one.   Any help would be appreciated.  Please and thank you ~  Here is what I have:   import javax.swing.*; import java.awt.*; import java.awt.event.*;  public class CreateAnApplet extends JApplet implements ActionListener {  	JTextField boxRectangle; 	JTextArea boxLocation; 	JLabel heading; 	int increase; 	int str;  	public void init () {  		boxRectangle = new JTextField (10); 		getContentPane().add(boxRectangle); 		boxRectangle.addActionListener(this);  		heading = new JLabel("Enter something herer"); 		getContentPane().add(heading);  		getContentPane().setLayout(new FlowLayout());  		boxLocation = new JTextArea(10,20); 		getContentPane().add(boxLocation);  	}  	public void actionPerformed(ActionEvent e){ 		str = Integer.parseInt(boxRectangle.getText());  		String str = boxRectangle.getText(); 		boxLocation.setText(str);  		++increase; 		boxLocation.setText("Your new number is now: " increase str); 	} }  Edited by: SunshineJava on Oct 24, 2012 4:45 PM

Hello, is it ok?  import java.awt.*; import java.awt.event.*; import javax.swing.*;   public class CreateAnApplet extends JApplet implements ActionListener {       JTextField boxRectangle;     JTextArea boxLocation;     JLabel heading;       @Override     public void init() {         boxRectangle = new JTextField(10);         getContentPane().add(boxRectangle);         boxRectangle.addActionListener(this);         heading = new JLabel("Enter something here");         getContentPane().add(heading);         getContentPane().setLayout(new FlowLayout());         boxLocation = new JTextArea(10, 20);         getContentPane().add(boxLocation);     }       @Override     public void actionPerformed(ActionEvent e) {         try {             int rectangle = Integer.parseInt(boxRectangle.getText());             boxLocation.setText("Your new number is now: " + (rectangle + 1));         } catch (NumberFormatException ex) {             boxLocation.setText("Please input an integer number..");         }     } } 

 I can get it to display the value but I cant figure out how to add the value, as an integer and have it increment by one.  Well first you need to set it to something.  You declare variables named 'increase' and 'str' (bad name for an int) int increase; int str;  Then you populate 'str' instead of 'increase' public void actionPerformed(ActionEvent e){ str = Integer.parseInt(boxRectangle.getText());  Then you define a new local variable name 'str' which is a String String str = boxRectangle.getText(); boxLocation.setText(str);  Then you try to increment 'increase' which was never assigned a value ++increase;  Straighten out this mess and populate 'increase' abvoe using the Integer.parseInt

Thank you - that is a mess. I will try your suggestions and reply 

Amazing when I cleaned up the mess. Thank you - excellent direction.    Final product:   import javax.swing.*; import java.awt.*; import java.awt.event.*;  public class CreateAnApplet extends JApplet implements ActionListener {  	JTextField boxRectangle; 	JTextArea boxLocation; 	JLabel heading; 	int increase; 	int word;  	public void init () {  		boxRectangle = new JTextField (10); 		getContentPane().add(boxRectangle); 		boxRectangle.addActionListener(this);  		heading = new JLabel("Enter something herer"); 		getContentPane().add(heading);  		getContentPane().setLayout(new FlowLayout());  		boxLocation = new JTextArea(10,20); 		getContentPane().add(boxLocation);  	}  	public void actionPerformed(ActionEvent e){ 		increase = Integer.parseInt(boxRectangle.getText());  		++increase; 		boxLocation.setText("Your new number is now: " +increase); 	} }


Related Links

Need help with parsing/reading/writing
Making my own Package
Input keyboard - Null pointer Exception
how to run java jar file
"View Source" problem in IE and Mozilla
sorting using display tag and Comparable interface
A Compiler error I didn't expect to see
Vector of vectors
Looping Question
Need help with installation of JCreator
HeeeeeeeeeeeeeeeeeeLP!
Help for newbie beginner (arrays)
Netbeans 5 beta CVS (totaly radiculouse)
quick question
Naming conventions....
Find and print illegal character in a string using regexp