Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #16  
Old November 15th, 2005, 01:56 PM
crownjewel82's Avatar
crownjewel82 crownjewel82 is offline
rebel with a cause
Dev Shed God (5000 - 5499 posts)
 
Join Date: May 2004
Location: The Batsh!t Crazy State.
Posts: 5,349 crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)  Folding Points: 32039 Folding Title: Starter FolderFolding Points: 32039 Folding Title: Starter Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 16 h 11 m 14 sec
Reputation Power: 1935
Be proud of yourself it looks good so far.

To answer your question:
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.
Code:
boolean right = false;
while (right) {
  //enter word1:
  if (word1.length() <= 37) right = true;
}
__________________
The day I get my hands on the cookbook it's all over. -nicky

Reply With Quote
  #17  
Old November 15th, 2005, 01:56 PM
bullet's Avatar
bullet bullet is offline
Java Junkie
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jan 2004
Location: Mobile, Alabama
Posts: 3,267 bullet User rank is Captain (20000 - 30000 Reputation Level)bullet User rank is Captain (20000 - 30000 Reputation Level)bullet User rank is Captain (20000 - 30000 Reputation Level)bullet User rank is Captain (20000 - 30000 Reputation Level)bullet User rank is Captain (20000 - 30000 Reputation Level)bullet User rank is Captain (20000 - 30000 Reputation Level)bullet User rank is Captain (20000 - 30000 Reputation Level)bullet User rank is Captain (20000 - 30000 Reputation Level)bullet User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 13 h 26 m 35 sec
Reputation Power: 307
Send a message via ICQ to bullet Send a message via AIM to bullet Send a message via MSN to bullet
Quote:
Originally Posted by mvantuyl
Actually, 'while' is a reserved word, but 'While' isn't. (But it still isn't a good idea to use 'While' as a class name)


True, I didn't catch that. Sorry for the mistake.

Reply With Quote
  #18  
Old November 15th, 2005, 01:59 PM
crownjewel82's Avatar
crownjewel82 crownjewel82 is offline
rebel with a cause
Dev Shed God (5000 - 5499 posts)
 
Join Date: May 2004
Location: The Batsh!t Crazy State.
Posts: 5,349 crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)  Folding Points: 32039 Folding Title: Starter FolderFolding Points: 32039 Folding Title: Starter Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 16 h 11 m 14 sec
Reputation Power: 1935
Something else I noticed:
You've got a stray while statement at the end there. Were you going for a do while loop? If so it's really not necessary.

Reply With Quote
  #19  
Old November 15th, 2005, 03:06 PM
Yawmark's Avatar
Yawmark Yawmark is offline
Feelin' Groovy
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Aug 2001
Location: WDSMIA
Posts: 7,625 Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 1 Day 13 h 56 m 33 sec
Reputation Power: 1344
Send a message via ICQ to Yawmark Send a message via MSN to Yawmark
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.

Java Code:
Original - Java Code
  1. public class Foo {   
  2.    
  3.     private Foo() { } // prevent instantiation
  4.  
  5.     public static void main(String[] args) throws Exception {
  6.         new Foo().go();       
  7.     }
  8.    
  9.     void go() {
  10.         String firstWord  = getWordFromUser();
  11.         String secondWord = getWordFromUser();
  12.         displayWords(firstWord, secondWord);
  13.     }
  14.  
  15. } 


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...

Java Code:
Original - Java Code
  1.  
  2.     private String getWordFromUser() {
  3.         return "something";
  4.     }
  5.    
  6.     private void displayWords(String first, String second) {
  7.         System.out.println("first: " + first + "\nsecond: " + second);
  8.     }


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:

Java Code:
Original - Java Code
  1.     private String getWordFromUser() {
  2.         String input = getStringInput();
  3.         while(isTooLong(input)) {
  4.             notifyUserStringTooLong();
  5.             input = getStringInput();
  6.         }
  7.         return input;
  8.     }


Again, we've got more undefined methods, so let's stub it out...

Java Code:
Original - Java Code
  1.     private String getStringInput() {
  2.         return "foo";
  3.     }
  4.    
  5.     private boolean isTooLong(String input) {
  6.         return false;
  7.     }
  8.    
  9.     private void notifyUserStringTooLong() {
  10.         //...
  11.     }
  12.            
  13.     private void quit() {
  14.         System.exit(0);
  15.     }


So now, we've got a compiling and running program...

Java Code:
Original - Java Code
  1. public class Foo {   
  2.    
  3.     private Foo() { } // prevent instantiation
  4.  
  5.     public static void main(String[] args) throws Exception {
  6.         new Foo().go();       
  7.     }
  8.    
  9.     void go() {
  10.         String firstWord  = getWordFromUser();
  11.         String secondWord = getWordFromUser();
  12.         displayWords(firstWord, secondWord);
  13.     }
  14.    
  15.     private String getWordFromUser() {
  16.         String input = getStringInput();
  17.         while(isTooLong(input)) {
  18.             notifyUserStringTooLong();
  19.             input = getStringInput();
  20.         }
  21.         return input;
  22.     }       
  23.    
  24.     private String getStringInput() {
  25.         return "foo";
  26.     }
  27.    
  28.     private boolean isTooLong(String input) {
  29.         return false;
  30.     }
  31.    
  32.     private void notifyUserStringTooLong() {
  33.         //...
  34.     }
  35.            
  36.     private void quit() {
  37.         System.exit(0);
  38.     }
  39.    
  40.     private void displayWords(String first, String second) {
  41.         System.out.println("first:  " + first + "\nsecond: " + second);
  42.     }
  43.  
  44. }


So now, we can go in and handle each small stub...

Java Code:
Original - Java Code
  1.     private String getStringInput() {
  2.         String input = JOptionPane.showInputDialog("Please enter a word");
  3.         if (input == null) quit();
  4.         return input;
  5.     }
  6.    
  7.     private boolean isTooLong(String input) {
  8.         return input.length() > MAX_INPUT_LENGTH;
  9.     }
  10.    
  11.     private void notifyUserStringTooLong() {
  12.         JOptionPane.showMessageDialog(null,
  13.             "Input can't be longer than " + MAX_INPUT_LENGTH + " characters");
  14.     }
  15.            
  16.     private void quit() {
  17.         System.exit(0);
  18.     }


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....

Java Code:
Original - Java Code
  1. import javax.swing.JOptionPane;
  2.  
  3. public class Foo { 
  4.    
  5.     private final int MAX_INPUT_LENGTH = 37;
  6.    
  7.     private Foo() { } // prevent instantiation
  8.  
  9.     public static void main(String[] args) throws Exception {
  10.         new Foo().go();       
  11.     }
  12.    
  13.     void go() {
  14.