Results 1 to 14 of 14
- 07-13-2011, 08:04 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 24
- Rep Power
- 0
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 10:42 PM.
- 07-13-2011, 08:08 PM #2
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!
- 07-13-2011, 08:14 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 24
- Rep Power
- 0
Sorry, I guess I should have mentioned that part
.gif)
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.
- 07-13-2011, 08:22 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,468
- Rep Power
- 16
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...
- 07-13-2011, 08:33 PM #5
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.
- 07-13-2011, 09:44 PM #6
Member
- Join Date
- Jul 2011
- Posts
- 24
- Rep Power
- 0
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
- 07-13-2011, 10:22 PM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
As your code stands now it shouldn't run. You can't define new methods inside other methods and main must be declared as
Also, do you have to check the numbers mathematically? Because if not a string builder makes it trivial to test a number for palindromeness.Java Code:public static void main(String[] args){}
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 10:27 PM.
- 07-13-2011, 10:41 PM #8
Member
- Join Date
- Jul 2011
- Posts
- 24
- Rep Power
- 0
So since I can't define new methods inside of main my code should look something like this
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.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); }
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.
- 07-13-2011, 10:53 PM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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 10:55 PM.
- 07-13-2011, 10:56 PM #10
Member
- Join Date
- Jul 2011
- Posts
- 24
- Rep Power
- 0
- 07-13-2011, 10:59 PM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
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()Java Code:public static void main(String[] args){ methodOne(); methodTwo(); methodThree(); ... }
- 07-14-2011, 12:30 AM #12
Another error.Java Code:} //Close the program System.exit(0); }
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).
- 07-14-2011, 02:18 PM #13
Member
- Join Date
- Jul 2011
- Posts
- 24
- Rep Power
- 0
edit: double post
Last edited by Logik22; 07-14-2011 at 02:58 PM.
- 07-15-2011, 02:44 PM #14
Member
- Join Date
- Jul 2011
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
Simple Java program
By Rolle in forum New To JavaReplies: 3Last Post: 10-26-2009, 04:05 PM -
Java beginner, need some help on a simple tutorial? :D
By mootxico in forum New To JavaReplies: 1Last Post: 03-15-2009, 03:50 PM -
Help with a very simple method for a very simple beginner.
By cakeman in forum New To JavaReplies: 2Last Post: 05-04-2008, 05:27 PM -
help with simple program in java
By katie in forum New To JavaReplies: 2Last Post: 08-06-2007, 08:03 PM -
help with simple java program
By leonard in forum New To JavaReplies: 3Last Post: 07-30-2007, 09:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks