Results 1 to 16 of 16
Thread: Printing issues...
- 02-13-2009, 03:24 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 27
- Rep Power
- 0
Printing issues...
I have created a loop structure in my testing class, and after I want it to print out the output, but it does not seem to print. Any ideas?
Here is my code:
Quote:
--------------------------------------------------------------------------------
/**
*
*/
package frogs;
/**
* @author epena
*
*/
public class FrogTester {
public static void main(String[] args) {
Frog myFrog = new Frog();
System.out.println("Position :" + myFrog.getPosition());
System.out.println("Maximum Position: " + myFrog.getMaximumPosition());
System.out.println("Mimimum Position: " + myFrog.getMinimumPosition());
System.out.println("Hops Taken: " +myFrog.getHopsTaken());
//Do this for hops
Frog kermit = new Frog();
for (int i=0; i<100;i++){
kermit.hop();
}
System.out.println("After 100 hops: \n" + kermit);
int posSum = 0;
int distSum = 0;
int minPosSum = 0;
int maxPosSum = 0;
int numFrogs = 10000;
System.out.println("Experiement: 10 frogs hopping 100 times");
for (int i=0; i <numFrogs; i++){
Frog frog = new Frog();
hopFrog(frog, 100);
posSum += frog.getPosition();
distSum += Math.abs(frog.getPosition());
minPosSum += frog.getMinimumPosition();
maxPosSum += frog.getMaximumPosition();
}
System.out.println("Average Final Position " + posSum / (double)numFrogs);
System.out.println("Average Final Distance " + distSum / (double)numFrogs);
System.out.println("Average Final Minimum Position " + minPosSum / (double)numFrogs);
System.out.println("Average Final Maximum Position " + maxPosSum / (double)numFrogs);
Frog thisFrog = new Frog();
thisFrog.hop(1000);
System.out.println("This Frog: " + thisFrog);
}
public static void hopFrog (Frog thisFrog, int n){
for (int i =0; i < n; i ++);
thisFrog.hop();
}
Frog kirby = new Frog();{
int q=0;
while(q == 50){
kirby.hop(50);}
System.out.println("Kirby : "+ kirby);
}
}
--------------------------------------------------------------------------------
It compiles and runs just fine, but I do not get the last print message. System.out.println("Kirby : "+ kirby); does not print out...
In addition, I am supposed to Use a while loop to have kirby hop until it reaches position 50. Output the number of hops that it requires.
Did that do that correctly?
- 02-13-2009, 04:01 PM #2
1. use CODE tags like this <CODE> code here </CODE> Except replace the < and > with [ and ].
I have been looking over your code so annoying trying to figure out where methods end and start.
2. Its fine to post the whole class in this case since it's small, but put the part with error in red, or in bold, or underline etc.
3. What is the kirby supposed to print out? Kirby is an object, not a string. What was the expected output?
4. Just a tip, try to break the program into methods, not all in the main. And make constructors and fields. Ok now I see that there is a hop method...
5. A suggestion, You're using a lot of frog objects but your only method is static with a frog argument.. it would be much simpler to make it non-static and then the calling frog object will be used.
6. A suggestion, You have no FrogTester constructor. If you plan it to be empty anyway, it is still good practice to put in an empty constructor, and not use the default one included when there isn't one.
7. I've now identified your problem:
The code Frog Kirby is making a new Frog field in your class called kirby - ok..Java Code:Frog kirby = new Frog();[COLOR="Red"]{[/COLOR] int q=0; while(q == 50){ kirby.hop(50);} System.out.println("Kirby : "+ kirby); [COLOR="red"]}[/COLOR]
The problem: those red braces, what are they? You have code outside any methods - there just in braces. Did you mean to include that in the main Method? You can't just have stray code like that. Hope this helped.
-MK12Last edited by MK12; 02-13-2009 at 04:04 PM.
Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-13-2009, 05:18 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 27
- Rep Power
- 0
I am sorry, I did not put in the Frog class, I just put in the main method. Here is the frog class with the constructors and methods.
Java Code:/** * */ package frogs; import java.util.*; public class Frog { private int hopsTaken; private int position; private int minimumPosition; private int maximumPosition; private static Random rand = new Random(); //Constructor public Frog (){ hopsTaken = 0; position = 0; minimumPosition = 0; maximumPosition = 0; } public int getPosition(){ return position; } public int getHopsTaken(){ return hopsTaken; } public int getMinimumPosition(){ return minimumPosition; } public int getMaximumPosition(){ return maximumPosition; } public void hop (int n){ for (int i=0; i < n; i++) this.hop(); } public void hop(){ int nextVal; nextVal = rand.nextInt(2); if (nextVal == 0) position++; else position--; hopsTaken++; if (position < minimumPosition) minimumPosition = position; if (position < maximumPosition) maximumPosition = position; } public String toString(){ String output = "Frog at " + position + " after " + hopsTaken + " hops "; output += "with minimum position " + minimumPosition + " and maximum position " + maximumPosition; return output; } }
- 02-13-2009, 05:26 PM #4
Member
- Join Date
- Feb 2009
- Posts
- 27
- Rep Power
- 0
In addition, I have fixed the braces, but the last print command will not print out. I left a comment, also is my while loop correct??
I am supposed to Use a while loop to have kirby hop until it reaches position 50. Output the number of hops that it requires.
Did that do that correctly?Java Code:/** * */ package frogs; /** * @author epena * */ public class FrogTester { /** * @param args */ public static void main(String[] args) { System.out.println("FrogTester written by Evan Pena"); Frog myFrog = new Frog(); System.out.println("Position :" + myFrog.getPosition()); System.out.println("Maximum Position: " + myFrog.getMaximumPosition()); System.out.println("Mimimum Position: " + myFrog.getMinimumPosition()); System.out.println("Hops Taken: " +myFrog.getHopsTaken()); //Do this for hops Frog kermit = new Frog(); for (int i=0; i<100;i++){ kermit.hop(); } System.out.println("After 100 hops: \n" + kermit); int posSum = 0; int distSum = 0; int minPosSum = 0; int maxPosSum = 0; int numFrogs = 10000; System.out.println("Experiement: 10 frogs hopping 100 times"); for (int i=0; i <numFrogs; i++){ Frog frog = new Frog(); hopFrog(frog, 100); posSum += frog.getPosition(); distSum += Math.abs(frog.getPosition()); minPosSum += frog.getMinimumPosition(); maxPosSum += frog.getMaximumPosition(); } System.out.println("Average Final Position " + posSum / (double)numFrogs); System.out.println("Average Final Distance " + distSum / (double)numFrogs); System.out.println("Average Final Minimum Position " + minPosSum / (double)numFrogs); System.out.println("Average Final Maximum Position " + maxPosSum / (double)numFrogs); Frog thisFrog = new Frog(); thisFrog.hop(1000); System.out.println("This Frog: " + thisFrog); } public static void hopFrog (Frog thisFrog, int n){ for (int i =0; i < n; i ++); thisFrog.hop(); } Frog kirby = new Frog(); int q=0;{ while(q == 50) kirby.hop(50); System.out.println("Kirby : "+ kirby); //This is the part that is not printing. } }
- 02-13-2009, 05:42 PM #5
First of all, I think you meant to do kirby.toString(), not just kirby. Secondly,
It's still outside any method. look, this is incorrect:Java Code:Frog kirby = new Frog(); int q=0;{ while(q == 50) kirby.hop(50); System.out.println("Kirby : "+ kirby); //This is the part that is not printing. }
Understand now?Java Code:public class Example { private static int field; // declaring fields public static void main(String[] args) { field = 1; } { System.out.println("h");} // INCORRECT. braces make no difference System.out.println("h"); // both these lines are wrong. They are not in a method // you can't have code like that outside a method. } // HERE's the right way: public class Example { private static int field; // declaring fields public static void main(String[] args) { field = 1; runApp(); } private static void runApp() { System.out.println("h"); } } // OR another right way: public class Example { private static int field; // declaring fields public static void main(String[] args) { field = 1; System.out.println("h"); } }
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-13-2009, 05:49 PM #6
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
fyi, if you println(Object), it automatically uses the .toString() method. pretty handy and not exactly the most widely known tidbit (i learned this a couple months ago... though i've never really found myself in a situation where i wanted to use it). it would be more useful if it was also extended to automatically call .toString() in any case where String is expected, though that'd probably a hell of a lot more annoying to debug than to call .toString() whenever you wanted... ok i'll stop.
- 02-13-2009, 06:04 PM #7
Member
- Join Date
- Feb 2009
- Posts
- 27
- Rep Power
- 0
Even when I call the toString method on Kirby it still does not print it out. However, if you look at above Kirby you will find an object called thisFrog. The way I printed thisFrog works just fine. Try running the program and seeing what you get.
- 02-13-2009, 06:16 PM #8
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
my post was actually pointed to mk12 (he said to use kirby.toString(), not just kirby). but if you read mk12's post, you'd see the solution to your problem.
- 02-13-2009, 06:25 PM #9
Member
- Join Date
- Feb 2009
- Posts
- 27
- Rep Power
- 0
Ahh It is still not printing and I am confident it is within the main class
[code]
/**
*
*/
package frogs;
/**
* @author epena
*
*/
public class FrogTester {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("FrogTester written by Evan Pena");
Frog myFrog = new Frog();
System.out.println("Position :" + myFrog.getPosition());
System.out.println("Maximum Position: " + myFrog.getMaximumPosition());
System.out.println("Mimimum Position: " + myFrog.getMinimumPosition());
System.out.println("Hops Taken: " +myFrog.getHopsTaken());
//Do this for hops
Frog kermit = new Frog();
for (int i=0; i<100;i++){
kermit.hop();
}
System.out.println("After 100 hops: \n" + kermit);
int posSum = 0;
int distSum = 0;
int minPosSum = 0;
int maxPosSum = 0;
int numFrogs = 10000;
System.out.println("Experiement: 10 frogs hopping 100 times");
for (int i=0; i <numFrogs; i++){
Frog frog = new Frog();
hopFrog(frog, 100);
posSum += frog.getPosition();
distSum += Math.abs(frog.getPosition());
minPosSum += frog.getMinimumPosition();
maxPosSum += frog.getMaximumPosition();
}
System.out.println("Average Final Position " + posSum / (double)numFrogs);
System.out.println("Average Final Distance " + distSum / (double)numFrogs);
System.out.println("Average Final Minimum Position " + minPosSum / (double)numFrogs);
System.out.println("Average Final Maximum Position " + maxPosSum / (double)numFrogs);
Frog thisFrog = new Frog();
thisFrog.hop(1000);
System.out.println("This Frog: " + thisFrog);
Frog kirby = new Frog();
int q=0;
while(q == 50)
kirby.hop(50);
kirby.toString();
//This is the part that is not printing.
}
public static void hopFrog (Frog thisFrog, int n){
for (int i =0; i < n; i ++);
thisFrog.hop();
}
}
[code]
- 02-13-2009, 06:27 PM #10
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
well, now you aren't even printing it. you're just converting it to a string and doing nothing with it.
- 02-13-2009, 06:49 PM #11
Member
- Join Date
- Feb 2009
- Posts
- 27
- Rep Power
- 0
haha sorry, that was a dumb mistake.
Unfortunatly when I print the message I get this:
So there must be something wrong with my while loop.Kirby: Frog at 0 after 0 hops with minimum position 0 and maximum position 0
I am supposed to Use a while loop to have kirby hop until it reaches position 50. Output the number of hops that it requires.
Did that do that correctly?
- 02-13-2009, 07:09 PM #12
So you figured out that I didn't mean it has tot be in the class, but that it must be in a method? all code except declaring & initializing fields must be in a method. For example, putting the println() in the main(String[] args) method, or in another method which the main calls (if you want it to run). OK. (bytheway you forgot CODE tags in your post) You fixed the mistake that mcn pointed out, so now it actually prints out the frog object,right? (and just type the object not .toString() because it automatically does that, as mcn informed us)... Ok:
What is it supposed to print out? as far as I can see you are only printing out the Frog object, which prints its toString method, which is this:Unfortunatly when I print the message I get this:
Kirby: Frog at 0 after 0 hops with minimum position 0 and maximum position 0
So, isn't what you said it is printing out what it is supposed to print out? explain please. Also, on your while loop, you left out braces. Please post you're up-to-date code with code tags.Java Code:public String toString(){ String output = "Frog at " + position + " after " + hopsTaken + " hops "; output += "with minimum position " + minimumPosition + " and maximum position " + maximumPosition; return output; }
That is your while loop(with the braces left out), and there is another problem: It will never execute because q is 0 and it only goes through loop while q is 50. did you mean:Java Code:int q=0; while(q == 50)
Hope this helpedJava Code:Frog kirby = new Frog(); int q=0; while(q [COLOR="Red"]<=[/COLOR] 50) [COLOR="red"]{[/COLOR] kirby.hop(50); [COLOR="red"]System.out.println[/COLOR]([COLOR="red"]kirby[/COLOR]); [COLOR="Red"]q++;[/COLOR] [COLOR="red"]}[/COLOR]
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-13-2009, 08:29 PM #13
Member
- Join Date
- Feb 2009
- Posts
- 27
- Rep Power
- 0
When I print out the loop you gave me it prints this
I want it just to print one line...Frog at 4 after 50 hops with minimum position -6 and maximum position -6
Frog at 0 after 100 hops with minimum position -6 and maximum position -6
Frog at 8 after 150 hops with minimum position -6 and maximum position -6
Frog at 4 after 200 hops with minimum position -6 and maximum position -6
Frog at 0 after 250 hops with minimum position -6 and maximum position -6
Frog at 2 after 300 hops with minimum position -6 and maximum position -6
Frog at 14 after 350 hops with minimum position -6 and maximum position -6
Frog at 30 after 400 hops with minimum position -6 and maximum position -6
Frog at 20 after 450 hops with minimum position -6 and maximum position -6
Frog at 18 after 500 hops with minimum position -6 and maximum position -6
Frog at 14 after 550 hops with minimum position -6 and maximum position -6
Frog at 10 after 600 hops with minimum position -6 and maximum position -6
Frog at 0 after 650 hops with minimum position -6 and maximum position -6
Frog at -10 after 700 hops with minimum position -12 and maximum position -12
Frog at -4 after 750 hops with minimum position -12 and maximum position -12
Frog at -4 after 800 hops with minimum position -12 and maximum position -12
Frog at -6 after 850 hops with minimum position -12 and maximum position -12
Frog at -4 after 900 hops with minimum position -12 and maximum position -12
Frog at -2 after 950 hops with minimum position -12 and maximum position -12
Frog at -4 after 1000 hops with minimum position -12 and maximum position -12
Frog at -6 after 1050 hops with minimum position -12 and maximum position -12
Frog at -12 after 1100 hops with minimum position -12 and maximum position -12
Frog at -22 after 1150 hops with minimum position -23 and maximum position -23
Frog at -20 after 1200 hops with minimum position -26 and maximum position -26
Frog at -20 after 1250 hops with minimum position -26 and maximum position -26
Frog at -30 after 1300 hops with minimum position -32 and maximum position -32
Frog at -38 after 1350 hops with minimum position -41 and maximum position -41
Frog at -36 after 1400 hops with minimum position -41 and maximum position -41
Frog at -36 after 1450 hops with minimum position -44 and maximum position -44
Frog at -26 after 1500 hops with minimum position -44 and maximum position -44
Frog at -44 after 1550 hops with minimum position -44 and maximum position -44
Frog at -40 after 1600 hops with minimum position -45 and maximum position -45
Frog at -38 after 1650 hops with minimum position -45 and maximum position -45
Frog at -36 after 1700 hops with minimum position -45 and maximum position -45
Frog at -20 after 1750 hops with minimum position -45 and maximum position -45
Frog at -28 after 1800 hops with minimum position -45 and maximum position -45
Frog at -28 after 1850 hops with minimum position -45 and maximum position -45
Frog at -34 after 1900 hops with minimum position -45 and maximum position -45
Frog at -36 after 1950 hops with minimum position -45 and maximum position -45
Frog at -36 after 2000 hops with minimum position -45 and maximum position -45
Frog at -30 after 2050 hops with minimum position -45 and maximum position -45
Frog at -32 after 2100 hops with minimum position -45 and maximum position -45
Frog at -34 after 2150 hops with minimum position -45 and maximum position -45
Frog at -40 after 2200 hops with minimum position -45 and maximum position -45
Frog at -34 after 2250 hops with minimum position -45 and maximum position -45
Frog at -24 after 2300 hops with minimum position -45 and maximum position -45
Frog at -24 after 2350 hops with minimum position -45 and maximum position -45
Frog at -24 after 2400 hops with minimum position -45 and maximum position -45
Frog at -22 after 2450 hops with minimum position -45 and maximum position -45
Frog at -14 after 2500 hops with minimum position -45 and maximum position -45
Frog at -14 after 2550 hops with minimum position -45 and maximum position -45
- 02-13-2009, 08:38 PM #14
Show me exactly what you want it to print and what you got before please. And your CODE tagged code so far.
Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-13-2009, 09:19 PM #15
Member
- Join Date
- Feb 2009
- Posts
- 27
- Rep Power
- 0
For example:
Prints outJava Code:Frog thisFrog = new Frog(); thisFrog.hop(1000); System.out.println("This Frog: " + thisFrog);
It does not print out 1000 hops at a timeThis Frog: Frog at 26 after 1000 hops with minimum position -9 and maximum position -9
- 02-14-2009, 02:01 AM #16
Tell me if you want a cool Java logo avatar like mine and I'll make you one.
Similar Threads
-
JList issues
By aneesahamedaa in forum AWT / SwingReplies: 7Last Post: 12-15-2009, 04:16 PM -
Array issues
By joka22 in forum New To JavaReplies: 6Last Post: 10-07-2008, 11:05 AM -
ScrollPane Issues..
By hanifa in forum AWT / SwingReplies: 4Last Post: 09-11-2008, 08:18 AM -
iterator issues
By orchid in forum New To JavaReplies: 2Last Post: 08-12-2008, 01:43 PM -
Issues with Jva I.O
By Annatar01 in forum New To JavaReplies: 0Last Post: 02-08-2008, 01:16 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks