Introduction to my problem:
I have to code a program that ask for the user to enter a positive integer and then I have to find if the value that was enter is divisible by 11 (with no remainder) and display "yes" if it is divisible and "no" if it is not.
However, the method that I have to use to determine if the enter value is divisible by 11 (with no remainder) is by using a certain method demonstrated below:
For example suppose that n=9343752, then t=2-5+7-3+4-3+9==11. 11 is
divisible by 11 then it follows that 8784204 is divisible by 11.
If n=843439 then t=9-3+4-3+4-8=3. 3 is not divisible by 11 then
843439 is not divisible by 11.
if the user enter's a non postive integer the program should continue asking for a positive value until
the user does so.
My code so far:
My code below accepts only 10-digit integers and I have struggled for it to accept any postive integers. It will not run if enter a negative 10-digit integer.
[FONT=Arial Black][SIZE=2]public class DivisionBy11{ public static void main(String[] args) throws IOException{ InputStreamReader in = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(in); System.out.println("Please enter a positive integer"); String numbers = br.readLine(); while( Keyboard.ReadInt() <0){[COLOR=Lime] /*I'm try to check if the enter value is a positive integer and if it's not, to prompt the user to enter a positive integer. So I've been unsuccessful.*/[/COLOR]:( System.out.println("Please enter a positive integer"); } String numbers1 = numbers.substring(9,10); String numbers2 = numbers.substring(8,9); String numbers3 = numbers.substring(7,8); String numbers4 = numbers.substring(6,7); String numbers5 = numbers.substring(5,6); String numbers6 = numbers.substring(4,5); String numbers7 = numbers.substring(3,4); String numbers8 = numbers.substring(2,3); String numbers9 = numbers.substring(1,2); String numbers10 = numbers.substring(0,1); int numberi1 = Integer.parseInt(numbers1); int numberi2 = Integer.parseInt(numbers2); int numberi3 = Integer.parseInt(numbers3); int numberi4 = Integer.parseInt(numbers4); int numberi5 = Integer.parseInt(numbers5); int numberi6 = Integer.parseInt(numbers6); int numberi7 = Integer.parseInt(numbers7); int numberi8 = Integer.parseInt(numbers8); int numberi9 = Integer.parseInt(numbers9); int numberi10 = Integer.parseInt(numbers10); System.out.println("The number was " + numbers); float t = 0; float n=0; int myvalue = 0; t = numberi1 - numberi2 + numberi3 - numberi4 + numberi5 - numberi6 + numberi7 - numberi8 + numberi9 - numberi10; System.out.println(t); n = t / 11; if(Math.round(n)==myvalue){ [COLOR=Lime]//I'm having difficulty in check the return value is an integer[/COLOR]:-ss System.out.println("Yes"); } else System.out.println("No"); } }[/SIZE][/FONT]
Any help will be greatly appreciated.