SlideShare a Scribd company logo
1 of 2
1. Write a java program that reads a String from the user and prints out each individual character
from the String on it's own line in the command line (i.e. a column instead of a row. Use
System.out.println)
2. Do the same thing as in Assignment6_1, except print it in reverse (down the column).
Solution
A java program that reads a String from the user and prints out each individual character
from the String on it's own line in the command line
public static void main(String[] args) {
// ask the user to enter a string
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter a string: ");
String s = input.nextLine();
// the user to enters a character
System.out.print("Enter a character: ");
char ch = input.nextLine().charAt(0);
int count = count(s, ch);
System.out.println(count);
}
public static int count(String str, char ch) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ch) {
count++;
}
}
return count;
1- Write a java program that reads a String from the user and prints o.docx

More Related Content

Similar to 1- Write a java program that reads a String from the user and prints o.docx

ppt on scanner class
ppt on scanner classppt on scanner class
ppt on scanner class
deepsxn
 

Similar to 1- Write a java program that reads a String from the user and prints o.docx (8)

MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdfMagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
 
Lab101.pptx
Lab101.pptxLab101.pptx
Lab101.pptx
 
ppt on scanner class
ppt on scanner classppt on scanner class
ppt on scanner class
 
Star pattern programs in java Print Star pattern in java and print triangle ...
Star pattern programs in java Print Star pattern in java and  print triangle ...Star pattern programs in java Print Star pattern in java and  print triangle ...
Star pattern programs in java Print Star pattern in java and print triangle ...
 
Class notes(week 5) on command line arguments
Class notes(week 5) on command line argumentsClass notes(week 5) on command line arguments
Class notes(week 5) on command line arguments
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdf
 
Anjalisoorej imca133 assignment
Anjalisoorej imca133 assignmentAnjalisoorej imca133 assignment
Anjalisoorej imca133 assignment
 
CountStringCharacters.javaimport java.util.Scanner; public cla.pdf
CountStringCharacters.javaimport java.util.Scanner; public cla.pdfCountStringCharacters.javaimport java.util.Scanner; public cla.pdf
CountStringCharacters.javaimport java.util.Scanner; public cla.pdf
 

More from cwayne3

How would you describe the macroeconomic --worldview-- of David Ricard.docx
How would you describe the macroeconomic --worldview-- of David Ricard.docxHow would you describe the macroeconomic --worldview-- of David Ricard.docx
How would you describe the macroeconomic --worldview-- of David Ricard.docx
cwayne3
 
10) Which species acts as the nucleophile in the reaction of 1-butene.docx
10) Which species acts as the nucleophile in the reaction of 1-butene.docx10) Which species acts as the nucleophile in the reaction of 1-butene.docx
10) Which species acts as the nucleophile in the reaction of 1-butene.docx
cwayne3
 
1-) What are the tax concepts involved in completing the Form 1040- Ex.docx
1-) What are the tax concepts involved in completing the Form 1040- Ex.docx1-) What are the tax concepts involved in completing the Form 1040- Ex.docx
1-) What are the tax concepts involved in completing the Form 1040- Ex.docx
cwayne3
 
1- Which planet has the least cloudiness- b) Earth c) Mars d) Jupiter.docx
1- Which planet has the least cloudiness- b) Earth c) Mars d) Jupiter.docx1- Which planet has the least cloudiness- b) Earth c) Mars d) Jupiter.docx
1- Which planet has the least cloudiness- b) Earth c) Mars d) Jupiter.docx
cwayne3
 

More from cwayne3 (16)

I am trying to create a code that will show an error if someone puts a.docx
I am trying to create a code that will show an error if someone puts a.docxI am trying to create a code that will show an error if someone puts a.docx
I am trying to create a code that will show an error if someone puts a.docx
 
I need a short code that is ritten in Visual basic that explains that.docx
I need a short code that is ritten in Visual basic that explains that.docxI need a short code that is ritten in Visual basic that explains that.docx
I need a short code that is ritten in Visual basic that explains that.docx
 
i Encapsulation} ra te compilation is to achieve encapsulation- How to.docx
i Encapsulation} ra te compilation is to achieve encapsulation- How to.docxi Encapsulation} ra te compilation is to achieve encapsulation- How to.docx
i Encapsulation} ra te compilation is to achieve encapsulation- How to.docx
 
How would you characterize Web 2-0- Describe the set of technologies t.docx
How would you characterize Web 2-0- Describe the set of technologies t.docxHow would you characterize Web 2-0- Describe the set of technologies t.docx
How would you characterize Web 2-0- Describe the set of technologies t.docx
 
How would you describe the macroeconomic --worldview-- of David Ricard.docx
How would you describe the macroeconomic --worldview-- of David Ricard.docxHow would you describe the macroeconomic --worldview-- of David Ricard.docx
How would you describe the macroeconomic --worldview-- of David Ricard.docx
 
1-Put the ionic compounds in order of smallest to largest lattice ener.docx
1-Put the ionic compounds in order of smallest to largest lattice ener.docx1-Put the ionic compounds in order of smallest to largest lattice ener.docx
1-Put the ionic compounds in order of smallest to largest lattice ener.docx
 
10- which of the following should be the first step in reviewing he fi.docx
10- which of the following should be the first step in reviewing he fi.docx10- which of the following should be the first step in reviewing he fi.docx
10- which of the following should be the first step in reviewing he fi.docx
 
1-8(x+2)-4- 12 Please show steps for pointsSolution 1-8(x+2)-4- 12.docx
1-8(x+2)-4- 12 Please show steps for pointsSolution 1-8(x+2)-4- 12.docx1-8(x+2)-4- 12 Please show steps for pointsSolution 1-8(x+2)-4- 12.docx
1-8(x+2)-4- 12 Please show steps for pointsSolution 1-8(x+2)-4- 12.docx
 
10- Dinitrogen trioxide is a molecular compound t-f 11- Lithium phosph.docx
10- Dinitrogen trioxide is a molecular compound t-f 11- Lithium phosph.docx10- Dinitrogen trioxide is a molecular compound t-f 11- Lithium phosph.docx
10- Dinitrogen trioxide is a molecular compound t-f 11- Lithium phosph.docx
 
10) Which species acts as the nucleophile in the reaction of 1-butene.docx
10) Which species acts as the nucleophile in the reaction of 1-butene.docx10) Which species acts as the nucleophile in the reaction of 1-butene.docx
10) Which species acts as the nucleophile in the reaction of 1-butene.docx
 
1-) What are the tax concepts involved in completing the Form 1040- Ex.docx
1-) What are the tax concepts involved in completing the Form 1040- Ex.docx1-) What are the tax concepts involved in completing the Form 1040- Ex.docx
1-) What are the tax concepts involved in completing the Form 1040- Ex.docx
 
1-) Which of the following accounting concepts do accountants and audi.docx
1-) Which of the following accounting concepts do accountants and audi.docx1-) Which of the following accounting concepts do accountants and audi.docx
1-) Which of the following accounting concepts do accountants and audi.docx
 
1- Why does the initially formed copper hydroxide precipitate dissolve.docx
1- Why does the initially formed copper hydroxide precipitate dissolve.docx1- Why does the initially formed copper hydroxide precipitate dissolve.docx
1- Why does the initially formed copper hydroxide precipitate dissolve.docx
 
1- Which substance CANNOT have dipole-dipole forces between its molecu.docx
1- Which substance CANNOT have dipole-dipole forces between its molecu.docx1- Which substance CANNOT have dipole-dipole forces between its molecu.docx
1- Which substance CANNOT have dipole-dipole forces between its molecu.docx
 
1-Write an algebraic expression to represent each verbal expression-.docx
1-Write an algebraic expression to represent each verbal expression-.docx1-Write an algebraic expression to represent each verbal expression-.docx
1-Write an algebraic expression to represent each verbal expression-.docx
 
1- Which planet has the least cloudiness- b) Earth c) Mars d) Jupiter.docx
1- Which planet has the least cloudiness- b) Earth c) Mars d) Jupiter.docx1- Which planet has the least cloudiness- b) Earth c) Mars d) Jupiter.docx
1- Which planet has the least cloudiness- b) Earth c) Mars d) Jupiter.docx
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 

1- Write a java program that reads a String from the user and prints o.docx

  • 1. 1. Write a java program that reads a String from the user and prints out each individual character from the String on it's own line in the command line (i.e. a column instead of a row. Use System.out.println) 2. Do the same thing as in Assignment6_1, except print it in reverse (down the column). Solution A java program that reads a String from the user and prints out each individual character from the String on it's own line in the command line public static void main(String[] args) { // ask the user to enter a string java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter a string: "); String s = input.nextLine(); // the user to enters a character System.out.print("Enter a character: "); char ch = input.nextLine().charAt(0); int count = count(s, ch); System.out.println(count); } public static int count(String str, char ch) { int count = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == ch) { count++; } } return count;