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-programming: Program to help Priyanka to generate the PIN

Priyanka an elite CBI agent have been working on a cyber crime case has intercepted an electronic safe which can be opened by an set of 4 digit pin. Now these 4 digit pins are actually the word count of a corresponding sentences.

Priyanka has received a bunch of sentences from one of her mole and need your help in generating the pin

Input Sentences

Sentence #1: Good morning

Sentence #2: India is a democratic country

Sentence #3: Long live the revolution

Sentence #4: Rights to speech

Pin: 2543

Your are requested to write a java program to generate the pin and save your country. Jai Hind..!!

Input Specification :

Four Sentences in consecutive line

Output Specification :

Ouput would be a 4 digit PIN

Sample input :

Good morning

India is a democratic country

Long live the revolution

Rights to speech

Sample output :

2543

Sample Input:

Why won´t each wisdom exceed any incorporated junk?
Why can´t every overtone walk around the merry sail?
When will the glad market switch the circulating tray?

Sample Output:

8990

Hint: All sentences word count would be less than 10


corejava x 352
programming x 168
Posted On : 2018-01-23 19:46:42.0
profile Xda Coder - anyforum.in Xda Coder
1300
up-rate
2
down-rate

Answers


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CBIPin {
public static void main(String[] args) throws Exception
{
Scanner input=new Scanner(System.in);
int count=0;
List<String> sentences=new ArrayList<String>();
while(count<4){
sentences.add(input.nextLine().trim());
count++;
}

for(String sentence:sentences){
int length=sentence.split(" ").length;
if(length<10){
if(length==1 && sentence.trim().length()<1)
System.out.print(0);
else
System.out.print(length);
}else{
throw new Exception("Invalid Arguments!");
}
}
}
}


Note:
------------------------------
New line space has to be there in case of blank sentences. Like if there is no 4th sentence, input must be like:

Input-
-----------------------------------------
Sentence-1
Sentence-2
Sentence-3

------------------------------------------

instead of

Input-
-----------------------------------------
Sentence-1
Sentence-2
Sentence-3
------------------------------------------

otherwise you have to press enter manually to see the output.

Posted On : 2018-01-24 23:47:18
Satisfied : 1 Yes  1 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188221932
Reply This Thread
up-rate
3
down-rate

Solution 1: All the sentences takes from console at a time.


import java.util.Scanner;
public class CBIPin {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String[] s=new String[4];
int sum=0;
for(int i=0;i<s.length;i++)
{
String st=sc.nextLine();
String[] str=st.split("\\s");
if(st.equals(""))
{
System.out.print("0");
}
else
{
for(String w:str)
{
sum=sum+1;
}
System.out.print(sum);
sum=0;
}
}
}
}


Solution 2 : Take each sentence from console and print the output once

import java.util.Scanner;
public class CBIPin2
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
String[] str = new String[4];
int sum=0;
for (int i = 0; i < str.length; i++)
{
str[i]=s.nextLine();
}
for(int i=0; i<str.length;i++)
{
String[] str2=str[i].split("\\s");
if(str[i].equals(""))
{
System.out.print("0");
}
else
{
for(String w:str2)
{
sum=sum+1;
}
System.out.print(sum);
sum=0;
}
}

}
}

Posted On : 2018-01-24 20:37:52
Satisfied : 0 Yes  2 No
profile Sudheer Kumar Javvadi - anyforum.in Sudheer Kumar Javvadi
0960
Reply This Thread
up-rate
-3
down-rate
Comments
Length < 10 is missing and if we don´t input 4th line it needs to be 0, not exception NoSuchElementException : No Line Found
profile Xda Coder - anyforum.in Xda Coder
13  0  0
Posted On :2018-01-24 21:28:04.0
Leave a Comment
when i misses the 4th line it prints "0" .check the if condition that i have used in both solutions.however i will resubmit the code with sentence word length less than 10.
profile Sudheer Kumar Javvadi - anyforum.in Sudheer Kumar Javvadi
0  96  0
Posted On :2018-01-24 22:57:48.0
Leave a Comment

As per the requirement i have restricted the sentence word count
to less than 10 at split() method . Check the codes and let me know.

Solution 1: All elements take at a time from console


import java.util.Scanner;
public class CBIPin {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String[] s=new String[4];
int sum=0;
for(int i=0;i<s.length;i++)
{
String st=sc.nextLine();
String[] str=st.split("\\s",9);
if(st.equals(""))
{
System.out.print("0");
}
else
{
for(String w:str)
{
sum=sum+1;
}
System.out.print(sum);
sum=0;
}
}
}
}


Solution 2 : Take each element from console and print the output once

import java.util.Scanner;
public class CBIPin2
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
String[] str = new String[4];
int sum=0;
for (int i = 0; i < str.length; i++)
{
str[i]=s.nextLine();
}
for(int i=0; i<str.length;i++)
{
String[] str2=str[i].split("\\s", 9);
if(str[i].equals(""))
{
System.out.print("0");
}
else
{
for(String w:str2)
{
sum=sum+1;
}
System.out.print(sum);
sum=0;
}
}

}
}


Posted On : 2018-01-24 23:12:25
Satisfied : 0 Yes  0 No
profile Sudheer Kumar Javvadi - anyforum.in Sudheer Kumar Javvadi
0960
Reply This Thread
up-rate
2
down-rate

import java.util.Scanner;

class Jaihind
{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
String str;
String a[];
int ans[]=new int[4];
String b[]=new String[4];
int i=0;
while(sc.hasNextLine()){
str=sc.nextLine();
a=str.split(" ");
ans[i]=a.length;
i++;

}

for(int k=0;k<4;k++){
System.out.print(ans[k]);
}
}

}

Posted On : 2018-01-25 18:06:08
Satisfied : 5 Yes  0 No
profile Divesh - anyforum.in Divesh
101180
Reply This Thread
up-rate
-1
down-rate



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