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 8 years ago.
Improve this question
Good day sir. My teacher told us to take all the even numbers from the input file. But the even number keeps telling me that it's 23 evens and it should be 8 and 66.67%. My input from file is:
5 7 2 8 9 10 12 98 7 14 20 22 (with white spaces)
and my code is:
import java.io.*;
import java.util.*;
public class number2 {
public static void main (String[] args) throws FileNotFoundException{
Scanner input = new Scanner(new File("number.txt"));
int sum = 0;
int count = 0;
int evenCount = 0;
float percent = 0;
while(input.hasNextInt()) {
int num = input.nextInt();
sum+=num;
count++;
evenCount = evenNumber(num, count);
}
percent = (evenCount*100)/count;
System.out.println("\n" +count + " Numbers, " + "Sum = " +sum);
System.out.println( evenCount + " evens " +"(" + percent +"%)");
}
public static int evenNumber(int counter, int number){
if(number%2==0)
counter++;
return counter;
}
}
I think you're calling the evenNumbermethod with the switched parameters...
while(input.hasNextInt()){
int num = input.nextInt();
sum+=num;
count++;
//first parameter is number read from file, second parameter is the current count
evenCount = evenNumber(num, count);}
//...
On your method evenNumbermethod signature you have
//first parameter is the counter, second parameter is the read number
public static int evenNumber(int counter, int number)
You should switch the parameters being called inside the evenNumbermethod in the while block:
evenCount = evenNumber(evenCount, num)
Also, you should pass evenCountto the evenNumbermethod instead of count, otherwise you will be increasing the count of all numbers instead the count for only even numbers.
Related
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
There is this program that asks me to write a java program that asks the user to type a positive number n and prints the sum of odd numbers using while loop: 1+3+5+7…+(2n-1).
Example : If the input is 4, then the program will print 16
so what i did is made this code :
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a positive number :");
int n = input.nextInt();
int i = 0;
int sum = 0;
while (i<=n)
{
sum += i ;
i++;
}
System.out.println("Sum = " + sum);
}
}
And the program is not working like how the question wants it to be like
You can use i += 2; which is same as i = i + 2;, instead of using i++;.
But this won't give you the output you expect. So, there has to be made several changes in your code to get the expected result.
First initialise the value of i to 1.
int i = 1;
Then, change the while loop statement to,
while (i <= (2 * n - 1)){
// Your Code
}
Finally, use i += 2; as your increment statement.
The full code is shown below.
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a positive number :");
int n = input.nextInt();
int i = 1;
int sum = 0;
while (i<=(2 * n - 1))
{
sum += i ;
i += 2;
}
System.out.println("Sum = " + sum);
}
}
You've initialized i wrong. It should start from 1 since you want to add only odd numbers. Also increase i by 2 units using +=2. Another problem is with the while loop condition. The last number in the sequence will be 2n-1 and not n. So the program will be:
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a positive number :");
int n = input.nextInt();
int i = 1;
int sum = 0;
while (i <= (2 * n - 1)) // Here is the new condition for last number...
{
sum += i ;
i+=2; // Here goes the 2 unit increment...
}
System.out.println("Sum = " + sum);
}
}
You can keep a variable like odd and increase its value by 2 in every iteration.
Also,
you should run the loop less than n times
because you start the loop from 0. Then I hope you will get your desired answer. Here is the sample code which may help you to understand.
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a positive number :");
int n = input.nextInt();
int i = 0;
int sum = 0;
int odd = 1;
while (i<n)
{
sum += odd ;
odd += 2;
i++;
}
System.out.println("Sum = " + sum);
}
}
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 2 years ago.
Improve this question
When I enter an even number, the code works and asks for an odd number, but when I input an odd number, it never closes and carries on the sum for every odd number.
import java.util.Scanner;
public class OddSums {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter an odd number");
int oddSumMax = in.nextInt();
int oddSum = 0;
do {
if (oddSumMax % 2 == 1) {
for(int i=1; i<=oddSumMax; i++) {
if (i % 2 == 1){
oddSum = oddSum + i;
}
}
System.out.println(oddSum);
} else if(oddSumMax % 2 == 0) {
System.out.println("This is even Please enter an odd number");
oddSumMax = in.nextInt();
}
} while (oddSumMax % 2 == 1 );
}
}
You should separate the loops. One loop to ensure the user inputs an odd number and a second one to do the calculations.
Scanner in = new Scanner(System.in);
int oddSumMax;
// Get user input
do {
// TODO: handle case where user does not enter a number
System.out.println("Enter an odd number");
oddSumMax = in.nextInt();
} while (oddSumMax % 2 == 0);
// Calculate
int oddSum = 0;
for (int i = 1; i <= oddSumMax; i += 2) {
oddSum += i;
}
System.out.println(oddSum);
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 6 years ago.
Improve this question
import java.io.*;
import java.util.*;
public class Binary {
public static void main(String args[]) {
int i = 0, j = 0, num;
Scanner in = new Scanner(System.in);
int arr[] = new int[100];
System.out.println("enter the number");
num = in.nextInt();
while (num != 1) {
j = num % 2;
num = num / 2;
arr[i] = j;
i++;
}
for (i = i; i <= 0; i--) {
System.out.print("The binary number: " + arr[i]);
}
}
}
I wrote this programme to convert decimal input to its corresponding binary value, the programme takes the input but it does not show the output i.e. the binary value. please help
As it was already pointed out, you need to change the condition of
while (num != 1) {
to
while (num > 0) {
since your version is prone to an infinite cycle due to the possibility of num being 2.
Change the for cycle, like this:
for (i = arr.length - 1; i >= 0; i--) {
System.out.print("The binary number: " + arr[i]);
}
but to be able to do this, you need to know how many elements will you need to use, so change the declaration of arr to this
int arr[] = new int[(int)Math.ceil(Math.log(num) / Math.log(2))];
But to be able to do this, you need to initialize num before you declare arr. Code is untested, let me know if there are any typos.
condition should be while(num!=0){ //do calculations}
and change the condition of for loop into for(i=i;i>=0;i--){}
You could use the Integer class and it's static method toBinaryString(int i). This method converts an int into its binary value and returns it as a String.
If I correctly understood what you are trying to achieve, you could just write:
Scanner in = new Scanner(System.in);
System.out.println("enter the number");
int num = in.nextInt();
String binary = Integer.toBinaryString(num);
System.out.print("The binary number: " + binary);
This question already has answers here:
What is x after "x = x++"?
(18 answers)
Closed 7 years ago.
I am a rookie in programming world and would appreciate if anyone can solve this problem and make me understand.the output is always "no data entered".
public class Trial {
public static void main(String[] args){
Scanner firstno = new Scanner(System.in);
int count = 0;
double sum = 0;
System.out.print("enter first number: ");
double firstnoin = firstno.nextDouble();
while (firstnoin != 0){
sum += firstnoin;
count = count++;
System.out.print("Enter next number, or 0 to end: ");
firstnoin = firstno.nextDouble();
}
if(count == 0){
System.out.println("No data is entered.");
}
else {
System.out.println("Average of the data is: "+(sum/count)+", number of terms are "+ count);
}
}
}
You have simply made a semantic error - this is when your program still compiles and works, but just doesn't work exactly how you wanted it to or doesn't do what you expected it to.
In your case, you have the following:
count = count++
If count = 0, this piece of count will simply keep count = 0. Hence why you are getting 'no data entered' every time.
Instead you need:
count++;
This will increment count by 1 each time.
Alternatively, you can use:
count = count + 1;
Again, this will take count's value and add 1 to it and then save this new value of count.
You can also change this to + 2, + 3 and so on and so forth as you wish and you can also do:
count += 1
which will also increment count by 1 and this can also be changed to 2, 3 and so on as you wish.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm getting 0 instead of 1 1 1 1 1.
class learn
package learn2;
import java.io.*;
public class learn {
public static void main(String[] args) throws IOException{
InputStreamReader ISR=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ISR);
int myArray[]=new int[5];
int sum= 0;
int pro = 0;
for(int u=0;u<5;u++)
{
if (myArray[u]>0)
{
sum= sum= sum = myArray[u];
pro = pro* myArray[u];
}
System.out.println("enter a number");
String x=br.readLine();
int x1=Integer.parseInt(x);
myArray[u]=x1;
}
for (int u1=0;u1<5;u1++)
{
System.out.println("You Enter" + myArray[u1] );
}
System.out.print("the sum of all positive numbers is: " + sum );
System.out.println("the product of all positive numbers is: " + pro);
}
}
Three problems:
Do sum = sum + myArray[u]; instead of sum= sum= sum = myArray[u];
Multiply something with 0 is always 0, so initialize pro with 1: int pro = 1;
Do the calculation after reading the first value in your for-loop because you are testing myArray[u]>0 and myArray[u] is always 0 if you test it before reading.
for(int u=0;u<5;u++)
{
System.out.println("enter a number");
String x=br.readLine();
int x1=Integer.parseInt(x);
myArray[u]=x1;
if (myArray[u]>0)
{
sum= sum + myArray[u];
pro = pro* myArray[u];
}
}
You're getting 0 because in Java the default values of int array is 0, for all elements. And since you're summing and multiplying the elements before you read the input, you're getting 0.
You should do:
for(int u=0;u<5;u++) {
System.out.println("enter a number");
String x=br.readLine();
int x1=Integer.parseInt(x);
myArray[u]=x1;
if (myArray[u]>0) {
sum+= myArray[u];
pro = pro* myArray[u];
}
}
More changes:
Initialize pro to 1, not to 0, as it's a multiplication
Chagne sum= sum= sum = myArray[u]; to sum += myArray[u];
Now you're fine.