help me plz , write a java program that reads a number and verifies if it has the same value when we read it from right to left and from left to right. my program should display "having the same value so it is readable from both sides" if the condition is true and "having different values so it is not readable from both sides" if the condition is false.

example:
1525251: "having the same value so it is readable from both sides".
4548446: "having different value , so it is not readable from both side".

plz help me the solve should be with out using method just array . like int[] myarray=new int ......

Recommended Answers

All 14 Replies

Well, post what you have so far, we are not simply going to do it for you.

Check the API of the String class. There is a method that converts the String into an array of chars.
Now loop that array and see if the first element is equal with the last.
The 2nd with the last -1
The 3rd with the last -2
The 4th with the last -3

If you find something that is not equals then return false.

import java.util.Scanner:
public static void main(String[] args){
int i;
int [] array=new int[7];
Scanner input = new Scanner(System.in);
System.out.printIn("Enter the elements: ");
array=input.nextInt();
for(i=0; i<array.length/2; i++){
if(array==array[array.length-i]){
System.out.printIn("its readable from both sides");
}
else
System.out.printIn("its not readable from both sides");
}}}


so can u tell me where is the mistake and correct it

package javaproject;
import java.util.Scanner;
public class NewClass {
public static void main(String[]args){

    int []array=new int[2];
    int i;

    Scanner input=new Scanner(System.in);



for(  i=0; i<array.length; i++){
    System.out.print("Enter the element: ");
    array[i]=input.nextInt();

}

       for(   i=0; i<array.length/2; i++){
    if(array[i]==array[array.length-1]){

        System.out.println("its readable");
array[i]=input.nextInt();

    }

     for(  i=0; i<array.length; i++){
         if(array[i]!=array[array.length-1]){

            System.out.print(" not readable");
         }
     }}}}

i tried this one it work but the output is to enter two different elements or two similar elements while the question is only to enter only one elements . but when i change to one elements its doesnt display readable or not readable. can u help me here

no, we can't.
Read the posting guidelines, code without proper formatting and code tags won't be looked at.

no, we can't.
Read the posting guidelines, code without proper formatting and code tags won't be looked at.

so tell me wat i have to do , i tried my best to solve this problem but i couldnt get it.

start using code tags when posting code for starters. We're NOT going to help you until and unless you do that.

the program u show is a different story. that is check if they two number u enter are the same.
but you're checking a digit from left to right is the same.

when u writing ur program, you need a loop to loop thought each character of your input. after checking the whole character, print the reachable msg, if not print the unreachable msg.

this is the loop im using =) have fun

for(int i = 0; i < input.length()/2; i++){
				if(input.charAt(i) != input.charAt(input.length()-(i+1))){
					valid = false;
				}
import java.util.Scanner:
public static void main(String[] args){
int i;
int [] array=new int[7];
Scanner input = new Scanner(System.in);
System.out.printIn("Enter the elements: ");
array[i]=input.nextInt();
for(i=0; i<array.length/2; i++){
if(array[i]==array[array.length-i]){
System.out.printIn("its readable from both sides");
}
else 
System.out.printIn("its not readable from both sides");
}}}

so can u correct my mistake or not

Why are you making a length 7 int array and then reading a single int and attempting to assign it to an undefined array element (at that point i is undefined).

Read the input as a single int (into it's own int varaible, not an array), to make sure that it is an int, then use String.valueOf(int).toCharArray() to get the character array (i.e. one array character element per digit in the number), then run through your loop with that char array, rather than an int array.

Edit: Now, take that info and at least try something. And, when you post here, then you tell us exactly what you're doing wrong. As of yet, you have not, even once, hinted at what kind of problems you're getting. Compiler messages, exceptions, simply unexpected or missing output. Nothing. How are we to help you solve your problem when we don't know what that problem is.

import java.util.Scanner;
public class NewClass {
	public static void main(String[]args){
		String input;
		int counter = 0;
		boolean valid = true;
		Scanner in = new Scanner(System.in);
		int[] array = new int[10];

		while(counter < array.length){
			System.out.println("Enter a integer");
			input = in.next();
			for(int i = 0; i < input.length()/2; i++){
				if(input.charAt(i) != input.charAt(input.length()-(i+1))){
					valid = false;
				}
			}
			if(valid){
				System.out.println("reachable");
				System.out.println();
				counter++;
			}
			else{
				System.out.println("Not reachable");
				System.out.println();
			}
		}
	}
}

try to understand this code ~~ remember to add the elements into the array

well, why u want to add the elements into the array? i dont see you want to print it..?

i know that there are many solution without usin an array put i tried to make in array

Uhm, read my post again, you will be using an array, but not creating or populating it in the manner in which you are "attempting" it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.