Results 21 to 40 of 40
- 12-07-2008, 01:41 AM #21
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):
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.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]
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.
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-07-2008, 02:05 AM #22
Member
- Join Date
- Nov 2008
- Posts
- 32
- Rep Power
- 0
Wait now you lost me. Are you saying that when I code:
the initializer is NOT outside the while loop?Java Code:int x = 1; while(x <= 1) { statements }
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.
- 12-07-2008, 02:35 AM #23
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.
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-07-2008, 02:47 AM #24
Member
- Join Date
- Nov 2008
- Posts
- 32
- Rep Power
- 0
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:
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.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 } } }
- 12-07-2008, 04:59 AM #25
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.
- 12-07-2008, 05:23 AM #26
Member
- Join Date
- Nov 2008
- Posts
- 32
- Rep Power
- 0
Okay, I added and removed where you indicated, with the exception of the semi-colon after the
int he while statement (it gave me an error when I compiled with that semi-colon missing).Java Code:System.out.print(symbol)
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?
- 12-07-2008, 05:37 AM #27
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 ifChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-07-2008, 05:58 AM #28
Member
- Join Date
- Nov 2008
- Posts
- 32
- Rep Power
- 0
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:
And here is the output: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 } }
Rectangle = 3 rows by 5 columns
hhhhh
h h
hhhhh
- 12-07-2008, 08:28 AM #29
Member
- Join Date
- Nov 2008
- Posts
- 32
- Rep Power
- 0
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:
I appreciate any feedback, hints or suggestions.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); } } }
- 12-07-2008, 03:04 PM #30
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.
- 12-07-2008, 05:20 PM #31
Member
- Join Date
- Nov 2008
- Posts
- 32
- Rep Power
- 0
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:
And thanks again.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++; } } }
-
Yeah, you'd better thank CJSLMAN. In my book he has the patience of a saint.
- 12-07-2008, 05:51 PM #33
Member
- Join Date
- Nov 2008
- Posts
- 32
- Rep Power
- 0
You are definitely right about that. I am very grateful.:)
- 12-07-2008, 07:11 PM #34
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-07-2008, 08:05 PM #35
Member
- Join Date
- Nov 2008
- Posts
- 32
- Rep Power
- 0
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.
- 12-09-2008, 09:54 PM #36
no substitute....
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?Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 12-09-2008, 10:15 PM #37
Member
- Join Date
- Nov 2008
- Posts
- 32
- Rep Power
- 0
- 12-10-2008, 12:40 AM #38
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
- 12-10-2008, 02:43 AM #39
Member
- Join Date
- Nov 2008
- Posts
- 32
- Rep Power
- 0
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?
- 12-10-2008, 02:08 PM #40
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.Want some?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); }Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Similar Threads
-
How to speed sql Statements?
By bezudar in forum Advanced JavaReplies: 3Last Post: 11-20-2008, 09:53 AM -
Need help - using algorithm statements
By javanewbie in forum New To JavaReplies: 2Last Post: 07-23-2008, 11:20 AM -
avoiding if statements
By valoyivd in forum New To JavaReplies: 1Last Post: 04-02-2008, 09:08 AM -
Help with actionPerformed Statements
By wco5002 in forum New To JavaReplies: 8Last Post: 03-26-2008, 04:02 AM -
Help with if else statements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 07:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks