Page 2 - Discuss Java basic program guidance in the Java Help forum on Dev Shed. Java basic program guidance Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
Posts: 10,135
Time spent in forums: 3 Months 1 Week 4 Days 1 h 22 m 45 sec
Reputation Power: 5052
Quote:
You want your end loop condition to be what you're using to see if it's right. So the loop ends when the input is right.
It's never too soon to talk about design and/or refactoring. I generally like to break methods up into fairly atomic units of work. Again, once you have a decent algorithm, the code practically writes itself. Here's an example:
Pseudocode:
1) Ask the user for a word
2) Ask the user for another word
3) Display the words
That's the basic idea of the program. We'll see that each step has its own set of requirements, but let's just start with this algorithm of program flow.
Okay, this obviously won't compile (the methods aren't defined), but I'm not too worried about it. This pretty much covers the main program, right? Now, we just need to define those methods. We'll write some basic methods as "stubs", just to get a working program...
System.out.println("first: " + first + "\nsecond: " + second);
}
Okay, this compiles and runs, so we can see that we've got a working skeleton. Let's tackle the next small piece: user input. Once again, let's write up our algorithm in pseudocode:
1) Get some input from the user
2) While the input is invalid
2a) Notify user of the error
2b) Try to get good input
Translated to Java, we get the following for our getWordFromUser() method:
You'll notice the MAX_INPUT_LENGTH variable. It's a constant I chose to define because I loathe Magic Numbers. So once I define all that, I've got another compilable, runnable class I can test....
System.out.println("first: " + first + "\nsecond: " + second);
}
}
Hopefully you're getting the idea here. Solve your problems by tackling one small thing at a time. Think of the algorithm first, then write code in a stepwise fashion. Test each little bit as you go. Hopefully this simple example will start you well on your way toward completing your program.
Posts: 5,817
Time spent in forums: 3 Months 4 Days 12 h 16 m 39 sec
Reputation Power: 3460
If I ever get off my lazy behind and do it. I plan on having a programming tutorial site with my methodology. Might even publish it into a book. "How to Program the Right Way"
Posts: 19
Time spent in forums: 5 h 35 m 15 sec
Reputation Power: 0
Ok I have tidied it up abit I think But I still cant seem to get it to loop back to the inputs if there is an error in the length of the word. I really appreciate the help so far aswell, it has giving me a much better understanding of programming.
Code:
import javax.swing.*;
class Homework9
{
public static void main (String args[])
{
int length1, length2;
//input a word
String input1 = JOptionPane.showInputDialog("Enter first word");
length1 = input1.length();
if (input1.length() > 37)
//prints an error message if the length of word 1 exceeds 37 characters
JOptionPane.showMessageDialog(null,"Error",
"Error word length exceeds 37", JOptionPane.ERROR_MESSAGE);
//input a second word
String input2 = JOptionPane.showInputDialog("Enter second word");
length2 = input2.length();
if (input2.length() > (38-length1))
//prints an error message if the length of word 1 and word 2 exceeds 38 characters
JOptionPane.showMessageDialog(null,"Error total length of words exceeds 38 characters",
"Error", JOptionPane.ERROR_MESSAGE);
//how long is input1
int i = input1.length();
//print input 1
System.out.print(input1);
//keep printing dots till there is only room left for input 2
while (i < (40 - input2.length()))
{
System.out.print(".");
i++;
}
//print input 2
System.out.println(input2);
}
}
Posts: 10,135
Time spent in forums: 3 Months 1 Week 4 Days 1 h 22 m 45 sec
Reputation Power: 5052
Quote:
Ok I have tidied it up abit I think
Everything is still piled into the main method, and begs to be refactored. Each method should do one thing, and do it well. Don't make your main method work so hard...
Quote:
But I still cant seem to get it to loop back to the inputs if there is an error in the length of the word.
Either you haven't yet developed an understanding of what the algorithm for such an action should be, or you don't understand how to translate that algorithm to Java. Either way, you haven't written your program to "loop back to the inputs"; you must tell the computer what you want it to do, and there's nothing in your example that says "keep trying to get input if it isn't correct". Please study the code I posted, especially the getWordFromUser() method.
__________________ Yawmark
class Sig{public static void main(String...args){\u0066or(int
\u0020$:"vÌÈÊ\"¤¾Àʲ¬Æ\"v¤Î¤\"²¤¨¸¬Æ".to\u0043h\u0061rArray()
)System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
+(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
Last edited by Yawmark : November 16th, 2005 at 07:24 AM.
Posts: 19
Time spent in forums: 5 h 35 m 15 sec
Reputation Power: 0
Sorted it, I was using if's instead of whiles and I have broken it down some. Thanks alot for the input guys, I like how you didnt just drop an answer to my question without trying to make me understand what it was I was trying to achieve. I will no doubt have many more questions as I try and gain a wider understanding of Java.
Posts: 643
Time spent in forums: 5 Days 10 h 43 m 3 sec
Reputation Power: 10
This topic is great!
I acutally read the whole thing.
I'm really getting into Java not with as much difficulty just because I have programmed before. I'm not so sure Java is the best language to start with.
Years ago I used "Learn To Program Basic". Fun stuff.