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 8 years ago.
Improve this question
Hello so I am attempting to write a code that test the operators and need a little help declaring variables.
So in my code under my if statement I declare a certain answer to be true or false depending on what the entered number would be and I keep getting stuck on the "cannot find symbol" error. Here is my code
import java.util.Scanner;
public class TestOperators
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
//prompt the user to enter an integer
System.out.print("Enter an integer: ");
int num = input.nextInt();
String t = new String("True");
String f = new String("False");
//calculate
if ((num % 5 == 0) && (num % 6 == 0))
answer = t;
else if ((num % 5 == 0) && (num % 6 != 0) || (num % 5 != 0) && (num % 6 == 0))
answer = t;
else if ((num % 5 == 0) || (num % 6 == 0))
answer = t;
else
answer = f;
//print results
System.out.println("Is " + num + " divisible by 5 and 6? " + answer);
System.out.println("Is " + num + " divisible by 5 or 6?" + answer);
System.out.print("Is " + num + " divisible by 5 or 6, but not both?" + answer);
}
}
The program tells me that I cannot declare the variable "answer" in that spot. I am trying to tie the string "f" and "t" variables to the answer variable and dont know how. Please help!
You're not declaring the variable at all. You're just trying to assign to it. You can just add
String answer;
to the code before you start trying to assign to it.
Additionally, you almost never want to call new String(String), and you can simplify your code significantly. You'd be better off determining the result (e.g. just with ||) and then converting the result into a string:
boolean result = ((num % 5 == 0) && (num % 6 == 0)) ||
((num % 5 == 0) && (num % 6 != 0) || (num % 5 != 0) && (num % 6 == 0)) ||
((num % 5 == 0) || (num % 6 == 0));
String answer = result ? "True" : "False";
Looking at that, the result you want actually seems to be as simple as:
String answer = (num % 5 == 0 || num % 6 == 0) ? "True" : "False";
The only way you can get "False" as an answer in both your original code and my final code is if the number isn't divisible by either 5 or 6.
Your code just expresses that in a really complicated way...
You never declared answer. Try adding this line at the beginning of the method:
String answer;
Related
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter value: ");
int value = input.nextInt();
if (value == 1 || value == 3 || value == 5 ||value == 7 || value == 9) {
if (value % 3 == 0)
System.out.println(value + " is an odd number less than 10"
+ " and divisible by 3");
}
else if (value == 2 || value == 4 || value == 6 ||value == 8) {
if (value % 3 == 0)
System.out.println(value + " is an even number less than 10"
+ " and divisible by 3");
}
else
System.out.println("Invalid number");
input.close();
}
if-else statement only recognizes integers that are divisible by 3 which are 3, 6 or 9. When I type 1 or 5, for example, the programme does not display any of the input and just stop running.
If you type "1" then if(value == 1) is true so then the program checks if(value %3 == 0). Since value%3 is 1 when value is 1 it doesn't print anything and exits. So you see nothing.
Normally, in any IDE there are some debugging facilities so by using them you will be able to see how the program works step by step.
I don't know why you used this particular logic in your if statements, but we can refactor assuming what you print is actually what you want:
is an odd/even number less than 10 and divisble by 3
if (value < 10 && value % 2 == 1 && value % 3 == 0) {
System.out.println(value + " is an odd number less than 10"
+ " and divisible by 3");
}
else if (value < 10 && value % 2 == 0 && value % 3 == 0) {
System.out.println(value + " is an even number less than 10"
+ " and divisible by 3");
}
else {
System.out.println("value does not meet our conditions.");
}
The immediate reason why 1 and 5 apparently cause the code to do nothing is that they enter the first if statement but then fail the next condition as they are not divisible by three, and your code does not output in this case.
This happens because of the given logic. When you type 1 or 5 as input first outer if becomes true. But if(value % 3 == 0) isn't true, that's why program ended up without giving any output.
If I understand your expectation of this program correctly please try the following code
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter value: ");
int value = input.nextInt();
if (value < 10 && value % 2 == 0 && value % 3 == 0) {
System.out.println(value + " is an even number less than 10" + " and divisible by 3");
}
else if (value < 10 && value % 2 != 0 && value % 3 == 0) {
System.out.println(value + " is an odd number less than 10" + " and divisible by 3");
}
else
System.out.println("Invalid number");
input.close();
}
when you enter 1 or 5 for example, below condition becomes true
if (value == 1 || value == 3 || value == 5 ||value == 7 || value == 9)
however, value %3 is not equal to 0 hence nothing is printed and program control directly goes to
input.close();
First of all, you should not use if-else statements that way. Now, the problem is that when you enter 1 or 5, they pass the first if statement, but neither (1 % 3 ==0) or (5 % 3 == 0) will be true, and that is why your code does nothing in those cases. Try this instead:
if(value % 3 == 0 && value <10) {
if(value % 2 != 0){
System.out.println(value + " is an odd number less than 10"
+ " and divisible by 3");
}else{
System.out.println(value + " is an even number less than 10"
+ " and divisible by 3");
}
}else{
System.out.println("Invalid number");
}
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 6 years ago.
Improve this question
I am brushing up on my Java and cannot get this program to work correctly. It is a while counter loop counting up to 100. If the counter is divisible by 3 it will output "On", if the counter is divisible by 7 it will output "Base", if the counter is divisible by 7 and 3 it will output "OnBase", otherwise it will output the number. Right now the program will not even compile and I have no idea what the issue is. Here is my program, any help is appreciated.
public class Counter {
public static void main(String[] args) {
int i = 1;
while(i <= 100)
{
if((i % 3) == 0){
system.out.println("On");
i++;
continue;
}
if((i % 7) == 0){
system.out.println("Base");
i++;
continue;
}
if((i % (3*7) == 0){
system.out.println("OnBase");
i++;
continue;
}
system.out.println(i);
i++;
}
}
}
There are two issues related to compilation:
Change the system to System.
The closing parenthesis is missing on line ----- if ((i % (3 * 7) == 0)) { // added the closing parenthesis
Apart from compilation, what you are looking for the logic is also not correct... I will provide some sample code below take a look there too...
// Sample Code to follow
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (((i % 3) == 0) && ((i % 7) == 0))
System.out.println("OnBase");
else if ((i % 3) == 0)
System.out.println("On");
else if ((i % 7) == 0)
System.out.println("Base");
else
System.out.println(i);
}
}
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
I can't seem to figure out what wrong with this method used to add two strings. It has a buffer when stores each added digit and finally displays. However when two huge numbers (greater than 10 digits) are added it just truncates and doesn't store the remainder of the string.
public static void add(String a,String b){
StringBuilder buf = new StringBuilder();
if((a.length() - 1==0)&&(a.charAt(0)=='0')){
System.out.println(b);
return;
}
if((b.length() - 1==0)&&(b.charAt(0)=='0')){
System.out.println(a);
return;
}
else{
for ( int i1 = a.length() - 1, i2 = b.length() - 1, carry = 0;
(i1 >= 0 && i2 >= 0) || carry != 0;i1--, i2-- ) {
int digit1 = i1 < 0 ? 0 :
Integer.parseInt(Character.toString(a.charAt(i1)));
int digit2 = i2 < 0 ? 0 :
Integer.parseInt(Character.toString(b.charAt(i2)));
int digit = digit1 + digit2 + carry;
if (digit > 9) {
carry = 1;
digit -= 10;
} else {
carry = 0;
}
buf.append(digit);
}
}
System.out.println(buf.reverse().toString());
}
Since need to stop when both i1 and i2 reach a negative value, the condition should be
i1 >= 0 || i2 >= 0 || carry != 0
rather than
(i1 >= 0 && i2 >= 0) || carry != 0
Demo on ideone.
Just use BigInteger, and your whole method is then three lines long.
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
This my code i don't know what to do next I'm totally beginner
int number = lb;
while(number <= ub){
if (number % 7 == 0 && number % 15 == 0 && number % 17 == 0 && number % 30 != 0 ){
sum+= number;
}
number++;
}
System.out.println("Answer: " + sum);
}
}
You have to do something like this
int sum=0;
for(int i=0;i<1000;i++){ // iterate from 0 to 1000
if ((i % 13 == 0 || i % 15 == 0 || i % 17 == 0) &&i % 30 != 0 ){
// take numbers divide by 13 or 15 or 17 which not divide by 30
sum=sum+i; // adding that number to current sum
}
}
System.out.println("Answer: " + sum); // final sum
Out put:
Answer: 76795
do it in one line using the latest release, Java 8:
package com.example;
import static java.util.stream.IntStream.rangeClosed;
import static java.lang.System.out;
public class Main {
public static void main(String[] args) {
out.println(rangeClosed(1, 1000)
.filter(n -> n % 13 == 0 || n % 15 == 0 || n % 17 == 0)
.filter(n -> n % 30 != 0)
.sum());
}
}
If I understand your question, it should be as simple as
int sum = 0;
// from 1 to 1000
for (int num = 1; num <= 1000; num++) {
if (num % 30 == 0) {
continue; // If it's divisible by 30, skip it.
}
if (num % 13 == 0 || num % 15 == 0 || num % 17 == 0) {
sum += num; // If it's divisible by 13 or 15 or 17 add it to sum.
}
}
System.out.printf("sum = %d%n", sum);
Output is
sum = 76795
int number = 0b1; //the proper way to start a binary number
int sum = 0;
while(number <= 1000){
if ((number % 13 == 0 || number % 15 == 0 || number % 17 == 0) && number % 30 != 0 ) {
sum+= number;
}
number++;
}
System.out.println("Answer: " + sum);
In the question you said numbers that are divisible by 13 or 15 or 17 and not by 30, so apply that same logic to the if statement. That would give you the right result.
Answer: 76795
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
In Java, how do I test if a character is an even or odd digit?
Here is what I have so far:
import java.util.Scanner;
public class OddOrEven{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int number, digit1, digit2, digit3;
System.out.print( "Enter three-digit number: " );// prompt
number = input.nextInt(); // read number
// determine the 3 digits
digit1 = number / 100;
digit2 = number % 100 / 10;
digit3 = number % 100 % 10;
if (digit1 % 2 == 0 && digit2 % 2 == 0 && digit3 % 2 == 0);
System.out.println( "This number contains all even digits.");
if (digit1 % 2 != 0 && digit2 % 2 == 0 && digit3 % 2 == 0);
System.out.println("This number contains both odd and even digits.");
if (digit1 % 2 != 0 && digit2 % 2 != 0 && digit3 % 2 == 0 );
System.out.println("This number contains both odd and even digits.");
if (digit1 % 2 == 0 && digit2 % 2 != 0 && digit3 % 2 == 0 );
System.out.println("This number contains both odd and even digits.");
if (digit1 % 2 == 0 && digit2 % 2 != 0 && digit3 % 2 != 0);
System.out.println("This number contains both odd and even digits.");
if (digit1 % 2 != 0 && digit2 % 2 != 0 && digit3 % 2 != 0);
System.out.println("This number contains all odd digits.");
}
}
If the number you are working with is an int (or a similar primitive type like long) then you can do something like this
int num = // something
while (num != 0) {
int digit = num % 10;
System.out.println(digit + " is " + (digit % 2 == 0 ? "even" : "odd"));
num /= 10;
}
This will iterate over the digits from right to left.
public static void main(String[] args) throws Exception {
int num = 28172;
String temp = Integer.toString(num);
int[] numArray = new int[temp.length()];
for (int i = 0; i < temp.length(); i++) {
numArray[i] = temp.charAt(i) - '0';
}
for (int i : numArray) {
System.out.println("Num " + i + " is " + ((i % 2 == 0) ? "even" : "odd"));
}
}
Gives me the output:
Num 2 is even
Num 8 is even
Num 1 is odd
Num 7 is odd
Num 2 is even
First, convert the int to a String. Then, since Strings are Character arrays, you can loop through each character in the array and turn it into an Integer by subtracting the ascii Character 0. Then, you can loop through each Integer in this array, and use the mod operator (%) which will give you the remainder of a division. num%2==0 will return true if the number is even, otherwise false.