-1

I have a program that takes letters for input and then sums the numeric value of each letter.

I have it so that if I input "abc", my output is "6".

I ignore uppercase letters, so if I input "abC", my output is "3".

What I want to do now, is in a separate class, make a method, which if set to true will run my main program as is, but when it is set to false, it will treat uppercase letters as lowercase, giving an input of "abC", an output of "6".

I hope this makes sense, I've tried a few different things but they all run the programm as is, ignoring uppercase.

Here is my code, I appreciate any constructive feedback.

Thanks

EDIT: I would also appreciate if you didn't downvote me for asking a question, if you don't want to help dont', seems every question I asked gets downvoted for no obvious or fair reason. I didn't want to ask for help since I knew this would happen. We all start have to somewhere!

Main method:

    Scanner scan = new Scanner(System.in);

    System.out.println("\nPlease enter the letters you would like to use:");

    String s, t = "";

    scan.next();

    s = scan.next();

    boolean b = Converter.caseSensitive(false, s);

    scan.close();

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

        if (!t.isEmpty()) {
            t += ",";
        }


        if (ch >= 'a' && ch <= 'z') {
            int n = ch - 'a' + 1;
            t += String.valueOf(n);
        }
    }

Second method in separate class:

public class Converter {

public static boolean caseSensitive(Boolean b, String s) {

    for (char c : s.toCharArray()) {
        if (Character.isLetter(c) && Character.isLowerCase(c)) {
            b = s.equalsIgnoreCase(s);
            return false;
        }
    }

    s = s.toLowerCase();
    return true;
}
}
Keeron
  • 35
  • 1
  • 10
  • Could you please cleanse your code from the unnecessary lines? – Anton Hlinisty Mar 18 '17 at 22:33
  • 1
    Why don't you move all your code from main() to some other function like pass(boolean option), then just replace boolean b = StringConverter.caseSensitive(false, s); with boolean b = StringConverter.caseSensitive(option, s); and call pass(true); pass(false); from new main? – Mikhail Antonov Mar 18 '17 at 22:34
  • I don't actually see a question in here. Please clarify where precisely you are having issues. – Joe C Mar 18 '17 at 22:37
  • @AntonHlinisty what lines are unnecessary exactly, the comments? I kept them to give people a better idea as to what it is I'm doing. – Keeron Mar 18 '17 at 22:41
  • @JoeC I want to be able to put in either "true" or "false in the line, " boolean b = StringConverter.caseSensitive(false, s);". This will then call my method in the Converter class. If set to false I want to convert all input to lowercase, if set to true it will basically do nothing. – Keeron Mar 18 '17 at 22:43
  • 3
    Welcome to SO. I've formatted your code to something like what would be expected on this site. However note that you're expected to cut out everything that isn't required to explain your question to make it easier for us to help. In your case you could have cut out everything other than the line asking for the boolean from another class. – sprinter Mar 18 '17 at 22:43
  • @sprinter Thanks for that, I'm just of the midset that more information is better, will keep it in mind next time. – Keeron Mar 18 '17 at 22:50
  • 2
    No you'll need to get used to providing just the right amount of information. That means doing extra work to provide a simple example of your problem but you will get much better quality answers. Also, 9 times out of 10 the process of finding an example will clarify the question in your mind and you'll find the answer is already available on SO and you won't need to ask at all. See https://stackoverflow.com/help/how-to-ask for a good overview of the principles. – sprinter Mar 18 '17 at 22:55

3 Answers3

1

I believe your question is "how do I record a static boolean value in a class and then request it from another class?"

public class Configuration {
    private static boolean convertToUppercase = true;

    public static void setConvertToUppercase(boolean convert) {
        convertToUppercase = convert;
    }

    public static boolean getConvertToUppercase() {
        return convertToUppercase;
    }
}

This can be used as:

StringConverter.caseSensitive(Configuration.getConvertToUppercase(), input);

Note that most coders (me included) would consider this poor design but explaining why is outside the scope of your question.

There are a lot of other issues with your code. For example your method call above will leave the input string unchanged. But I suggest you ask another question with just the relevant code when you get stuck.

sprinter
  • 27,148
  • 6
  • 47
  • 78
  • I believe you are right. Thanks for the advice, I know my code is far from elegant, I am only starting so, for now, if it works I am happy. I will try a few more ideas to see how I get on, I appreciate your help. – Keeron Mar 18 '17 at 22:57
1

String is immutable in Java. Please read following stackoverflow question for more information about this topic: String is immutable. What exactly is the meaning?

public static void main(String[] args) throws Exception
{
    String test = "abc";
    toUpperCase(test);
    System.out.println(test);
}

private static void toUpperCase(String test)
{
    test = test.toUpperCase();
}

Please note that above code will output:

abc

In order to have "ABC" as result you need to use following code:

public static void main(String[] args) throws Exception
{
    String test = "abc";
    test = toUpperCase(test);
    System.out.println(test);
}

private static String toUpperCase(String test)
{
    return test.toUpperCase();
}

This one outputs:

ABC

So your Converter.caseSensitive method should return String.

Community
  • 1
  • 1
JCasso
  • 5,423
  • 2
  • 28
  • 42
1

I don't think you really need the Converter class. You can delete class and replace the line:

boolean b = Converter.caseSensitive(false, s);

with this

boolean shouldCountUppercaseLetters = false;

if (shouldCountUppercaseLetters) {
    s = s.toLowerCase();
}
Andree
  • 3,033
  • 6
  • 36
  • 56
  • Hi Andree, I definitely don't need it, I just want to see how it could be done using it. What can I say, I like making things awkwards for myself :) – Keeron Mar 18 '17 at 23:08