java, else if "Error: illegal start of expression" - java

//the idea of the code if to see is point is in a rectangle. The input is a 6 digit number (abcdef). the top left corner of the rectangle has coordinates (a,b), the right lower (c,d) and the point (e,f)
enter codeimport java.util.Scanner;
public class Rectangle{
Scanner sc = new Scanner(System.in);
public static void run() {
int object;
System.out.println("input:");
object = sc.nextInt();
if(object/100000 >= (object/1000)%10 || (object/10000)%10 <= (object/100)%10){
System.out.print("inside");
}else if (object/100000 <= (object/10)%10 && (object/10)%10 <= (object/1000)%10 && (object/100)%10) <= object%10 && object%10 <= (object/10000)%10){
System.out.print("inside");
}else {
System.out.print("outside");
}
public static void main(String[] args) {
( new Rectangle()).run();
}
}

Your brackets are wrong, please use the formatting of your code tool. I post the fixed code below. Note that the condition must be wrapped between () brackets.
if (condition) { ... }
// In case there are complete calculations within condition
if ((condition) && (condition) && (condition)) { ... }
Moreover it surely says:
non-static method cannot be referenced from a static context
This error should be fixed by removing the static keyword since you use the instance of Scanner that's not static as well.
Scanner sc = new Scanner(System.in);
public void run() {
int object;
System.out.println("input:");
object = sc.nextInt();
if (object / 100000 >= (object / 1000) % 10 || (object / 10000) % 10 <= (object / 100) % 10) {
System.out.print("inside");
} else if (((object / 100000 <= (object / 10) % 10) &&
((object / 10) % 10 <= (object / 1000) % 10) &&
((object / 100) % 10) <= object % 10) &&
(object % 10 <= (object / 10000) % 10))
{
System.out.print("inside");
} else {
System.out.print("outside");
}
}
public static void main(String[] args) {
new Rectangle().run();
}

This is because you have extra ) in third condition in else if statement.
else if (object/100000 <= (object/10)%10 &&
(object/10)%10 <= (object/1000)%10 &&
(object/100)%10 remove it--->) <= object%10 &&
object%10 <= (object/10000)%10){
Also you cannot reference non static 'Scanner sc' in static method, make Scanner sc as static or run() method non static.

Related

Beginner(looping statements and conditional statements in java)

It shows compilation error:
Compile Message
Solution.java:19: error: unexpected type
if((N%2=0) && (N>=2 && N<=5))
^
required: variable
found: value
1 error
public class Solution {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int N = s.nextInt();
if (N % 2 != 0) {
System.out.println("Weird");
}
if ((N % 2 = 0) && (N >= 2 && N <= 5)) {
System.out.println("Not Weird");
}
}
}
N % 2 = 0 is an incorrect assignment because N % 2 isn't a variable. Even if it were a correct expression, it wouldn't return a boolean, so the line would never compile.
You need N % 2 == 0.

I'm facing an issue with control structures

My problem is with output of my code. When I enter 20, the output must be weird, but I am getting not weird. Same with the value 18.
import java.util.Scanner;
public class conditional {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
String ans = "";
if(n%2 == 1){
ans = "Weird";
} else {
if(n <= 2 && n >= 5){
ans="Not weird";
} else if(n <= 6 && n >= 20){
ans = "Weird";
} else{
ans = "Not Weird";
}
}
System.out.println(ans);
}
}
the output must be weird,but i am getting not weird
Because, if(n%2 == 1) return false and fall to else block where
if(n <= 2 && n >= 5) is `false`
and
else if(n <= 6 && n >= 20) is also `false`
So, again falls to else block. You probably change
if(n <= 2 && n >= 5)
to
if(n >= 2 && n <= 5)
and
else if(n <= 6 && n >= 20)
to
else if(n >= 6 && n <= 20)
Otherwise, they will never be true and always falls to else.
In your program last else is being executed. Change && (logical AND) to || (logical OR) which will check if number is less than something OR higher than something, instead of checking if something is less or equal 5 AND higher or equal to 20 in the same time as it doesn't have a possibility to evaluate in any case.
I have come up with two solutions and also i see a flaw:
1. if(n%2 == 1) this code can be altered to if(n%2 == 0)
2. The flaw is **(n <= 2 && n >= 5)** . No number can be <2 and >5 at the same time. Try changing that to (n <= 2 || n >= 5) and same goes for (n <= 6 && n >= 20)
import java.util.Scanner;
public class conditional {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
String ans = "";
if(n%2 == 1){
ans = "Weird";
} else {
if(n <= 6 || n >= 20){
ans="Not weird";
} else if(n <= 2 || n >= 5){
ans = "Weird";
} else{
ans = "Not Weird";
}
}
System.out.println(ans);
}
}

Understanding nested loop and classes

I started a project to understand deeply nested loops and classes. In my CYCLING method when I reach the if(y >= 0) loop it doesn't properly use my variables in classes. For example if MPH is 15 and the gear is 1 or 3 it won't ask me to change gear. Or if gear is 1 and speed is 11+ it wont ask me to change gear? What am I doing incorrectly?
public class Bike {
int speed;
int gear;
void changeGear(int newVal) {
gear = newVal;
}
void speedUp(int newVal) {
speed = newVal;
}
void breaks(int slow) {
speed = speed + slow;
}
void printState() {
System.out.println("Gear is: " + gear);
System.out.println("Speed is: " + speed + ("MPH"));
}
}
//________________________
public static boolean cycle = true;
public static Bike b1 = new Bike();
public static int x;
public static int y;
public static Scanner input = new Scanner(System.in);
public static int choice;
//______________________________
public static void cycling() {
while (cycle == true) {
System.out.println("What would you like to do now? Enter a number.");
System.out.println("1: Speed Change");
System.out.println("2: Change Gear");
choice = input.nextInt();
if (choice == 1) {
System.out.println("Choose a speed change");
y = input.nextInt();
if (y < 0) {
b1.breaks(y);
if (b1.speed < 0) {
b1.speed = Math.abs(y) + y;
System.out.println("You've stopped entirely");
}
}
if (y >= 0) {
b1.speed = y;
b1.printState();
if (b1.speed >= 0 && b1.speed <= 10) {
while (b1.gear != 1) {
System.out.println("You need to be in Gear 1 for " + "that! Please change gears.");
x = input.nextInt();
b1.changeGear(x);
}
if (b1.speed >= 11 && b1.speed <= 20) {
while (b1.gear != 2) {
System.out.println("You need to be in Gear 2 for" + "that! Please change gears.");
x = input.nextInt();
b1.changeGear(x);
}
if (b1.speed >= 21) {
while (b1.gear != 3) {
System.out.println("You need to be in Gear 3 for" + "that! Please change gears.");
x = input.nextInt();
b1.changeGear(x);
}
}
/*if(b1.speed >= 0 && b1.speed <=10){
b1.gear = 1;
}else if(b1.speed >= 11 && b1.speed <=20){
b1.gear = 2;
}else if(b1.speed >= 21){
b1.gear = 3;
}*/
b1.printState();
}
}
}
}
}
}
Think about your statements. You have something that basically looks like this:
if (b1.speed >= 0 && b1.speed <= 10) {
//some while loop here to do whatever
if (b1.speed >= 11 && b1.speed <= 20) {
//more code
}
}
In your code, this statement will never be true:
if (b1.speed >= 11 && b1.speed <= 20) {
The only way you get to that statement is if b1.speed>=0 && b1.speed<=10. Therefore, will b1.speed ever be between 11 and 20 when you get to that second (nested) if statement?

How to get 10 per line -leap years

Please help with formatting my output.
I have been asked to "Write a program that displays all the leap years, ten per line, in the twenty-first century (from 2001 to 2100), separated by exactly one space".
Although I get the right results, it's not in the required format.
Thanks in advance
public class Leapyear {
public static void main(String[] args) {
//declare variabls;
int year;
int count=1;
int yearsperline = 10;
//loop
for(year=2001;2001<=2100;year++){
if((year%4==0 && year%100!=0) || (year%400==0))
System.out.print(year+",");
if ( year ==2100)
break;
while (count%10==0)
System.out.println();
}
}
}
You can write something like this:
//declare variables;
final int YEARS_PER_LINE = 10;
final int START_YEAR = 2001;
//loop
for(int year = START_YEAR; year <= 2100; year++){
System.out.print(year);
if((year - START_YEAR) % YEARS_PER_LINE == (YEARS_PER_LINE - 1)) {
System.out.println();
} else {
System.out.print(",");
}
}
Try this one:
public class LeapYear
{
public static void main(String[] args)
{
// declare variables
final int startYear = 2001;
final int endYear = 2100;
final int yearsPerLine = 10;
// loop
for (int year = startYear, count = 0; year <= endYear; year++)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
if (count % yearsPerLine != 0)
System.out.print(" ");
else if (count > 0 && count % yearsPerLine == 0)
System.out.println();
System.out.print(year);
++count;
}
}
}
}
int startFromYear = 2001;
int stopAtYear = 2100;
int count = 0;
for(int i = startFromYear; i <= stopAtYear; i++){
if((i % 4 == 0 && i % 100 != 0)||(i % 400 == 0)){
System.out.print(i + " ");
count++;
if(count % 10 ==0)
System.out.println();
}
}
100% correct! and easy to understand.
public class LeapYears
{
public static void main(String[] args)
{
int count = 0;
for(int i = 2001; i <= 2100; i++)
{
if ((i % 4 == 0 && i % 100 != 0)||(i % 400 == 0))
{
count++;
//if count is 10 then start a new line.
if(count % 10 == 0)
{
System.out.println(i);
}
//if count is smaller 10 then in the same line
else
{
System.out.print(i + " ");
}
}
}
}
}

Output is not getting displayed

I am writing a simple java program to find the smallest number which is divisible by all the numbers from 1 to 20.
I have written the following code:
package smallmultiple;
public class SmallMultiple {
public static void main(String[] args) {
int sml = 0;
outerloop:
for (int i = 40; i < 100000; i++) {
int j=1;
do {
if(i%j==0)
j++;
} while(j<21);
if(j==20) {
sml=i;
break outerloop;
}
}
System.out.println(sml);
}
}
It is not giving any error but it is not giving any output.
You can try this which is a bit faster than yours solution:-
for(int i = 190; ; i += 190) {
if(i % 3 == 0
&& i % 4 == 0
&& i % 6 == 0
&& i % 7 == 0
&& i % 8 == 0
&& i % 9 == 0
&& i % 11 == 0
&& i % 12 == 0
&& i % 13 == 0
&& i % 14 == 0
&& i % 15 == 0
&& i % 16 == 0
&& i % 17 == 0
&& i % 18 == 0
&& i % 20 == 0) {
System.out.println(i);
break;
}
}
You can also check out this article.
You are incrementing j only if i is perfectly divisible by j. Shouldn't you break or exit the do.. while loop of atleast one number is not divisible? Not doing so is causing infinite loop I believe. It should be something like
if(i%j==0) {
j++;
}
else {
break;
}
its simple. Let me explain your loop. First, i = 40 and j = 1, its ok. Then j++.Next i = 40 and j = 2, its still correctly. Then j++ again. Now i = 40, j = 3 and i%j !=0 => j cannot ++ and j still equal 3. And you see, j = 3 is still satisfy your loop ( j < 21) then it loop and loop forever. This is the reason why you cant get any output. You can use your IDE debugging to find this mistake. Sorry my English not good.
In java, to respect the object oriented best practise, it's not advised to user labels, try this :
public class NumberTool {
public static void main(String[] args) {
System.out.println("Smallest number is : " + getSmallestNumberDividedByOneToTwnety());
}
public static int getSmallestNumberDividedByOneToTwnety() {
for ( int i = 40; i <= 2147483647; i++) {
if (isNumberDivdedByOneToTwenty(i)) {
return i;
}
}
return 0;
}
public static boolean isNumberDivdedByOneToTwenty(int numberToTest) {
for (int i = 1; i <= 20; i++) {
if (numberToTest % i != 0) {
return false;
}
}
return true;
}
}
Output is:
Smallest number is : 232792560

Categories