Results 1 to 14 of 14
  1. #1
    Logik22 is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default JAVA Beginner - Simple Program help

    Hey, I'm taking a class on JAVA and i'm running into a few problems. I'm using the Eclipse editor. My understanding of JAVA is basic.

    The point of the program is to find out if a 5 digit number is a palindrome. A palindrome is a number that looks the same backwards.

    Examples of Palindromes:
    11511
    25352
    88988
    11111
    28882

    Onto my code:
    Java Code:
    import javax.swing.JOptionPane;
    
    /***********************************************************************
       Program Name: Palindrome.java
       Programmers Name: Brent
       Program Description: This program will check to see if a 5 digit number is a palindrome
       ***********************************************************************/
    
    public class Palindrome {
    
    	public void main()
    	{
    		
    		public static retriveInput()
    		{
    		
    		String number;
    		int convNum;
    		
    		//User inputs the number
    		number = JOptionPane.showInputDialog("Please input a 5-digit number.");
    		
    		//Converts the String to an Integer
    		convNum = Integer.parseInt(number);
    		}
    		//Method to validate the number and check if it's a palindrome
    		public static check()
    		{
    			int valid;
    			//If statement to verify it's 5 digits
    			If(valid = data.length() == 5;){
    				
    			//If statement to see if it's a palindrome
    			If(number.charAt(0) == number.charAt(4) && number.charAt(1) == number.charAt(3)) {
    			
    			JOptionPane.showMessageDialog(null, convNum + "is a palindrome.", "Palindrome",
    					JOptionPane.PLAIN_MESSAGE);
    			
    			} else {
    				JOptionPane.showMessageDialog(null, convNum + "is not a palindrome.", "Not a palindrome",
    						JOptionPane.PLAIN_MESSAGE);
    			}
    			} else {
    				JOptionPane.showMessageDialog(null, "That is not a valid input. Please input a 5-digit number", "Not Valid"
    						JOptionPane.PLAIN_MESSAGE);
    			}
    			
    		}
    		//Close the program
    		System.exit(0);        
    	}
    
    }
    Last edited by Logik22; 07-13-2011 at 11:42 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    Did you have a question?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Logik22 is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Did you have a question?
    Sorry, I guess I should have mentioned that part

    Currently my editor is acting up. I hit RUN and it runs a different program (Same work package, different class/source files) even though I don't have that said source file open.

    My main concern was is there something I can do to clean this code up. What I mean by that is am I making it harder then it is and can I shorten it anyway? I haven't ran the program due to the problem I mentioned above but just by eyeballing it I think it's right for the most part. There may be some syntax errors here and there but they should be easy to resolve.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    The Run button will run whatever the run setup says to run.
    If that is set to the other class then it'll run that one.

    I can't remember if you can do a right click on the class in the explorer bit of Eclipse and run the class that way...
    If not then you might have to dig out the runtime parameter bit. Somewhere...

  5. #5
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    make sure if you're running it in Debug that in the Debug configuration the entry point (the class with main()) is set to your class name you intend to run.

  6. #6
    Logik22 is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    When I go into "Run Configuration". It says my Project name and then under that it says Main Class. For Main Class, it's set to "Largest" (Which is the project that automatically runs. When I click the Search button beside it it only displays 4 different classes (Palindrome is not listed there). It's missing 2 classes (Palindrome and another one called Diamond).

    Up until this point I had just made one project and then every time I wanted to start a new program I would right click on that project (in Package Explorer) and select New Class.

    As I mentioned, I have 6 classes but it only displays 4 of them. There's two things in common with both classes that don't display.

    1) They were the 2 most recent classes I created
    2) They both have multiple errors in them

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    As your code stands now it shouldn't run. You can't define new methods inside other methods and main must be declared as
    Java Code:
    public static void main(String[] args){}
    Also, do you have to check the numbers mathematically? Because if not a string builder makes it trivial to test a number for palindromeness.

    Although this is merely my opinion I will say it anyway. You may want to consider using a simpler text editor and the terminal to compile rather than using an IDE. Then everytime you want a new class, make a new file and start fresh. You will also be forced to remember many things the IDE does for you(which is good when you are new).
    Last edited by sunde887; 07-13-2011 at 11:27 PM.

  8. #8
    Logik22 is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    So since I can't define new methods inside of main my code should look something like this

    Java Code:
    import javax.swing.JOptionPane;
    
    /***********************************************************************
       Program Name: Palindrome.java
       Programmers Name: Brent
       Program Description: This program will check to see if a 5 digit number is a palindrome
       ***********************************************************************/
    
    public class Palindrome {
    
    	public static void main(String[] args)
    	{
    		
    	       
    	}
    	
    	public static retriveInput()
    	{
    	
    	String number;
    	int convNum;
    	
    	//User inputs the number
    	number = JOptionPane.showInputDialog("Please input a 5-digit number.");
    	
    	//Converts the String to an Integer
    	convNum = Integer.parseInt(number);
    	}
    	//Method to validate the number and check if it's a palindrome
    	public static check()
    	{
    		int valid;
    		//If statement to verify it's 5 digits
    		If(valid = data.length() == 5){
    			
    		//If statement to see if it's a palindrome
    		If(number.charAt(0) == number.charAt(4) && number.charAt(1) == number.charAt(3)) {
    		
    		JOptionPane.showMessageDialog(null, convNum + "is a palindrome.", "Palindrome",
    				JOptionPane.PLAIN_MESSAGE);
    		
    		} else {
    			JOptionPane.showMessageDialog(null, convNum + "is not a palindrome.", "Not a palindrome",
    					JOptionPane.PLAIN_MESSAGE);
    		}
    		} else {
    			JOptionPane.showMessageDialog(null, "That is not a valid input. Please input a 5-digit number", "Not Valid"
    					JOptionPane.PLAIN_MESSAGE);
    		}
    		
    		
    	}
    	
    	//Close the program
    	System.exit(0); 
    }
    Now i'm guess I must make some type of reference to the methods within the main() method but i'm not sure how to do that.

    As far as checking the input mathematically, I don't think that will need to take place. It doesn't have to change anything with the number, just check some logic against it.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    By mathematically, I meant split the number into it's digits and compare it that way, but the point is invalid since that's not the approach you are taking. However, you can make the comparisons much quicker with a string builder.

    You call the methods inside main. Since they are static you can call them directly without creating an instance of the class.
    Last edited by sunde887; 07-13-2011 at 11:55 PM.

  10. #10
    Logik22 is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    By mathematically, I meant split the number into it's digits and compare it that way, but the point is moot since that's not the approach you are taking. However, you can make the comparisons much quicker with a string builder.

    You call the methods inside main. Since they are static you can call them directly without creating an instance of the class.
    No, I found some information on how to specify which digit to check so I should be fine in that department.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Alright, was just really pointing out that all the logic can be replaced with one or two lines.

    The programs main method should be something like this
    Java Code:
    public static void main(String[] args){
      methodOne();
      methodTwo();
      methodThree();
      ...
    }
    This assumes you are using static methods contained in the same class, if they are not you want to prefix them with the class name, ClassName.methodName()

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Java Code:
            }
    	//Close the program
    	System.exit(0); 
    }
    Another error.

    Lines of code must be inside a method or constructor. Only things that can be outside are variable declarations, initialiser and static blocks (which are beyond your understand at this point).

  13. #13
    Logik22 is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    edit: double post
    Last edited by Logik22; 07-14-2011 at 03:58 PM.

  14. #14
    acmohan is offline Member
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default

    i have read somewhere not sure just try this and explore on this ok."StringBuilder(word).reverse().toString().equal s(word)" will give result for palindrome.

Similar Threads

  1. Simple Java program
    By Rolle in forum New To Java
    Replies: 3
    Last Post: 10-26-2009, 05:05 PM
  2. Replies: 1
    Last Post: 03-15-2009, 04:50 PM
  3. Replies: 2
    Last Post: 05-04-2008, 06:27 PM
  4. help with simple program in java
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 09:03 PM
  5. help with simple java program
    By leonard in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 10:40 AM

Posting Permissions

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