Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to do a summation puzzle, the questions asks to use summation puzzles by enumerating and testing all possible configurations and then it says use it to solve the examples given. The examples given were

pot + pan = bib

dog+cat= pig

boy + girl = baby

I keep getting an error saying left hand side of assignment must be a variable

charSet.charAt(setIndex++) = stringTwo.charAt(loop);

cannot convert from int to bool.

if (exists = 0)

Also in my code where I try to display the output it doesn't run.

import java.util.Scanner;
public class Recursion
{
    // Example program


    public static String stringOne = new String(new char[10]);
    public static String stringTwo = new String(new char[10]);
    public static String stringThree = new String(new char[11]);
    public static String charSet = new String(new char[11]);
    public static int numberOne;
    public static int numberTwo;
    public static int numberThree;

    public static int maxCharCount;

    public static int[] numberSet = new int[10];

    public static void checkForEquality()
    {

    numberOne = numberTwo = numberThree = 0;
    int loop;
    int subloop;


        for (loop = 0; loop < stringOne.length(); loop++)

        {
            for (subloop = 0; subloop < maxCharCount; subloop++)

            {
            if (stringOne.charAt(loop) == charSet.charAt(subloop))

            {
            if (loop == 0 && numberSet[subloop] == 0)

        return;

        //generate the number   

        numberOne = (numberOne * 10) + numberSet[subloop];
            }
            }

        }
        for (loop = 0; loop < stringOne.length(); loop++)

        {
            for (subloop = 0; subloop < stringTwo.length(); subloop++)

            {
            if (stringTwo.charAt(loop) == charSet.charAt(subloop))

            {
            if (loop == 0 && numberSet[subloop] == 0)

        return;

        //generate the numeber  

        numberTwo = (numberTwo * 10) + numberSet[subloop];
            }
            }
        }


        for (loop = 0; loop < stringThree.length(); loop++)

        {
            for (subloop = 0; subloop < maxCharCount; subloop++)

            {
            if (stringThree.charAt(loop) == charSet.charAt(subloop))

            {
                if (loop == 0 && numberSet[subloop] == 0)

        return;

        //generate the number   

        numberThree = (numberThree * 10) + numberSet[subloop];
            }
            }
        }

        if (numberOne + numberTwo == numberThree)
        {
        //display the output 

        System.out.print("  Summation Puzzle solved.    ");

        System.out.print("
");
        System.out.print(stringOne);
        System.out.print("<==>");
        System.out.print(numberOne);
        System.out.print("
");
        System.out.print(stringTwo);
        System.out.print("<==>");
        System.out.print(numberTwo);
        System.out.print("
");
        System.out.print(stringThree);
        System.out.print("<==>");
        System.out.print(numberThree);
        System.out.print("
");

    //loop to show the result

    for (loop = 0; loop < maxCharCount; loop++)
    {
        System.out.print(charSet.charAt(loop));
        System.out.print("<==>");
        System.out.print(numberSet[loop]);
        System.out.print("
");
    }

    System.exit(0);

        }
    }


        public static void generateCombinations(int indexCounter, int[] availableSet)

        {
            int loop;
            if (indexCounter != 0)
            {

        for (loop = 0; loop < 10; loop++)
        {

        numberSet[indexCounter] = loop;

        if (availableSet[loop] == 1)
        {

        availableSet[loop] = 0;

        generateCombinations(indexCounter + 1, availableSet);

        availableSet[loop] = 1;
        }
        }
            }

        if (indexCounter == maxCharCount)

        {
    checkForEquality();
        }

        }

        public static void createCharSet()
        {

        int loop;
        int setIndex; 
        int exists;
        int subloop;

        setIndex = 0;

        for (loop = 0; loop < stringOne.length(); loop++)
        {
            exists = 0;

        for (subloop = 0; subloop < setIndex; subloop++)

        {

        if (stringOne.charAt(loop) == charSet.charAt(subloop))
        {
        exists = 1;
        }

        }
        if (exists == 0)

        {
            charSet = StringFunctions.changeCharacter(charSet, setIndex++, stringOne.charAt(loop));
        }
        }

            for (loop = 0; loop < stringTwo.length(); loop++)
            {
        exists = 0;

        for (subloop = 0; subloop < setIndex; subloop++)

        {

        if (stringTwo.charAt(loop) == charSet.charAt(subloop))

        {
        exists = 1;
        }

        }

        if (exists == 0)
        {

             charSet = StringFunctions.changeCharacter(charSet, setIndex++, stringTwo.charAt(loop));

        }
            }

        for (loop = 0; loop < stringThree.length(); loop++)

        {
            exists = 0;

        for (subloop = 0; subloop < setIndex; subloop++)

        {

            if (stringThree.charAt(loop) == charSet.charAt(subloop))
            {
        exists = 1;
            }
        }

            if (exists == 0)
            {


            charSet = StringFunctions.changeCharacter(charSet, setIndex++, stringThree.charAt(loop));
            }
        }

            maxCharCount = setIndex;
        }

            public static void calculateSummation()

            {
            int loop;

        if (maxCharCount > 10)

        {

        System.out.print("Please check the input again");

        return;
        }
            else

            {

            int[] avaliableSet = new int[10];

        for (loop = 0; loop < 10; loop++)
        {

            avaliableSet[loop] = 1;
        }

            generateCombinations(0, avaliableSet);
            }
            }

     public static void main(String[]args)
            {
                Scanner scan = new Scanner(System.in);

            System.out.print(" Enter the first String :");
            stringOne = scan.next();


            System.out.print("  Enter the second String :");
            stringTwo = scan.next();

            System.out.print("  Enter the thirsd String :");
            stringThree = scan.next();

        createCharSet();



        System.out.print("  The character set formed from the given string = ");
        System.out.print(charSet);

        calculateSummation();
        checkForEquality();
            }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
233 views
Welcome To Ask or Share your Answers For Others

1 Answer

A lot of your problems are stemming from the syntax errors in the code you've written. For example:

  1. line 74: if (stringThree.charAt(loop) == charSet.charAt(subloop) != null)

    • charSet.charAt(subloop) != null is an invalid comparison since the != operator cannot be used for booleans when comparing to null. If you're trying to determine if the characters return from .charAt(var) exist, use parentheses to make independent comparisons of each object.charAt(var) to null.
  2. line 183: charSet = tangible.StringFunctions.changeCharacter(charSet, setIndex++, stringOne.charAt(loop));

    • tangible is ironically not tangible, as the variable does not exist locally or has not been defined globally.
  3. charSet.charAt(setIndex++) = stringTwo.charAt(loop);

    • charSet.charAt(setIndex++) is a method that returns a character. This does not mean you can set the character at the specified index like it's a variable.
  4. line 227: if (exists = 0)

    • You must use == when conducting comparisons in a conditional.
  5. line 269: Scanner scan = new Scanner(System.in);

    • The Scanner class was not imported and thus cannot be used.
  6. line 283: charSet.charAt(maxCharCount) = '';

    • Again, you can't use .charAt(var) to set the character at that index like it's a variable.

All of these problems can be self-determined by using a proper IDE, such as Eclipse.

Edit: Try to spend a little more time with pencil and paper working out the logic of your program before writing the code to represent your algorithm. This way you have focus and can write more comprehensive, commented, cleaner code. Here is a bit of a guide to help condense your existing project.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...