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...
write JAVA program have a public class named GeometricShapes that has the main() method. In the...
write JAVA program have a public class named GeometricShapes that has the main() method. In the main() method the user needs to select if he want 2D shapes or 3D shape -if user select 2D user needs to select the shape type (Square, Circle, or Triangle) after selected shape the user needs to specify whether to find the Area or the Perimeter or to find side-length (radius for the circles), accordingly the needed constructor is used. (using Polymorphism principle) -if...
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...
Create a Java program. The class name for the program should be 'EncryptText'. In the main...
Create a Java program. The class name for the program should be 'EncryptText'. In the main method you should perform the following: You should read a string from the keyboard using the Scanner class object. You should then encrypt the text by reading each character from the string and adding 1 to the character resulting in a shift of the letter entered. You should output the string entered and the resulting encrypted string. Pseudo flowchart for additional code to be...
Java Class Create a class with a main method. Write code including a loop that will...
Java Class Create a class with a main method. Write code including a loop that will display the first n positive odd integers and compute and display their sum. Read the value for n from the user and display the result to the screen.
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...
Write a complete java program that Define a class named sample containing: A method named division...
Write a complete java program that Define a class named sample containing: A method named division that receives two integers x and y and returns the division of the two numbers. It must handle any possible exceptions. A method named printArray that receives an array of double arr and an integer index and prints the element in the positon index. It must handle any possible exceptions. main method that recievs input from user and repeat if wrong input. Then it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT