#cant figure out whats wrong with my code

1 messages · Page 1 of 1 (latest)

urban cosmos
#
import java.util.Scanner;
import java.lang.Integer;
public class primeAdam 
{
   public static void main(String args[])
   {
      Scanner sc=new Scanner(System.in);
      int m,n,i,j,x=0,y,z,rev;
      System.out.println("enter m val");
      m=sc.nextInt();
      System.out.println("enter n val");
      n=sc.nextInt();
      while(m>n)
      {
           System.out.println("not valid.. try again");
           System.out.println("enter m val");
           m=sc.nextInt();
           System.out.println("enter n val");
           n=sc.nextInt();
      }
      sc.close();

      System.out.println("the prime-adam no.s within the range is ");
      
      if(m>10)
      m=10;

      for(i=m;i<n;i++)
      {
          for(j=1;j<=i;i++)
          {
            if(i%j==0)
            x++;
         }
         if(x==2)
         continue;
         y=Integer.reverse(i);
         z=i*i;
         rev=y*y;

         if(z==Integer.reverse(rev))
         System.out.print(i+" ");
      }



   }
    
}

the aim of the program is to check if a. the no. is prime and b. is the no. when squared and reversed is equal to the original no when squared

spring sageBOT
# urban cosmos ``` java import java.util.Scanner; import java.lang.Integer; public class primeA...

Detected code, here are some useful tools:

Formatted code
javaimport java.util.Scanner;
import java.lang.Integer;

public class primeAdam {
  public static void main(String args[] ) {
    Scanner sc = new Scanner(System.in);
    int m, n, i, j, x = 0, y, z, rev;
    System.out.println("enter m val");
    m = sc.nextInt();
    System.out.println("enter n val");
    n = sc.nextInt();
    while (m > n) {
      System.out.println("not valid.. try again");
      System.out.println("enter m val");
      m = sc.nextInt();
      System.out.println("enter n val");
      n = sc.nextInt();
    }
    sc.close();
    System.out.println("the prime-adam no.s within the range is ");
    if (m > 10) m = 10;
    for (i = m; i < n; i++) {
      for (j = 1; j <= i; i++) {
        if (i % j == 0) x++;
      }
      if (x == 2) continue ;
      y = Integer.reverse(i);
      z = i * i;
      rev = y * y;
      if (z == Integer.reverse(rev)) System.out.print(i + " ");
    }
  }
}
#

<@&987246399047479336> please have a look, thanks.

spring sageBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

Of course! Please provide the code you need help with, and let me know what specific issues or questions you have.

urban cosmos
#

i cant seem to find whats wrong with this code

import java.util.Scanner;
import java.lang.Integer;
public class primeAdam 
{
   public static void main(String args[])
   {
      Scanner sc=new Scanner(System.in);
      int m,n,i,j,x=0,y,z,rev;
      System.out.println("enter m val");
      m=sc.nextInt();
      System.out.println("enter n val");
      n=sc.nextInt();
      while(m>n)
      {
           System.out.println("not valid.. try again");
           System.out.println("enter m val");
           m=sc.nextInt();
           System.out.println("enter n val");
           n=sc.nextInt();
      }
      sc.close();

      System.out.println("the prime-adam no.s within the range is ");
      
      if(m>10)
      m=10;

      for(i=m;i<n;i++)
      {
          for(j=1;j<=i;i++)
          {
            if(i%j==0)
            x++;
         }
         if(x==2)
         continue;
         y=Integer.reverse(i);
         z=i*i;
         rev=y*y;

         if(z==Integer.reverse(rev))
         System.out.print(i+" ");
      }



   }
    
}
urban cosmos
# spring sage Of course! Please provide the code you need help with, and let me know what spec...

i cant seem to find whats wrong with this code

import java.util.Scanner;
import java.lang.Integer;
public class primeAdam 
{
   public static void main(String args[])
   {
      Scanner sc=new Scanner(System.in);
      int m,n,i,j,x=0,y,z,rev;
      System.out.println("enter m val");
      m=sc.nextInt();
      System.out.println("enter n val");
      n=sc.nextInt();
      while(m>n)
      {
           System.out.println("not valid.. try again");
           System.out.println("enter m val");
           m=sc.nextInt();
           System.out.println("enter n val");
           n=sc.nextInt();
      }
      sc.close();

      System.out.println("the prime-adam no.s within the range is ");
      
      if(m>10)
      m=10;

      for(i=m;i<n;i++)
      {
          for(j=1;j<=i;i++)
          {
            if(i%j==0)
            x++;
         }
         if(x==2)
         continue;
         y=Integer.reverse(i);
         z=i*i;
         rev=y*y;

         if(z==Integer.reverse(rev))
         System.out.print(i+" ");
      }



   }
    
}
spring sageBOT
# urban cosmos i cant seem to find whats wrong with this code ``` import java.util.Scanner; i...

Detected code, here are some useful tools:

Formatted code
import java.util.Scanner;
import java.lang.Integer;

public class primeAdam {
  public static void main(String args[] ) {
    Scanner sc = new Scanner(System.in);
    int m, n, i, j, x = 0, y, z, rev;
    System.out.println("enter m val");
    m = sc.nextInt();
    System.out.println("enter n val");
    n = sc.nextInt();
    while (m > n) {
      System.out.println("not valid.. try again");
      System.out.println("enter m val");
      m = sc.nextInt();
      System.out.println("enter n val");
      n = sc.nextInt();
    }
    sc.close();
    System.out.println("the prime-adam no.s within the range is ");
    if (m > 10) m = 10;
    for (i = m; i < n; i++) {
      for (j = 1; j <= i; i++) {
        if (i % j == 0) x++;
      }
      if (x == 2) continue ;
      y = Integer.reverse(i);
      z = i * i;
      rev = y * y;
      if (z == Integer.reverse(rev)) System.out.print(i + " ");
    }
  }
}
#

The code you provided is incomplete. It seems that you are trying to import the Scanner class and the Integer class from the java.util and java.lang packages respectively. However, you have not completed the import statement for the Scanner class.

To fix this issue, you need to complete the import statement for the Scanner class by adding "Scanner;" after "java.util.Scanner". The corrected code should look like this:

import java.util.Scanner;
import java.lang.Integer;

public class primeAdam {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      // rest of your code
   }
}

Additionally, make sure that you have closed all brackets and semicolons correctly in your code.

urban cosmos
feral meteor
#

You want to check for a number, but from your code I can see, you are checking in a range from m to n.. Why m > 10, m = 10 ?? Why if x == 2, then continue ??

urban cosmos
urban cosmos
#

the code keeps on taking values from the user and does not give anything

spring sageBOT
#

@urban cosmos

Mixing any nextXXX method with nextLine from the Scanner class for user input, will not ask you for input again but instead result in an empty line read by nextLine.

To prevent this, when reading user input, always only use nextLine. If you need an int, do

int value = Integer.parseInt(scanner.nextLine());

instead of using nextInt.

Assume the following:

Scanner scanner = new Scanner(System.in);

System.out.println("Enter your age:");
int age = scanner.nextInt();
System.out.println("Enter your name:");
String name = scanner.nextLine();

System.out.println("Hello " + name + ", you are " + age + " years old");

When executing this code, you will be asked to enter an age, suppose you enter 20.
However, the code will not ask you to actually input a name and the output will be:

Hello , you are 20 years old.

The reason why is that when you hit the enter button, your actual input is

20\n

and not just 20. A call to nextInt will now consume the 20 and leave the newline symbol \n in the internal input buffer of System.in. The call to nextLine will now not lead to a new input, since there is still unread input left in System.in. So it will read the \n, leading to an empty input.

So every user input is not only a number, but a full line. As such, it makes much more sense to also use nextLine(), even if reading just an age. The corrected code which works as intended is:

Scanner scanner = new Scanner(System.in);

System.out.println("Enter your age:");
// Now nextLine, not nextInt anymore
int age = Integer.parseInt(scanner.nextLine());
System.out.println("Enter your name:");
String name = scanner.nextLine();

System.out.println("Hello " + name + ", you are " + age + " years old");

The nextXXX methods, such as nextInt can be useful when reading multi-input from a single line. For example when you enter 20 John in a single line.

stark onyx
#

also, put a print into ur loop to see whether ur stuck in an infinite loop

urban cosmos
#

vscode suggested me to do it therefore did it

stark onyx
urban cosmos
stark onyx
stark onyx
urban cosmos
drowsy remnant
urban cosmos
stark onyx
urban cosmos
#

ill used to intellij then

urban cosmos
#
import java.util.Scanner;
import java.lang.Integer;
public class primeAdam 
{
   public static void main(String args[])
   {
      Scanner sc=new Scanner(System.in);
      int m,n,i,j,x=0,y,z,rev;
      System.out.println("enter m val");
      m=sc.nextInt();
      System.out.println("enter n val");
      n=sc.nextInt();
      while(m>n)
      {
           System.out.println("not valid.. try again");
           System.out.println("enter m val");
           m=sc.nextInt();
           System.out.println("enter n val");
           n=sc.nextInt();
      }
      

      System.out.println("the prime-adam no.s within the range is ");
      
      if(m>10)
      m=10;

      for(i=m;i<n;i++)
      {
         System.out.println("check");

         for(j=1;j<=i;i++)
         {
            if(i%j==0)
            x++;
         }
         if(x==2)
         {
   
          y=Integer.reverse(i);
          z=i*i;
          rev=y*y;
          if(z==Integer.reverse(rev))
          System.out.print(i+" ");
          
         }
         

      }

   }
}
#

this is the change i have made

#

what have i messed up

stark onyx
#

mind sharing the input and output?

#

is it still in an infinite loop?

#

also, u should replace ur scanner nextInt, as the bot explained

#

or ull run into more issues later on

urban cosmos
urban cosmos
stark onyx
#

not with nextInt()

drowsy remnant
#

Did you read the bots message?

urban cosmos
#

I understand now

feral meteor
urban cosmos
#

And could I proceed

feral meteor
#

If the number is prime, why you continue, because as per the code, x == 2 is true when number is prime

urban cosmos
feral meteor
#

Do you even know, what Integer.reverse() function does??

urban cosmos
#

That's what I think it does

feral meteor
#

not the number

urban cosmos
#

What does that mean?

#

Please explain with an example

feral meteor
#

Check this out

twin crescent
#

Confused question. What do you need?

urban cosmos
#

Even after accepting m and n it prints that one line and then continues to ask for inputs from the user

placid knot
#

can u post your current code ? the code u posted above does not have any behavior like that

feral meteor
#
  1. U are wrongly reversing the number
  2. You are not incrementing j pointer
    Change these two, and ur code will work
urban cosmos
urban cosmos
placid knot
#

do what Deepak suggested, start with his number 2 suggestion. That shouldnt take longer then 5 seconds to fix

urban cosmos
#

but is there any function that can reverse an int

placid knot
#

multiple ones, including a very easy to use

#

fix your loop first

urban cosmos
# placid knot fix your loop first
for(i=m;i<n;i++)
      {
         System.out.println("the prime-adam no.s within the range is ");

         for(j=1;j<=i;j++)
         {
            if(i%j==0)
            x++;
         }
         if(x==2)
         {
   
          y=Integer.reverse(i);
          z=i*i;
          rev=y*y;
          if(z==Integer.reverse(rev))
          System.out.print(i+" ");
          
         }
         

      }

   }
}
#

i made the change

placid knot
#

and did u actually run it ? 😉

urban cosmos
#

oh Wavey

#

the output

feral meteor
feral meteor
urban cosmos
#

but what is the use of shifting bits?

#

what will it do?

#
for(j=1;j<=i;j++)
         {
            if(i%j==0)
            x++;
         }
         if(x==2)
         {
   
          y = Integer.parseInt(new StringBuilder(""+i).reverse().toString());
          z=i*i;
          rev=y*y;
          int rev1=Integer.parseInt(new StringBuilder(""+rev).reverse().toString());
          if(z==rev1)
          System.out.print(i+" ");
          
         }
         
#

am i right?

#

and what exactly is string builder?

urban cosmos
#

and wb stringbuffer?

#

would i be able to achieve this using stringbuffer?

lyric fiber
urban cosmos
lyric fiber
urban cosmos
feral meteor
#

ur code should look like this!

#
import java.lang.Integer;

public class primeAdam 
{
   public static void main(String args[])
   {
      Scanner sc=new Scanner(System.in);
      System.out.println("enter m val");
      int m=sc.nextInt();
      System.out.println("enter n val");
      int n=sc.nextInt();

      while(m>n)
      {
           System.out.println("not valid.. try again");
           System.out.println("enter m val");
           m=sc.nextInt();
           System.out.println("enter n val");
           n=sc.nextInt();
      }

      System.out.println("the prime-adam no.s within the range is ");
      
      if(m>10)
        m=10;

      for(int i=m;i<n;i++)
      {
        int x = 0;

         for(int j=1;j<=i;j++)
         {
            if(i%j==0)
                x++;
         }

         if(x==2)
         {
   
          int y = Integer.parseInt(new StringBuilder(""+i).reverse().toString());
          int z=i*i;
          int rev=y*y;
          if(z == Integer.parseInt(new StringBuilder(""+rev).reverse().toString()))
            System.out.print(i+" ");
         }
      }
   }
}```
urban cosmos
#

Sure thing lemme check this out mate

feral meteor
urban cosmos
#

thanks for the help mate

feral meteor
urban cosmos
#

im gonne learn a lil more on string builder

#

seems interesting

feral meteor
#

yeah, that is good if you want to play with strings, because java has immutable strings

#

and it makes string mutable

urban cosmos
#

anyway thanks for the help mate

#

appreciate it

feral meteor
#

np! 🙂

urban cosmos
#

i can learn alot from you

feral meteor
#

obvio cat_thumbs_up