I need this in the simplest form you can give me, so I can understand it no uppe
ID: 3713113 • Letter: I
Question
I need this in the simplest form you can give me, so I can understand it no upper crazy level things that I have never learned
In this exercise you will develop skills with input and output of text strings. Start with the code below and complete the following tasks:
a) Read String input1 with a Scanner and print each word.
b) Read String input2 with a Scanner and print each character.
c) Read String input3 with a Scanner and print each character and whether it is a letter, a digit, or whitespace.
d) Read String input4 with a Scanner and print each line on a different line.
Explanation / Answer
ReadStrings.java
import java.util.Scanner;
public class ReadStrings {
public static void main(String[] args) {
String str="";
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Sentence :");
str=sc.nextLine();
String arr[]=str.split(" ");
System.out.println("Displaying Each Word in the String:");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
System.out.print("Enter a String :");
str=sc.next();
System.out.println("Displaying Each Character in the String:");
for(int i=0;i<str.length();i++)
{
System.out.println(str.charAt(i));
}
sc.nextLine();
System.out.print("Enter a Sentence :");
str=sc.nextLine();
System.out.println("Displaying Each Character in the String:");
for(int i=0;i<str.length();i++)
{
if(isAlphabet(str.charAt(i)))
{
System.out.println("'"+str.charAt(i)+"' is an Alphabet");
}
else if(Character.isDigit(str.charAt(i)))
{
System.out.println(str.charAt(i)+" is Digit");
}
else if(str.charAt(i)==' ')
{
System.out.println("'"+str.charAt(i)+"' is whitespace");
}
}
System.out.print("Enter a Sentence :");
str=sc.nextLine();
String lines[]=str.split(" ? ");
System.out.println("Displaying Lines :");
for (String line : lines) {
System.out.println(line);
}
}
private static boolean isAlphabet(char c) {
if(c>='A' && c<='z' || c>='a' && c<='Z')
return true;
else
return false;
}
}
__________________
Output:
Enter a Sentence :Now is the time for all good men to come to the aid of their country
Displaying Each Word in the String:
Now
is
the
time
for
all
good
men
to
come
to
the
aid
of
their
country
Enter a String :abcdefghijklmnopqrstuvwxyz0123456789
Displaying Each Character in the String:
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
0
1
2
3
4
5
6
7
8
9
Enter a Sentence :a1b2c3 d4
Displaying Each Character in the String:
'a' is an Alphabet
1 is Digit
'b' is an Alphabet
2 is Digit
'c' is an Alphabet
3 is Digit
' ' is whitespace
'd' is an Alphabet
4 is Digit
Enter a Sentence :Line 1 Line2 Line3 Line4
Displaying Lines :
Line 1
Line2
Line3
Line4
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.