60 465
Assignments Done
98,1%
Successfully Done
In April 2018

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.
Expert's answer
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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions