Results 1 to 5 of 5
  1. #1
    Romally is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default Trying to parse int from string. Need help please.

    I'm trying to convert the string input from the gui input, into an int. However, the editor isn't letting me do this, as far as I know, I have everything set correctly. I'm only on chapter 4 of my Java book. O.o;

    Main Class File:

    Java Code:
    // Begin import
    
    import javax.swing.JOptionPane;
    /* import java.util.Scanner;
     * Used as fallback
     */
    
    // End import
    
    // Begin main class
    
    public class Main 
    
    {
    
    	
    	// Begin main method set
    	
    	
    	public static void main(String[] args)
    	
    	{ 
    	
    	// Begin create new object
    	
    	Data myData = new Data();
    	
    	// End create new object
    	
    	
    	// Begin welcome screen
    	
    	{
    		String settingName = // Set variable
    			JOptionPane.showInputDialog( null, "Welcome, please enter your name: "); // Get input
    		myData.setName( settingName ); //Store name
    		
    		// Display name for data check
    		/*System.out.println(settingName);
    		 * 
    		 */
    		
    		// End welcome screen
    		
    		//Begin account screen
    		
    		String settingAccountNumber = // Set variable
    			JOptionPane.showInputDialog( null, "Please enter your account number: "); // Get input
    		Integer.parseInt( settingAccountNumber  ); // Translate string to "int"
    		myData.setAccount( settingAccountNumber ); // Pass new int to store account
    		
    		// Display account for data check
    		/*System.out.println(settingAccountName);
    		 * 
    		 */
    		
    		// End account screen
    		
    	}
    
    }
    }

    Data Class File

    Java Code:
    // Begin class Data
    
    public class Data 
    {
    	// Begin private variables
    	private String name;
    	private int account;
    	
    	// End private variables
    
    	// Begin public method set
    	
    	// Begin set name
    
    	public void setName( String settingName )
    	
    	{
    		name = settingName;
    	} //End set Name
    	
    	//Begin set account
    	
    	public void setAccount(int settingAccountNumber )
    	
    	{
    		account = settingAccountNumber;
    	} // End set account
    	
    	//End public method set
    
    } // End public class Data

    Thanks for any help!

    P.S. if its too much trouble, point me directly to the reference files I need to read to solve this problem please. Thank you again!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Integer.parseInt doesn't change the String that is passed into it. In fact Strings are immutable, and nothing can change them. Rather it returns an int that you can use. So declare an int variable that will accept the int returned from this method and you can then use it.

    Luck!

  3. #3
    Romally is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Didn't I already do that when I declared the private int account? Maybe I'm misunderstanding you, but I declared the private variable as an int and also declared the setAccount parameters variable as an int.

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Romally View Post
    Didn't I already do that when I declared the private int account? Maybe I'm misunderstanding you, but I declared the private variable as an int and also declared the setAccount parameters variable as an int.

    what fubarable is talking about is changing the code in the class Main to

    int i = Integer.parseInt( settingAccountNumber ); // Translate string to "int"

    but, since an account id may contain hyphens and other non-digits i would change the type of account in the class Data to a string, othewise you risk to get an NumberFormatException if somebody enters 123-456 as account.

  5. #5
    Romally is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Now I understand how to use it. Thank you. I didn't even know you could do that.

    As for the exception, I was planning on forcing a loop to start the code segment again if it != int

Similar Threads

  1. parse string into 2 double variables
    By woodsie2523 in forum New To Java
    Replies: 3
    Last Post: 10-26-2010, 02:46 PM
  2. Replies: 4
    Last Post: 02-22-2009, 05:31 PM
  3. how to Parse int to a string variable
    By raj reddy in forum Java Servlet
    Replies: 10
    Last Post: 01-09-2009, 08:41 PM
  4. how to Parse int to a string variable (pls hlp)
    By raj reddy in forum Threads and Synchronization
    Replies: 5
    Last Post: 06-10-2008, 07:32 AM
  5. How to parse String effectively based on a dilimiter
    By raghu408 in forum Advanced Java
    Replies: 26
    Last Post: 04-10-2008, 11:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •