Page 2 of 2 FirstFirst 12
Results 21 to 40 of 40
  1. #21
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default I'm counting to 1 million...1,2,3....

    HD... honest to Pete, I'm trying to be as patient as possible. That code is NOT the original code with for loops. Now it has JOptionPane all over the place, brackets running amok... I'm counting now... 1, 2, 3, ....
    OK... I'm back.
    Let's start with this code (the ORIGNAL for loop code):
    Java Code:
    [B][COLOR="Red"]for (int x = 1; x <= height; x++)[/COLOR][/B]  //replace this loop with a while loop
      {
         if (x == 1 || x == height)
         {
        for (int y = 1; y <= width; y++)  //replace this loop with a do-while loop
          {
        System.out.print(symbol);
          } //end for loop
         } //end if
      
        else{ 
           System.out.print(symbol);
           for (int w = 2; w <= width-1; w++){
              System.out.print(" ");}
       
           System.out.println(symbol);
        } //end else
         System.out.println();
      [B][COLOR="red"]}//end for[/COLOR][/B]
    What is in red is what is going to be replaced by the while loop. A while loop, as you have noticed, doesn't have an initializer (int x = 1) like the for loop does. You have to provide it separately. It has to be OUT of the scope of the while loop, because otherwise everytime the loop repeats itself "x" will reset to 1 (which you don't want). Therefore "int x = 1;" goes before the while loop starts.
    The next part that the while loop doesn't have is the incrementor (x++). This usually will go at the very end of the while loop scope (HINT: before the while closing bracket... the red bolded one).
    Now, go do the above changes and it should work like it did when you used the for loops. When it works, come back and we will tackle the replacement of the second "for" loop with the do-while loop.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  2. #22
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default

    Wait now you lost me. Are you saying that when I code:
    Java Code:
    int x = 1;
    while(x <= 1)
    {
    statements
    }
    the initializer is NOT outside the while loop?
    How so? it is initialized before the while loop starts isn't it?
    I don't mean to sound stupid but I really don't understand why you are saying it's not outside the loop.

  3. #23
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default my head hurts...

    I just explained the initializer out of the loop so you would understand the reason. Now, if you're refering the mangled code with JOptionPanes in it, yes it is out of the while loop.
    You're not going to follow my suggestions, are you? You insist on using modified code. We could have finished this by now.

    Question: did you put the x++ in the proper place? Put it before the while's closing bracket.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #24
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default

    I'm sorry, I am trying to follow your suggestions, and I do appreciate all your help and patience. I put the x++ right before the closing while tag as you suggested. Now, when I run the program, it prints the character endlessly across the page. The code, as I have it now, is:
    Java Code:
    import javax.swing.JOptionPane;
    import java.util.*;
    import java.io.*;
    
    public class rectangle
    {
       static Scanner console = new Scanner(System.in);
       public static void main(String[] args)throws IOException
       {
           int index = 0;
           String symbol = " ";
           int height = 0;
           int width = 0;
    	    ;
           symbol = JOptionPane.showInputDialog(
           "You will be outputting a rectangle to the screen"
          + "\nEnter the character that you would like to use");
    
          height = Integer.parseInt(JOptionPane.showInputDialog("Please enter the height.\n"));
          width = Integer.parseInt(JOptionPane.showInputDialog("Please enter the width.\n"));
    
          System.out.println("Rectangle  = " + height +" rows by "+ width + " columns");
    	
         int x = 1	    
         while (x <= height)
         {           
    		   {			 
    			  System.out.print(symbol);             
             }
        if (x == 1 || x == height)
         {
    	  
        for (int y = 1; y <= width; y++)  //replace this loop with a do-while loop
          {
            System.out.print(symbol);
    	 
          } //end for loop
         } //end if
      
        else{ 
           System.out.print(symbol);
    		 
           for (int w = 2; w <= width-1; w++){
    		 
              System.out.print(" ");    
           System.out.println(symbol);		
        } //end else
         System.out.println();
    	  x++;
      }//end for
    }
    }
    }
    The reason for the JOptionPane stuff is that I am using dialog boxes to prompt the user for the character and dimensions of the rectangel the program is supposed to draw.

  5. #25
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Screen filler

    Well, at least now you have a creative way of filling screen with symbols (sorry about that... some dark humor :-).
    Ok, back to business...
    The reason it's filling the screen with symbols is because you have bracket issues (rememeber my comments about brackets a few posts back?). Here're the problems:
    Java Code:
    import javax.swing.JOptionPane;
    import java.util.*;
    import java.io.*;
    
    public class rectangle
    {
       static Scanner console = new Scanner(System.in);
       public static void main(String[] args)throws IOException
       {
           int index = 0;
           String symbol = " ";
           int height = 0;
           int width = 0;
    	    ; [B][COLOR="Red"]<- get rid of this[/COLOR][/B]
           symbol = JOptionPane.showInputDialog(
           "You will be outputting a rectangle to the screen"
          + "\nEnter the character that you would like to use");
    
          height = Integer.parseInt(JOptionPane.showInputDialog("Please enter the height.\n"));
          width = Integer.parseInt(JOptionPane.showInputDialog("Please enter the width.\n"));
    
          System.out.println("Rectangle  = " + height +" rows by "+ width + " columns");
    	
         int x = 1 [COLOR="red"][B]<- add ; here[/B][/COLOR]	    
         while (x <= height)
         {           
    		   { [B][COLOR="red"]<-get rid of this[/COLOR][/B]	 
    			  System.out.print(symbol); [B][COLOR="red"]<- get rid of this[/COLOR][/B]            
             } [B][COLOR="red"]<- get rid of this[/COLOR][/B]
        if (x == 1 || x == height)
         {
    	  
        for (int y = 1; y <= width; y++)  //replace this loop with a do-while loop
          {
            System.out.print(symbol);
    	 
          } //end for loop
         } //end if
      
        else{ 
           System.out.print(symbol);
    		 
           for (int w = 2; w <= width-1; w++){
    		 
              System.out.print(" "); 
            [COLOR="red"][B]} <- add this[/B][/COLOR]  
           System.out.println(symbol);		
        } //end else
         System.out.println();
    	  x++; 
      }//end for
       
    }
    }
    } [B][COLOR="red"]<- remove this [/COLOR][/B]
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #26
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default

    Okay, I added and removed where you indicated, with the exception of the semi-colon after the
    Java Code:
    System.out.print(symbol)
    int he while statement (it gave me an error when I compiled with that semi-colon missing).
    I now get the following when I run the program:
    Rectangle = 3 rows by 5 columns
    kkkkkk
    kk k

    kkkkkk
    Why would it be adding a character to each the top and bottom lines and taking them from the middle line like that? Am I still missing something?

  7. #27
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default the whole statement

    I apologize that if I wasn't clear... I meant the WHOLE print statement. It shouldn't be there. Remove the whole print statement. That section of code should look like this:
    Java Code:
    int x = 1;	    
         while (x <= height)
         {           
    		   
        if (x == 1 || x == height)
         {
    	  
        for (int y = 1; y <= width; y++)  //replace this loop with a do-while loop
          {
            System.out.print(symbol);
    	 
          } //end for loop
         } //end if
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #28
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default

    I think maybe I screwed something else up here because even after making the changes and taking out the output statement, I still get the same output when I run the program. Here is my code:
    Java Code:
    import javax.swing.JOptionPane;
    import java.util.*;
    import java.io.*;
    
    public class rectangle
    {
       static Scanner console = new Scanner(System.in);
       public static void main(String[] args)throws IOException
       {
           int index = 0;
           String symbol = " ";
           int height = 0;
           int width = 0;
    	    
           symbol = JOptionPane.showInputDialog(
           "You will be outputting a rectangle to the screen"
          + "\nEnter the character that you would like to use");
    
          height = Integer.parseInt(JOptionPane.showInputDialog("Please enter the height.\n"));
          width = Integer.parseInt(JOptionPane.showInputDialog("Please enter the width.\n"));
    
          System.out.println("Rectangle  = " + height +" rows by "+ width + " columns");
    	
         int x = 1;	    
         while (x <= height)
         {           
           
        if (x == 1 || x == height)
         {
    	  
        for (int y = 1; y <= width; y++)  //replace this loop with a do-while loop
          {
            System.out.print(symbol);
    	 
          } //end for loop
         } //end if
      
        else{ 
           System.out.print(symbol);
    		 
           for (int w = 2; w <= width-1; w++){
    		 
              System.out.print(" ");
    			}     
           System.out.println(symbol);		
        } //end else
         System.out.println();
    	  x++;
      }//end for
    }
    }
    And here is the output:
    Rectangle = 3 rows by 5 columns
    hhhhh
    h h

    hhhhh

  9. #29
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default

    okay I actually got the while statement right but now I'm having a rough time with the do-while statement. I really can't seem to get a grasp on the braces and how they are placed. Here is my new code:
    Java Code:
    import javax.swing.JOptionPane;
    import java.util.*;
    import java.io.*;
    
    public class rectangle
    {
       static Scanner console = new Scanner(System.in);
       public static void main(String[] args)throws IOException
       {
           int index = 0;
           String symbol = " ";
           int height = 0;
           int width = 0;
    	    
           symbol = JOptionPane.showInputDialog(
           "You will be outputting a rectangle to the screen"
          + "\nEnter the character that you would like to use");
    
          height = Integer.parseInt(JOptionPane.showInputDialog("Please enter the height.\n"));
          width = Integer.parseInt(JOptionPane.showInputDialog("Please enter the width.\n"));
    
          System.out.println("Rectangle  = " + height +" rows by "+ width + " columns");
    	
         int x = 1;	    
         while (x <= height)
         {           
           
        if (x == 1 || x == height)
    	 {
         
         int y = 1;
         do 
             {
               if (x == 1 || x == height)
                  { 
    			  	   System.out.print(symbol);
                   } 			
    			     
    				  
               else
                  {
    		   	   System.out.print(symbol);
    								
               for (int w = 2; w <= width-1; w++)
                  {
    		         {System.out.print(" "); 
    					          
                  System.out.println(symbol);}
    				 		
                   
    	           System.out.println();
    	           x++;
    			while (y <= width);
    			}	  
           }       
           }
    I appreciate any feedback, hints or suggestions.

  10. #30
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Don't move the brackets

    You don't need to move the brackets. There's no need to move them. You are recplacing a "for" loop with a "do-while" loop. They do exactly the same thing, therefore there is no need to move the brackets. If you move the brackets, you are changing the scope/reach of the loop.
    I'm counting now... 1,2,3,.....
    OK, I'm back...
    Let's go back to the version of code that was working with the replaced "while" loop. I have highlighted in red the scope of the second "for" loop. You have to do the same thing that you did with the "while" loop.
    Step 1: Replace "for" with the "do while"
    Step 2: Place initializer (int y=1) before the "do-while" loop
    Step 3: Place incrementor (y++) at the end of the "do-while" loop, before the closing bracket
    Step 4: DO NOT MOVE ANY BRACKETS

    Give it a try and see if it works... CJSL

    Java Code:
    import javax.swing.JOptionPane;
    import java.util.*;
    import java.io.*;
    
    public class rectangle
    {
       static Scanner console = new Scanner(System.in);
       public static void main(String[] args)throws IOException
       {
           int index = 0;
           String symbol = " ";
           int height = 0;
           int width = 0;
    	    //;
           symbol = JOptionPane.showInputDialog(
           "You will be outputting a rectangle to the screen"
          + "\nEnter the character that you would like to use");
    
          height = Integer.parseInt(JOptionPane.showInputDialog("Please enter the height.\n"));
          width = Integer.parseInt(JOptionPane.showInputDialog("Please enter the width.\n"));
    
          System.out.println("Rectangle  = " + height +" rows by "+ width + " columns");
    	
         int x = 1;	    
         while (x <= height)
         {           
    		   
        if (x == 1 || x == height)
         {
    	  
        [B][COLOR="red"]for (int y = 1; y <= width; y++)  //replace this loop with a do-while loop
          {[/COLOR][/B]
            System.out.print(symbol);
    	 
          [B][COLOR="red"]} //end of do-while loop[/COLOR][/B]
         } //end if
      
        else{ 
           System.out.print(symbol);
    		 
           for (int w = 2; w <= width-1; w++){
    		 
              System.out.print(" ");  
    	   }
           System.out.println(symbol);		
        } //end else
         System.out.println();
    	  x++;
      }//end for
    	  
    }
    }
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #31
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default

    Believe it or not I finally got it! Thanks so much for all your help and especially for your patience. I think I need a whole lot more practice. In case it may help another here is my final code:
    Java Code:
    import javax.swing.JOptionPane;
    import java.util.*;
    import java.io.*;
    
    public class rectangle
    {
       static Scanner console = new Scanner(System.in);
       public static void main(String[] args)throws IOException
       {
           int index = 0;
           String symbol = " ";
           int height = 0;
           int width = 0;
    	    
           symbol = JOptionPane.showInputDialog(
           "You will be outputting a rectangle to the screen"
          + "\nEnter the character that you would like to use");
    
          height = Integer.parseInt(JOptionPane.showInputDialog("Please enter the height.\n"));
          width = Integer.parseInt(JOptionPane.showInputDialog("Please enter the width.\n"));
    
          System.out.println("Rectangle  = " + height +" rows by "+ width + " columns");
    	
         int x = 1;	    
         while (x <= height)
         {           
           
        if (x == 1 || x == height)
    	 {     
         int y = 1;
         do 
                {
    				 System.out.print(symbol);
    				 y++;
    				} 			
    			     while (y <= width);
    	  }			  
               else
                  {
    		   	   System.out.print(symbol);
    				  			
               for (int w = 2; w <= width-1; w++)
                  {
    		         System.out.print(" "); 
    				  }	          
                  System.out.println(symbol);
    				 }		
                   
    	          System.out.println();
    	           x++;
    	  }
     
     } 
     }
    And thanks again.

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

    Default

    Yeah, you'd better thank CJSLMAN. In my book he has the patience of a saint.

  13. #33
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default

    You are definitely right about that. I am very grateful.:)

  14. #34
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Hey... congratulations !!!

    You're welcome. Now the last part of this and definitely the most important is:
    • Do you understand everything that was done? Do you know why it was done?


    If you can answer yes to those questions, you've learned somethng important. If not, then look for examples and practice until you do understand.

    Some useful links to use (learn how to use them):
    • Sun tutorial:

    The Java™ Tutorials
    • Java SE API 6:

    Java Platform SE 6

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  15. #35
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default

    Again, thank you very much and yes, I think I did learn from this. I got stuck thinking I had to rearrange my braces instead of realizing that the while and do-while statements were nested just as the for statements they replaced were. I also got stuck understanding how the incrementers worked until I wrote out what was happening on paper. Thanks also for the links. I have them bookmarked and will be spending a lot of time using them.

  16. #36
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Exclamation no substitute....

    Quote Originally Posted by hungdukie View Post
    I also got stuck understanding how the incrementers worked until I wrote out what was happening on paper. Thanks also for the links. I have them bookmarked and will be spending a lot of time using them.
    Like I said, there is no substitute, I was too busy or I would have been in there on this like I was with willemjav and Norm. If I may suggest, there will not be a sufficient intake here on what you have learned - not just yet - until, if, and when you work through this several times on paper and in code and in logic - a sort of triune basis. It will not stick otherwise, and you can get your canoe crossways in the creek undil you do.

    So this is "New to Java" ~ I take a slight risk here looking stupid myself, so any new to java reading through here benefit from Original Posters struggle and do the work before you get in a tight way on a holiday == these are called "weekend ruiners" for the obvious reason.

    Quote Originally Posted by hungdukie View Post
    Again, thank you very much and yes, I think I did learn from this. I got stuck thinking I had to rearrange my braces instead of realizing that the while and do-while statements were nested just as the for statements they replaced were.
    Why did you re-arrange you cb's?
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  17. #37
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    Like I said, there is no substitute, I was too busy or I would have been in there on this like I was with willemjav and Norm. If I may suggest, there will not be a sufficient intake here on what you have learned - not just yet - until, if, and when you work through this several times on paper and in code and in logic - a sort of triune basis. It will not stick otherwise, and you can get your canoe crossways in the creek undil you do.

    So this is "New to Java" ~ I take a slight risk here looking stupid myself, so any new to java reading through here benefit from Original Posters struggle and do the work before you get in a tight way on a holiday == these are called "weekend ruiners" for the obvious reason.

    Why did you re-arrange you cb's?
    I rearranged them initially because I got a compiler error saying "reached end of file while parsing" When that happens I have a bad habit of just adding a brace here and there without thinking hard about what I'm doing first.

  18. #38
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Would this be because you are totally lost?

    { Poster and moderators, please note: I am not trying to hammer the poster, I am trying to discover failure modes...this question will break some threads of discussion, there is no intent to do this here - let's see what poster comes back with }
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  19. #39
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default

    Yes, you may be correct, I am totally lost. Since you seem to be a fan of analogies let me paint you one here. I have entered a forest. The forest covers millions of acres. I have a map(a book) but it is poorly drawn. I only got to the forest a little over a month ago. I am trying to find my way. I use my map but when it's not clear, I stop and ask for directions. When I ask directions from someone who has been romping in the forest for years and years directions and that person exclaims, "Oh that's over by the grassy knoll, just past the green meadow!" I do my best to follow their directions but having never been to the grassy knoll or the green meadow and having a very poor picture of them on my map, it may take some further guidance and direction before I can get there. Having said all that, let me ask you a question, Did you romp freely all about the forest by yourself when you first entered it, or did you have some good guides to show you the way?

  20. #40
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Smile books

    In general, a person with not sufficient information will if given a task that is of unknown aspects try guesses. The books ( previously ) have been poorly drawn, today we have better books and the fora ( plural of forum ) provide additional tools. What ( if any books ) are you using and how many fora and websites did you visit before you shifted the braces around. That would be zero, but did you do testing to find out what the movement of the braces did and did you have a grasp of how machinery operates?

    My first five lines of code ( without any guidance from books ) blew out on an STL error... ( STL is another tool in the base c/c++ coding ) - in that few lines of code I saw an old-geezers way of doing things and decided on the spot to never do a string.length() without runaway pointer protection. The code was for what is called a one time pad, to do that one must have a true source of random patterns.

    While working on this, I had a master cryptographer ask me if I intended to store the keys on the machine. I still do not have that question answered. In the process of attempting to answer, I was cited ( he said he would ) in scientific literature. I still do not have the answer. The true answer lay in the field of the human mind. Welcome to search.

    Do you use a compass in the forest? And if so, what happens if you get scared - do you trust the compass? This is known territory in insturment flight training - I do not know the answer, we can address how your training influenced you cb movement attempts. I detest rote, but that may have a place .... I would rather use cut and try, a known and proven shop tool. I do bare knuckle push ups on hot asphalt and ice pack the forearms so that I can try code in greater amounts, I am working on developing a print api that can be used in the browser, your cb shifting could stand some testing.
    Java Code:
    // C:\Documents and Settings\Owner\My Documents\java\Flight_of_the_Phoenix.java
    // This code is a discussion in a technical a discussion and is not complete, nor tested.....
    public class Flight_of_the_Phoenix
    {
        // A cryptographically strong random number generator (RNG).
        private static final String randomizerBase = "SHA1PRNG":
        //
        private static final String availableHashCodeAlgorithm = "SHA-1":
        //
        SecureRandom random;
        // Something handy, I was working on this yesterday.
        private static final Integer a = new Integer(java.lang.Character.getNumericValue('a')); //
        private static final Integer z = new Integer(java.lang.Character.getNumericValue('z')); //
        //
        private static final java.lang.String[][] stringTable =
        {
            {"Here's the code:"},
            {"class HelloWorldApp {"},
            {"public static void main(String[] args) {"},
            {"System.out.println(\"Hello World\"); // Display string."},
            {"}"},
            {"}"},
            {"Let me know what you got, hopefully I didn't make any mistakes,..."},
        }
        // default constructor:
        public Flight_of_the_Phoenix
        {
            random = SecureRandom.getInstance(randomizerBase);
        }
    Want some?
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Page 2 of 2 FirstFirst 12

Similar Threads

  1. How to speed sql Statements?
    By bezudar in forum Advanced Java
    Replies: 3
    Last Post: 11-20-2008, 09:53 AM
  2. Need help - using algorithm statements
    By javanewbie in forum New To Java
    Replies: 2
    Last Post: 07-23-2008, 11:20 AM
  3. avoiding if statements
    By valoyivd in forum New To Java
    Replies: 1
    Last Post: 04-02-2008, 09:08 AM
  4. Help with actionPerformed Statements
    By wco5002 in forum New To Java
    Replies: 8
    Last Post: 03-26-2008, 04:02 AM
  5. Help with if else statements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 07:56 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
  •