Java code not printing to console in Eclipse - java

I have this java code in eclipse. When I run it, I assume I should get something back in the console at the bottom of eclipse. This is not the case. The console at the bottom of eclipse is blank.
package com.veggiedogtreats.javacode;
public class doobeedoobeedo {
/**
* #param args
*/
public static void main(String[] args) {
int x = 1;
while (x < 0) {
System.out.println("Doo");
System.out.println("Bee");
x = x + 1;
}
if (x == 2 ) {
System.out.print("Do");
}
}
}

you have the while loop set to x < 0, it should be x > 0. The way you have it, it will never enter the while loop

Your while condition is wrong. it should read while ( x > 0 ) instead of while ( x < 0 )

Your program only prints to the console when x is less than zero and when x is 2.
x always has a value of 1.
int x = 1; // x is 1
while (x < 0) { // 1 is not less than zero, doesn't enter the loop
System.out.println("Doo");
System.out.println("Bee");
x = x + 1;
}
if (x == 2 ) { // 1 is not two, doesn't enter the if
System.out.print("Do");
}
Maybe you wanted something like this:
while (x < 0) { .... }

your conditions are not satisfied and hence the sysout statements are not executed. Either change the initial value of x or the conditions so that the sysout statements are executed atleast once.

Neither of the conditional statements are true. 1 is not less than zero or equal to 2.

Related

I need explanation why the output is a-b c-d [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
public class Main {
public static void main(String[] args) {
int x = 3;
while(x > 0) {
if(x > 2) {
System.out.print("a");
}
x = x - 1;
System.out.print("-");
if(x == 2) {
System.out.print("b c");
}
if(x == 1) {
System.out.print("d");
x = x - 1;
}
}
}
}
Output: a-b c-d
by the time "b c" is printed the value of x is supposed to be 2, why did "d" get printed as well?
look at the snippet closely
int x = 3;
while(x > 0) {
if(x > 2) {
System.out.print("a");
}
x = x - 1;
System.out.print("-");
if(x == 2) {
System.out.print("b c");
}
if(x == 1) {
System.out.print("d");
x = x - 1;
}
}
when loop runs for the first time the value of x is 3 and the following operations happens
while(x > 0) {
if(x > 2) {
System.out.print("a"); // as the value is 3 so it ll print
}
x = x - 1; // here value is 2 now
System.out.print("-"); // it ll be printed all the time the loop runs
if(x == 2) {
System.out.print("b c"); // again this ll be printed because the value is 2
}
if(x == 1) {
System.out.print("d");
x = x - 1;
}
}
now the value of x is 2 and the following operations happen
while(x > 0) { // value of x is 2
if(x > 2) {
System.out.print("a"); // it is skipped as the value of x is 2
}
x = x - 1; // now the value of x is 1
System.out.print("-"); // it gets printed again
if(x == 2) {
System.out.print("b c"); // it gets skipped as the value is 1
}
if(x == 1) {
System.out.print("d"); // it gets printed
x = x - 1; // loop gets over here as the value becomes 0
}
}
i have simplified it so that you understand well :)
Because the loop runs until X == 0, which means it will run once and print a, bc, Then it checks if X==0, which it isnt, x = x-1, now x == 1 and prints d. The loop runs once more, makes x == 0, and then it stops
First round - X == 3, so it prints "a",
X = X - 1, Now x == 2
X== 2, so it prints "bc"
jumps over the last if statement since x > 1.
Second round
X is not 3 so it doesnt print.
X = X -1, now x == 1,
X is not 2, so now print,
X== 1 so it prints d.
third round since X > 0
only thing happening is X = X -1;
now X == 0, and the while loop will end
The best is to go through the code like a debugger. Line after line.
You see you have your program wrapped in a while loop, while(x>0) means the whole block of the code will be repeated as long as x is bigger than 0.
While 1st iteration
You are right in first iteration after "b c" is printed out the value of x is 2. and program will skip printing "d". At this point the while loop 1st iteration is over.
While 2nd iteration
Now the condition of the while is checked x is 2, that means (x > 0) and program will enter the second iteration of the while loop. "a" will not be printed. x is getting decreased to 1. "b-c" will not be printed. As x is 1 "d" will be printed to console.
While 3rd iteration
Condition checks value of x. x=1, its bigger then zero so again while loop executes. This time x is decreased to 0. Nothing gets printed as none of the if statements is satisfied.
End
As x is 0, its not bigger than 0 anymore, condition of the while loop is not satisfied. While loop will not be executed and program will end.
Check and play with the while loop
Oracle docs
W3schools

Loop and if statement do not give desired output [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
public class Shuffle1 {
public static void main(String[] args) {
int x = 3;
if(x > 2) {
System.out.print("a");
}
while(x > 0) {
x = x - 1;
System.out.print("-");
}
if(x == 2) {
System.out.print("b c");
}
if(x == 1) {
System.out.print("d");
x = x - 1;
}
}
}
I'm learning Java from a book called "Head First Java", and I was using TextEdit as recommended in the book. I am supposed to be able to compile the code to get an answer of a-b c-d, but instead each time I compile it, I get a result of a---. I have checked it thoroughly myself and would really appreciate if anyone could help me.
Here is the original question from the book.
So if x is 3, I'll take you through what happens:
Print's out "a" because 3 > 2
Decrements x to 0, printing "--" on the way because it took 2 decrements to satisfy break condition, x > 0
That means it would correctly print a--. To achieve a-b c-d, you must the if statements inside the loop, like this:
while(x > 0) {
x = x - 1;
System.out.print("-");
if(x == 2) {
System.out.print("b c");
}
if(x == 1) {
System.out.print("d");
x = x - 1;
}
}
Now the execution cycle is:
x > 2, so print "a"
Goes into the loop
x becomes 2
Prints "-"
x is 2, so print "b c"
Continue iteration
Next iteration, x becomes 1
Print "-"
x is 1 so print "d"
x is now 0
Terminate loop
This gives the following desired result: a-b c-d.
This will print as per your expectation.
public class Shuffle1 {
public static void main(String[] args) {
int x = 3;
if(x > 2) { //First time x=3, which is true
System.out.print("a"); // print a
}
while(x > 0) { // x=3, which is true
x = x - 1; //first loop, x=2, then second loop x=1
System.out.print("-"); //prints "-"
if(x == 2) { // x=2, which is true
System.out.print("b c"); //prints "b c"
}
if(x == 1) { // as x=2, so it won't get display in first loop, but when it loop for second time, x become 1, which is true.
System.out.print("d");
x = x - 1;
}
}
}
}

Printing Small Numbers Between 1 and 100 in Java

I'm new to Java and this is a simple question but i'm having trouble printing small numbers between 1 and 100. Small numbers are those less than 20 and I want my program to print out "small x" for each small number.
When I run this I'm not getting what I'm suppose to I just get "small x" printed 100 times.
Here's my code:
class ExerciseA {
public static void main(String[] args) {
int x = 1;
while ( x < 100 ) {
x = x + 1;
if( x > 20) {
System.out.println("small x");
}
}
}
}
> means greater, but you want lesser. So use <. Another thing in your code, 1 would not be printed as it's being incremented before printing. This should be:
int x = 0;
while ( x < 100 ) {
x++; // shorter than x = x + 1;
if (x < 20) {
System.out.println("small x");
//System.out.println("small " + x); //if want to print like small 1, small 2 etc.
}
}
Your System.out.println is printing small x since it is using the whole string instead of using the value of variable x.
Also, if you want to print small x for numbers less than 20, you should use x<20.
int x = 0;
while ( x < 100 ) {
x = x + 1;
if( x < 20) {
System.out.println("small " + x);
}
}
You can write down such kind of loops in the more matching for-loop style:
for (int i = 1; i <= 100; i++) {
if (isSmall(i)) {
System.out.println("small i");
}
using a bit of "re-factoring" by putting the check "is this a small number" into its own method:
private boolean isSmall(int i) {
return i < 20;
}
Using such small methods is something that you should get used to as soon as possible - as it helps to "isolate" certain functionality. Another change I made: in computer science, variables for whole numbers are typically called "i,j,k" ... and so on; whereas x, y, ... would be for floating point numbers!
And of course: you got the comparison wrong - if you want numbers smaller than 20, well, than you have to say so ( i < 20 ).

Why does this loop infinitely execute if the condition for the for loop is above 2?

public class Proof {
public static void main(String[] args) {
for (int x = 1; x < 3; x++) {
do {
if (x == 0) {
x = x * 3 + 1;
} else if (x % 2 != 0) {
x = x * 3 + 1;
} else if (x % 2 == 0) {
x /= 2;
}
System.out.println(x);
} while (x != 1);
System.out.println("DONE!!!!!!");
System.out.println();
}
}
}
To test this issue just change x < 3 to 2 and it works fine. Then change it back to 3 and it doesn't work. I'm just a new programmer but I'm confused about this.
So, when x == 1, you go through the do while loop and eventually the value goes back to 1, you exit and then you iterate the for loop to 2... That would exit the for loop when you change is to x < 2.
When x < 3, what happens? X goes through the do while loop just like before, but you don't exit the for loop at 2, you actually go back into the do while loop. But when do you exit the do while loop??? Only when x == 1... so then what happens to your for loop? It iterates x to..... 2 again. And you are back in your do while loop. Forever!
As Casey said, the debugger will show you this and is incredibly useful. Good luck going forward!
Once your for loop reaches it's second iteration (x = 2), your do while loop will set it back to 1 (x /= 2), and your for loop increments it back to 2 (x++), causing the process to repeat infinitely.
Here's a visual:
x is initially equal to 1.
Go through the first else if condition, x is increased to 4.
x != 1 at this point; iterate in the while loop again, and go through the second else if condition. x is divided by 2 and is therefore equal to 2.
When equal to 2, x%2 ==0 is true, x is divided by 2 again and goes down to 1.
The while loop is exited as x is equal to 1.
Back to the for loop, x is increased to 2, and you go back through the second else if condition.
You're back to the fourth step, thus reaching a cycle in the calculation and having the infinite loop execution you described.

Multiples of 7 between 98 and 266

The statement says:
Write a list of multiples of 7 between 98 and 266, both
including
I put this code:
import java.util.*;
public class Multiples7 {
public static void main (String[] args) {
Scanner entrada;
int x;
entrada = new Scanner(System.in);
while (x >= 98 && x <= 266) {
if (x % 7 == 0){
System.out.println(x);
}
}
}
}
and I get this error that I don't understand:
variable x might not have been initialized
Why x not start?
To solve the question asked: you simply need to initialize x, which is currently uninitialized. To initialize a variable, you have to assign it a value. For example x = 0;.
However, that still is not going to cause your program to print the correct result.
One way to accomplish what you actually want to do is iterate the numbers between 98 and 266 print them when they are divisible by 7.
for(int y = 98; y <= 266; ++y) if (y % 7 == 0) System.out.println(y);
alternately, you can start at 98 (14 * 7) and then increment it by 7, printing as you go.
int y = 98;
while(y <= 266) {
System.out.println(y);
y+=7
}
You need to read the value of x or initialize it yourself. This error is shown because there is a chance that the program might get over without x being initialized.
Just initialize it :
int x = 0;
or read from scanner
x = entrada.nextInt();
Alternatively, you could use a for loop, which includes initialization.
for (int x = 98; x <= 266; x++) {
if (x % 7 == 0) {
System.out.println(x);
}
}
You have only declared x but did not initialize it. Insted of int x do int x = 0;. Replace 0 with the desired value.
You need to give X a starting value or it might as well not exist.
For example if X should start at 0 then use:
int x = 0;
you need to initialize x so it has a starting value and is not empty when your programm starts(int x = 98;). Also you should increment x inside your while loop (x++; or you will have an infinity loop always printing the same line.
int x = 98;
entrada = new Scanner (System.in);
while ( x >= 98 && x <= 266) {
if (x % 7 == 0){
System.out.println(x);
}
x++;
}
It could be a single for loop. Initialize x at 98, increment by 7 and stop when x is greater then 266. Something like,
for (int x = 98; x <= 266; x += 7)
System.out.printf("%d = 7 * %d%n", x, x / 7);

Categories