Answer to Question #57462 in Java | JSP | JSF for Akosmeeta Hazarika

Question #57462
Write a Java program to accept a number of three or more digits and form a number by exchanging the digits at both the ends..Display an appropriate error message if user's given number is less than 3 digits.
1
Expert's answer
2016-01-25T07:05:14-0500
package com.company;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

public static void main(String[] args) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str;
str = reader.readLine();
try {
Integer.parseInt(str);
if (str.length() >= 3) {
char[] chars = str.toCharArray();
char first = chars[0];
char last = chars[chars.length-1];
chars[0] = last;
chars[chars.length-1] = first;
System.out.println(chars);
}else
System.err.println("Number is less than 3 digits");
} catch (NumberFormatException e) {
System.err.println("Wrong format string!");
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS