0

I need to write a program where the main reads two strings as input: if the strings have the same length, then it has to pass the whole first string and the first char of the second string to a method called find, which has to return 'true' if the character appears even a single time on the string. If the length differs, then it will pass the whole second sentence and the last char of the first string to find. At last, the main will give whatever the method returns as output, so it has to be true, or false. I've created the whole main, and it works correctly, but I have no idea how to create the find method. Here is the code:

import java.util.Scanner;

public class Exercise {
    /*
     * public static boolean find(String... sentence, char... character) {
     *     // No, I can't use multiple varargs...
     * }
     */

    public static void main(String[] args) {
        String first, second;
        char firstChar, lastChar;
        Scanner keyboard = new Scanner(System.in);
        int lengthFirst, lengthSecond;
        boolean goal = true;

        first = keyboard.nextLine();
        lengthFirst = first.length();
        lastChar = first.charAt(lengthFirst - 1);

        second = keyboard.nextLine();
        lengthSecond = second.length();
        firstChar = second.charAt(0);

        System.out.println("Length 1: " + lengthFirst); // Those lines are test lines.
        System.out.println("Length 2: " + lengthSecond); // They're here just to check
        System.out.println("Char 1: " + firstChar); // if everything else works.
        System.out.println("Char 2: " + lastChar);

        if (lengthFirst == lengthSecond) {
            goal = find(first, firstChar);
            System.out.println("Goal is: " + goal);
            System.exit(0);
        } else
            goal = find(second, lastChar);
        System.out.println("Goal is: " + goal);
        System.exit(0);
    }
}

I was thinking about using the varargs option, using a varargs for the String, and another for the char, and then using a 'for' loop inside of the method to check if the character appears or not, and everything was easy on my head...but with some research I found out it will be a waste of time, because I can't use two varargs on the same method. The for loop idea works, but I can't figure out how to pass only the right String and the right Char. How should I pass them to the method, without passing them both?

Edit: No, this is not a duplicate. I allow loops, the other question doesn't. Also, my problem is about how am I supposed to pass multiple variables, but then using just some. That's an example:

The strings are both long 50, so the method needs to use only 'first' as String, and 'firstChar' as Char.

M. Rossi
  • 3
  • 1
  • 4
  • 1
    possible duplicate of [How can I check if a single character appears in a string?](http://stackoverflow.com/questions/506105/how-can-i-check-if-a-single-character-appears-in-a-string) – ImGone98 Aug 04 '15 at 16:29
  • No, it isn't. That question doesn't even allow the loops, and I talk about a 'for' loop, so no, totally unrelated. I'm talking about how to pass multiple variables to a method, but then using just some, and I can't solve it with varargs because I can't use multiple varargs. – M. Rossi Aug 04 '15 at 16:52
  • why would you pass the variables which you are not going to use ? – Archit Garg Aug 04 '15 at 17:09
  • Because there are times where I need to pass the first couple, and there are times where I need to pass the other couple. Not everytime both sentences are long 50, for example. If they are, let's pass the first couple. If the first is long 50, and the second is long 51, let's pass the second couple. – M. Rossi Aug 04 '15 at 17:17
  • but by implementing if and else you have already taken care of what you need to pass parameters to your function... so the function will be common for both cases ie if both sentences are long 50 then first couple would be passed else the second couple of parameters would be passed to the same function – Archit Garg Aug 04 '15 at 17:23

4 Answers4

0
private static boolean find(String str, char Char) {
        for(int i=0;i<str.length();i++)
            if(str.charAt(i)==Char)
                return true;
        return false;
    }

This would help hopefully...

Archit Garg
  • 1,012
  • 8
  • 16
  • Thanks for writing down the for loop for me, but I need to find a solution to pass JUST the right String and the right char! :) – M. Rossi Aug 04 '15 at 16:52
  • Uh? How? I can't write find(String first, String second, char firstChar, char lastChar). It would pass four variables, and I should pass only 'first' and 'firstChar', or 'second' and 'lastChar'. Of course the 'for' loop works, but the question is not about that. – M. Rossi Aug 04 '15 at 16:57
  • whether it be the first string and first char or second string and last char ... what you want to implement is that if the char is present in string the function shud return true or false accordingly .... it does exactly that – Archit Garg Aug 04 '15 at 16:59
  • Nope. I don't want to implement that. I do know, how to implement that. What I do not know is "How can I pass a specific String, and a specific Char, when I have multiple Strings and Chars?" – M. Rossi Aug 04 '15 at 17:05
0

Find will simply contain

private boolean find(String subject, char first) {
    return subject.indexOf(first) > -1;
}
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
0

You can use String.indexOf().

returns the index within this string of the first occurrence of the specified character. If a character with value ch occurs in the character sequence represented by this String object, then the index (in Unicode code units) of the first such occurrence is returned, if no such character occurs in this string, then -1 is returned.

 public static boolean find(String str, char ch){
        if(str.indexOf(ch) == -1)
            return false;
        return true;
  } 

As you are thinking, you don't need four parameters for this function. You can use this function with two parameters for both cases:

  goal = find(first, firstChar);
  goal = find(second, lastChar);

EDIT I think you have misunderstood the way the parameters are mapped.

if you have a function like

      public static boolean find(String str, char ch){
              //do something
      }

You don't need to call the find with same parameters str and ch, I mean find(str,ch). You can call them with any parameter, with any name, like :

        goal = find(s,c); // s is a string and c is a char

        goal = find(a,b); // a is a string and b is a char

when you call find(s,c), s will be mapped to the first argument in your function that is str and c will be mapped to your second argument that is ch.

This is the reason you are able to call both find(first, firstChar) and find(second, lastChar) with a single function.

Community
  • 1
  • 1
Karthik
  • 4,950
  • 6
  • 35
  • 65
  • Or in short `return str.indexOf(ch) != -1;`. – Tom Aug 04 '15 at 16:40
  • 1
    @Tom this is a usual problem/case with me :P, I somehow don't like to return computed values in the same line. – Karthik Aug 04 '15 at 16:45
  • Yeah, that can work too, but then i should write find(String first, String second, char firstChar, char lastChar), and that means passing 4 variables, and I should pass only a single String and a single char. I've edited the question for a better explanation. – M. Rossi Aug 04 '15 at 16:50
  • @M.Rossi `if the strings have the same length, then it has to pass the whole first string and the first char of the second string to a method called find, which has to return 'true' if the character appears even a single time on the string. If the length differs, then it will pass the whole second sentence and the last char of the first string to find` according to this you will always pass one `String` and one `char`. Why do you have 4 parameters in method? – Karthik Aug 04 '15 at 16:59
  • @karthik *"I somehow don't like to return computed values in the same line"* And I prefer the style I've posted :D. Luckily everyone can has his style of coding and there is nothing wrong with both approaches. – Tom Aug 04 '15 at 17:01
  • @karthik Because you need to write every parameter you pass, I guess. Maybe I'm wrong, but I learned that if you want to pass a specific parameter to a method, you need to write inside of the brackets the type and the name of the parameter. I can't know which parameter should be passed BEFORE iterating the program. Until the user writes down the strings, the program can't know which one to pass. – M. Rossi Aug 04 '15 at 17:03
  • @M.Rossi what you have written in your main method is correct. You can just use this function for both `goal = find(first, firstChar);` & `goal=find(second, lastChar);` . You will pass only those parameters which are required, here you need only two parameters, a `String` and a `char`. – Karthik Aug 04 '15 at 17:07
  • Yeah, but I totally don't know the code for the method. I need the code for the method, like 'public static boolean find(WhatShouldIWriteHere?) { //a for-loop }. – M. Rossi Aug 04 '15 at 17:11
  • @M.Rossi you don't need a for loop in this case, I edited my answer. – Karthik Aug 04 '15 at 17:16
  • I've tried your code, and the compiler gets no error, but whatever the length of the sentences, it only gives me 'true'. It's always a big step ahead, thank you! Can I ask you whythe compiler gets no error, tho? I'm not totally sure about! – M. Rossi Aug 04 '15 at 17:21
  • @M.Rossi why do you think it should show an error? :o – Karthik Aug 04 '15 at 17:23
  • ...sorry. Lack of coffee, that's why. I'm sorry. Tho, if you can explain me how it works, I will really appreciate the effort. Oh, and thanks once again! – M. Rossi Aug 04 '15 at 17:24
0

Seems like what you are looking for is:

if (second.indexOf(c) == -1) return false; //char c not found
return true;
kg_sYy
  • 1,127
  • 9
  • 26
  • Or in short `return str.indexOf(ch) != -1;`. – Tom Aug 04 '15 at 16:40
  • True, you could even put that inside the if statement if you only need it in one place. No real need for separate method in that case.. – kg_sYy Aug 04 '15 at 16:42
  • I know I can put inside of the main, but I'm doing this as exercise, so I need to put it out on a method. The problem is when I need to pass String and char variables. I can't pass all of the four variables, I can only pass a single String, and a single char. Then work on them on the method. How can I? I can't use multiple varargs, and that's the only option I could think of. – M. Rossi Aug 04 '15 at 16:55
  • @kg_sYy A dedicated method has the advantage of the method name. The main method could be more readable with a proper method name. For example, `if(hasChar(str, ch)) {..}` looks better than `if(str.indexOf(ch) != -1) {..}`. But this is just an opinion and not a rule :D. – Tom Aug 04 '15 at 17:00