Jump to content

[HELP] pls check my JAVA program for errors

Go to solution Solved by colonel_mortis,

Your problem is this loop

		while(temp>0){    //while loop to count number of digits and form multiple of 10 to do modulus  division
			temp=temp%10; 
			d=d*10;       
		}     //loop ends  

The modulo operator, %, returns the remainder when divided, so 5 % 10 = 5. This means that you have an infinite loop.

To fix it, you need to use / rather than %, so it will divide by 10 each time (and it uses integer division since the arguments are integers, so it rounds down).

This program is to check if a number is Automorphic (i.e. a number whose square ends in itself. example - 25*25 = 625)

the logic of my program seems to be right to me, but BlueJ does not want to do anything after i enter a number, it does nothing.

(PS ima noob so pls dont overwhelm me with complicated crap)

 

here's the program:

import java.io.*;
public class AutomorphicNumber {  //start of class 
	public static void main(String[] args)throws IOException {  //main method starts
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
		System.out.println("Enter number to check Automorphic or not : "); 
		int n = Integer.parseInt(br.readLine()); //taking user input
		int temp=n;
		int d=1;
		while(temp>0){    //while loop to count number of digits and form multiple of 10 to do modulus  division
			temp=temp%10; 
			d=d*10;       
		}     //loop ends            
		if ((n*n) % d == n)  //get original number from square
			System.out.println("It is an Automorphic Number.");
		else
			System.out.println("It is not an Automorphic Number.");
	}  //end of method
}  //end of class


 

Screenshot (7).png

Screenshot (8).png

Edited by colonel_mortis
Code tags and cleaned up code formatting
Link to comment
Share on other sites

Link to post
Share on other sites

lol...

Since this is Java, your int temp and int n both have reference to the same memory pointer.

And when you exit the following loop - both temp and n values are at 0 (or less)

 

When you first assign temp value, try this instead.

int temp = Integer.valueOf(n);

Yes Java (and C#) are retarded like that... they are built to get rid of manual memory management and pointers, but then introduce shit like this for God knows what reason....

 

good luck and have fun

 

EDIT:

umm... didn't read the OP careful enough, guess your problem is solved down below, but it's still okay to know about this stuff.

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

I'm too acustomed to Java, but I would guess standard input got stuck or something.

 

Have you tried using a scanner and a nextint like here:

 

https://www.tutorialspoint.com/java/util/scanner_nextint.htm

If you want to reply back to me or someone else USE THE QUOTE BUTTON!                                                      
Pascal laptops guide

Link to comment
Share on other sites

Link to post
Share on other sites

Your problem is this loop

		while(temp>0){    //while loop to count number of digits and form multiple of 10 to do modulus  division
			temp=temp%10; 
			d=d*10;       
		}     //loop ends  

The modulo operator, %, returns the remainder when divided, so 5 % 10 = 5. This means that you have an infinite loop.

To fix it, you need to use / rather than %, so it will divide by 10 each time (and it uses integer division since the arguments are integers, so it rounds down).

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, DXMember said:

lol...

Since this is Java, your int temp and int n both have reference to the same memory pointer.

And when you exit the following loop - both temp and n values are at 0 (or less)

 

When you first assign temp value, try this instead.


int temp = Integer.valueOf(n);

Yes Java (and C#) are retarded like that... they are built to get rid of manual memory management and pointers, but then introduce shit like this for God knows what reason....

 

good luck and have fun

 

EDIT:

umm... didn't read the OP careful enough, guess your problem is solved down below, but it's still okay to know about this stuff.

yup, its solved. Thanks for answering :)

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×