Is the function wrong? [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
while(n>0) {
int count=0;
n= n/10;
count++;
}
System.out.println(count);

In your code, you have declared the variable count in the while loop block and so it is a local variable and the scope of the variable is limited till while loop block.
If you want the program to execute successfully, you should declare the variable inside the main method block but outside the while loop block.
Like this
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int count = 0;
while(n > 0){
n = n/10;
count++;
}
System.out.println("Count = "+count);
}

Scope of a variable is the part of the program where the variable is accessible.
Refer this article for more details Scope in Java
Also to correct the code,
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
int count = 0;
while(n>0) {
count=0;
n= n/10;
count++;
}
System.out.println(count);

Related

While statement isn't excuting inside if statement? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am trying to make it print out a range of specified numbers. So if they select option 1 and put in 1 and 15, I want it to print out 1 to 15. Once it gets to the while statement though it just prints nothing.
import java.util.Scanner;
public class Lab4 {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.print("Please choose your choice from the following menu");
System.out.print("\n1) Print through all integer numbers between two given integers");
System.out.print("\n2) Display a right triangular pattern of stars");
System.out.println("\n3) Quit");
int userInput = in.nextInt();
if (userInput == 1) {
System.out.print("Enter the start number: ");
int firstInteger = in.nextInt();
System.out.print("Enter the second number: ");
int secondInteger = in.nextInt();
while (firstInteger < secondInteger);
System.out.print(firstInteger);
firstInteger++;
} else if (userInput == 2) {
System.out.print("Enter the height: ");
int triangleHeight = in.nextInt();
} else if (userInput == 3);{
System.exit(userInput);
}
in.close();
}
}
You should change :
while (firstInteger < secondInteger);
System.out.print(firstInteger);
firstInteger++;
to
while (firstInteger < secondInteger) {
System.out.print(firstInteger);
firstInteger++;
}
while (firstInteger < secondInteger);
This will be treated as two executable lines of instructions,
which is similar like
while (firstInteger < secondInteger)
;
Try to remove the ; after while statement

Can do-while loops only run with boolean variables? Java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am having some trouble with this small segment. Can do-while loops only run using boolean variables? I am trying to ask the "user" to enter "0" or "1" so that the program will either loop or end.
Error message:
Chapter4Practice.java:23: error: incompatible types: int cannot be
converted to boolean
} while (choice = 1);
^ 1 error
My code:
import java.util.Scanner;
public class Chapter4Practice
{
public static void main(String[] args)
{
int choice, num1, num2;
String input;
Scanner sc = new Scanner(System.in);
do {
System.out.print("Enter a number: ");
num1 = sc.nextInt();
System.out.print("Enter another number: ");
num2 = sc.nextInt();
System.out.println("The sum is " + (num1 + num2));
System.out.println("Do you want to do this again?");
System.out.println("(1 = yes, 0 = no)");
sc.nextLine();
choice = sc.nextInt();
} while (choice = 1);
} //End Main
} //End Class
Short answer: Yes, you can have simple or complex statements inside your do-while but at the end it will have to evaluate to either true or false
Also your statement should be == ( single = means assign, where == is evaluate)

Getting an integer to go to a different line [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
This is my code for reversing the digits of an integer,
but the result gives each digit reversed but in a new line,
like if you give 341 to it , it gives you:
1
4
3
But I want to make it all come to the same line , is there a way to do it without changing the main code?
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter your number to reverse it ...");
int a=scan.nextInt();
while(a>0) {
i=a%10;
a/=10;
l++;
System.out.println(i);
}
System.out.println("Number of digits: "+l);
}
You could use print instead of println:
System.out.print(i);
Answer :
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter your number to reverse it ...");
int a=scan.nextInt();
while(a>0) {
i=a%10;
a/=10;
l++;
System.out.print(i);
}
System.out.println("Number of digits: "+l);
}
I think your code has few errors. It should be
int i = 0;
while(a>0) {
i=a%10;
a/=10;
System.out.print(i+" ");
i++;
}
In your code you are just picking digits and printing it in reversed order instead of really reversing it.
public static void main(String[] args)
{
int i,l=0,num=0;
Scanner scan=new Scanner(System.in);
System.out.println("Enter your number to reverse it ...");
int a=scan.nextInt();
while(a>0)
{
i=a%10;
a/=10;
num=(num*10)+i;
l++;
}
System.out.println("Number of digits: "+l);
System.out.println("Reversed Number: "+num);
}
Try this code instead.....

Not able to take user input when using while loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have this code bellow which is supposed to take a user input and store it in an array, and I was just wondering why it is not allowing me to input any numbers.
Should the input part be inside the if statement? Also what is the best way to make it work properly?
import java.util.*;
public class fun_with_loops {
static Scanner scan = new Scanner(System.in);
public static void main (String[] args) throws java.io.IOException
{
int[] numbers = new int[10];
int numberSize = 0;
System.out.print("Enter a few numbers please\n");
while (numbers.length < 10)
{
int input = scan.nextInt();
if (input != 0)
{
numbers[numberSize] = input;
numberSize++;
}
else
{
break;
}
}
}
}
Problem
The following expression on loop's control is always evaluated as false:
while (numbers.length < 10)
since array's length is in fact equals 10 as when declared.
Solution
In order to program work as expected you have to use numberSize variable as control:
while (numberSize < 10)
since it grows based on number of inputs.
As Am_I_Helpful stated, you are using a while loop on a value that will not change. I am not sure if the use while is needed in this case. Since you want to loop a specific amount of times you might want to use a for loop. If the amount of times will depend on the size of your array, you could still replace the "10" by your array length (numbers.length).
for (int i; i< 10; i++)
{
int input = scan.nextInt();
if (input != 0)
{
numbers[numberSize] = input;
numberSize++;
}
else
{
break;
}
}
Hoping it helped!
Dan
a short and sweet summary of when to use each loop:
http://mathbits.com/MathBits/CompSci/looping/whichloop.htm
but of course it always depends of your goal while coding so it's sometimes hard to say which is best if you are not the one coding.
Because the array is initialized to size 10, the length will always be 10. A counter variable needs to be used. Here is the code:
static Scanner scan = new Scanner(System.in);
public static void main (String[] args) throws java.io.IOException
{
int[] numbers = new int[10];
System.out.print("Enter a few numbers please\n");
int count = 0;
while (count < 10)
{
int input = scan.nextInt();
if (input != 0)
{
numbers[count] = input;
count++;
}
else
{
break;
}
}
The length property returns the size of the array, not the number of elements that are present in the array. You need to keep track of number of elements in the array on your own.
for(int eleCount = 0; eleCount < 10; eleCount++)
{
int input = scan.nextInt();
if (input != 0)
{
numbers[eleCount] = input;
}
else
{
break;
}
}

Accessing a variable from inside a do-while loop [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
How can I access a variable from inside a do-while loop in Java?
The code below writes out a value until the value entered is not is between 0 and 10.
Here is my code :
import java.util.Scanner;
public class DoWhileRange {
public static void main(String[] args) {
do{
System.out.println("Enter a number between 0 an 10");
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int total +=0;
}while (a>0 && a<10);
System.out.println("Loop Terminated");
System.out.println("The total is : "+total);
}
}
The loop continues to ask for input so long as the input is between 0 and 10. Once some other number is entered the loop terminates and displays the total of all inputted numbers.
try like (declare the variable a outside the loop):
int a = -1;
do{
System.out.println("Enter a number between 0 an 10");
Scanner in = new Scanner(System.in);
a = in.nextInt();
}while (a>0 && a<10);
To access a variable beyond the loop, you need to declare/initialize it outside of the loop and then change it inside the loop. If the variable in question wasn't an int, I would suggest that you initialize it to null. However, since you can't initialize an int variable to null, you'll have to initialize it to some random value:
import java.util.Scanner;
public class DoWhileRange {
public static void main(String[] args) {
int a = 0; //create it here
do {
System.out.println("Enter a number between 0 an 10");
Scanner in = new Scanner(System.in);
a = in.nextInt();
} while (a>0 && a<10);
System.out.println("Loop Terminated");
// do something with a
}
}
NOTE: If you simply declare the variable before the loop without initializing it (as per #Evginy's answer), you'll be able to access it outside the loop but your compiler will complain that it might not have been initialized.
try this
Scanner in = new Scanner(System.in);
int a;
do {
System.out.println("Enter a number between 0 an 10");
a = in.nextInt();
} while (a > 0 && a < 10);
System.out.println("Loop Terminated");

Categories