AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-string-handling: WAP to concatenate the last 2 characters from each string stored in an Array

Write a program to read an array of string values, Concatenate the last 2 characters in each string and print it.

Input specification:

The first line of input will contain the number of strings in the array. The string values, one per line will follow.

Sample Input :

3

java
coding
bugs

Sample Output :

vanggs

corejava x 326
string-handling x 34
Posted On : 2017-07-17 11:43:43.0
profile Divesh - anyforum.in Divesh
5100
up-rate
3
down-rate

Answers


import java.util.Scanner;

public class SubStringExample {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n=Integer.parseInt(input.nextLine());
StringBuilder output=new StringBuilder();
String word=null;
for(int i=0;i<n;i++){
word=input.nextLine().trim();
output.append(word.substring(word.length()>=2?word.length()-2:0));
}
System.out.println(output);
}
}

Posted On : 2017-07-17 18:35:24
Satisfied : 1 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596128121888
Reply This Thread
up-rate
4
down-rate
Comments
can solve using array?
profile Divesh - anyforum.in Divesh
51  0  0
Posted On :2017-07-18 19:18:52.0
Leave a Comment

java.util.NoSuchElementException: No line found

Posted On : 2017-07-20 12:03:50
Satisfied : 0 Yes  0 No
profile Srishti - anyforum.in Srishti
000
Reply This Thread
up-rate
0
down-rate
Comments
Hi Shrishti,
I think you are giving input in single line, All words must be in different different line. Please try once again.
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1854  18676
Posted On :2017-07-20 19:54:10.0
Leave a Comment

Check this below code and let me know if your conditions are satisfied or not.

import java.util.Scanner;
import java.lang.*;
class ConcatlastTwo
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String[] str=new String[n];
String st="";
for(int i=0;i<n;i++)
{
str[i]=sc.next();
}
for(int i=0;i<n;i++)
{
if(str[i].length()>=2)
{
int len1=str[i].length()-2;
int len2=str[i].length();
st+=str[i].substring(len1, len2);
}
else
{
System.out.println("::You have entered Less Than 2 length size strings ! Check and try again !!!!::");
System.exit(0);
}
}
System.out.println(st);
}
}

Posted On : 2017-07-20 22:53:29
Satisfied : 1 Yes  0 No
profile sudheerkumar Javvadi - anyforum.in sudheerkumar Javvadi
0920
Reply This Thread
up-rate
2
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in