-2
\$\begingroup\$

Write a program that manipulates two strings. The program inputs two strings (string1 and string2) and a character (char1) and an integer (position) to represent a character position within a string. The program will display the following:

  1. Whether string1 is less, greater or equal to string2
  2. string1 in upper case
  3. string2 in lower case
  4. The number of characters in string1
  5. The first character in string2
  6. The number of occurrences that char1 is contained in string1. Hint: use a for loop and charAt
  7. The character that is in the position of string1. Turn in a run of the program that uses your first and last names as the strings. Use at least two methods with the following headers:

int countOccurrences(String s, char c) // to answer #6 char

showChar(String s, int pos) // to answer #7

import java.util.Scanner; public class Hwk5A { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter your First Name: "); String string1 = keyboard.nextLine(); System.out.print("Enter your Last Name: "); String string2 = keyboard.nextLine(); System.out.print("Enter a character: "); char char1 = keyboard.next().charAt(0); System.out.print("Enter an number: "); int position = keyboard.nextInt(); //Question 1 if(string1.compareTo(string2)>0){ System.out.println("First name is greater than Last name"); } else if (string1.compareTo(string2)==0) { System.out.println("First name is equal to Last Name"); } else { System.out.println("First name is less than Last name"); } //Question #2: System.out.println("\nstring1 uppercase:" + string1.toUpperCase()); //Question #3: System.out.println("string2 lowercase:" + string2.toLowerCase()); //Question #4: System.out.println("number of characters in string1: " + string1.length()); //Question #5: System.out.println("first character in string2: " + string2.charAt(0)); //Question #6: System.out.println("# of occurrences that char1 is contained for string1: " + countOccurrences(string1, char1)); //Question #7: System.out.println("the character in string 1 from inputted position # is: " + showChar(string1, position)); } public static int countOccurrences(String s, char c) { int countOccurrences = 0; int totalOccurrences = 0; for(int i = 0; i <= s.length()-1; i++){ if(s.charAt(i) == c) { countOccurrences = 1; totalOccurrences += countOccurrences; } } return totalOccurrences; } public static char showChar(String s, int pos) { return s.charAt(pos); } } 
\$\endgroup\$
1
  • 3
    \$\begingroup\$where is your doubt or problem you are facing?\$\endgroup\$
    – aakansha
    CommentedJul 22, 2015 at 3:36

2 Answers 2

1
\$\begingroup\$

In your countOccurences method, you don't need 2 variables:

public static int countOccurrences(String s, char c) { int nbOccurence = 0 for(int i = 0; i <= s.length()-1; i++){ if(s.charAt(i) == c) { nbOccurence += 1; } } return nbOccurence; } 

and although the fact that I advocate encapsulation for a better comprehension, your method showChar() is far from being needed :) But if you want to go this way, you should encapsulate your code for question 1 too.

\$\endgroup\$
    1
    \$\begingroup\$
     if(string1.compareTo(string2)>0){ System.out.println("First name is greater than Last name"); } else if (string1.compareTo(string2)==0) { 

    If you're using the same value multiple times, you'd be better off storing it and using the variable.

     int comparison = string1.compareTo(string2); if (comparison > 0) { System.out.println("First name is greater than Last name"); } else if (comparison == 0) { 

    This is also a good place to use a method so you can say something like

     System.out.println(generateComparisonStatement(firstName, lastName)); 

    In fact, it's a much better place to use a method than the proposed showChar.

    Note that I also changed from string1 and string2 to firstName and lastName. These names better describe what the variables hold than generic numbered names.

     } else { 

    I'm not a big fan of the half-cuddled else, but if you're going to do it, please avoid putting any vertical whitespace into it. Think of the closing of the previous block and the opening of the new as one unit. Any of the following are valid

     } else { } else { } else { 

    They should never vary from that, as it leads to confusion. For example, the first time that I read the program I missed the less than assertion altogether.

     for(int i = 0; i <= s.length()-1; i++){ 

    It would save a subtraction to say

     for (int i = 0; i < s.length(); i++) { 

    And personally, I'd prefer something like

     for (char current : s.toCharArray()) { if (current == c) { ++totalOccurrences; 

    That way you don't have to fool with an index variable or dereferencing a character position at all.

     int location = s.indexOf((int)c); while (-1 != location) { ++totalOccurrences; location = s.indexOf((int)c, location+1); } 

    Another alternative. Not sure if that is better than the for each form, although I find both alternatives to be more idiomatic Java than the original C-ish version.

    I don't like either of the method signatures, but of course they were outside your control.

    \$\endgroup\$

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.