Question

In: Computer Science

Java Programming Create a class named Problem1, and create a main method, the program does the...

Java Programming Create a class named Problem1, and create a main method, the program does the following:

- Prompt the user to enter a String named str. - Prompt the user to enter a character named ch.

- The program finds the index of the first occurrence of the character ch in str and print it in the format shown below.

- If the character ch is found in more than one index in the String str, the program prints the first index only.

- You may not use any built-in String methods to complete the task. You can only use the chartAt()and length() methods from the String class.

- Copy the output to a text file named Problem1.txt example output:

https://media.cheggcdn.com/media/c1a/c1a52903-6edb-4c01-a19d-dbb94d7a6308/phpvOMJmm.png

Solutions

Expert Solution

// Required program

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;

public class Problem1
{
   public static void main(String[] args) throws IOException {
   Scanner scanner = new Scanner(System.in);
  
   //Instantiating the File class
File file = new File("Problem1.txt");
//Instantiating the PrintStream class
PrintStream stream = new PrintStream(file);
  
   System.out.print("Enter a text: ");
   String inputString = scanner.nextLine();
  
   System.out.print("Enter a character to look for: ");
   char ch = scanner.next().charAt(0);
  
   boolean flag = false;
  
   String outputString = "";
  
   for(int i = 0; i < inputString.length(); i ++) {
   if(inputString.charAt(i) == ch){
   outputString = ch + " was found at index " + i;
   flag = true;
   break;
   }
   }
  
   if(flag == false) {
   outputString = ch + " was not found in the String " + inputString;
   }
  

  System.out.println(outputString);


System.out.println("Output copied to the text file " + file.getAbsolutePath());
  
System.setOut(stream);
  
System.out.println("Enter a text: " + inputString);
System.out.println("Enter a character to look for: " + ch);
System.out.println(outputString);

   }
}

// Outputs:

CONSOLE OUPUT

PROBLEM1.TXT FILE OUTPUT

CONSOLE OUTPUT

PROBLEM1.TXT FILE OUTPUT


Related Solutions

Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user to enter a short sentence which ends in a period. Use Java String class methods to determine the following about the sentence and display in the console: • How many total characters are in the sentence? • What is the first word of the sentence? Assume the words are separated by a space. • How many characters are in the first word of the...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
In java: -Create a class named Animal
In java: -Create a class named Animal
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then create 4 classes: 1- Leaf: that implements turn(), which changes the color of the Leaf object and returns true. If for any reason it is unable to change color, it should return false (you can come up with a reason for failure). The new color can be determined at random. 2- Document: that implements turn(), which changes the page on the document to the...
Part I: Have a program class named TestArrays This class should have a main() method that...
Part I: Have a program class named TestArrays This class should have a main() method that behaves as follows: Have an integer array of size 10. Using a loop, fill the elements with increments of 5, starting with 5 in the first element. Using the array elements, sum the second, fifth and seventh elements. Display the sum with a label. Change the fourth element to 86. Subtract 9 from the ninth element. Using a loop, display the elements of the...
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for...
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for loop, display each element of this array: String[] names = {"alice", "bob", "carla", "dennis", "earl", "felicia"}; Part 2 (30%) In a new class, implement two methods that will each calculate and return the average of an array of numeric values passed into it. Constraints: your two methods must have the same name one method should accept an array of ints; the other should accept...
Java program Create a constructor for a class named Signal that will hold digitized acceleration data....
Java program Create a constructor for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp data The constructor...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT