0

I am trying to use two int variables from other classes in another class and then add them together into another variable and print the result. When I try this though, I always get a result of zero like the values are not being brought over into the new class and I can't figure out what the problem is. Here is some example code:

class1

public static int finished = (match2.totalpoints + match3.iq);
    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        Scanner s = new Scanner(System.in);
        System.out.println("THIS IS YOUR OWN EXCLUSIVE IQ TEST OR MEMORY QUIZ OR WHATEVER....");
        System.out.println("");
        System.out.println("When taking the quiztest you have only two seconds before making each guess");
        match2 m = new match2();
        System.out.println("THAT MEANS ACCORDING TO YOUR QUIZTEST YOU'VE GOT AN IQ OF " + finished + " POINTS");
        }
}

EDIT: class2

 public class match2 {

    public static int totalpoints;
    int a;
    int b;
    int c;
    int d;
    int e;
    int f;
    String guess;
    String group;

    String countdown[] = {
    "3...",
    "2...",
    "1...",
    ""
    };

    String memorize[] = {
    ""
    };
public match2() throws InterruptedException
    {
        int x = set2();
        int y = set3();
        int z = set4();
        total(x, y, z);
        System.out.println("For the next part of your IQ ASSesment\njust type back the words in CAPSLOCK in CAPSLOCK");
        System.out.println("");
        match3 n = new match3();            
    }

public void set1() throws InterruptedException 
    {
        //Scanner s = new Scanner(System.in);
        for (int i = 0; i < countdown.length; i++) 
            {
                Thread.sleep(750);
                System.out.println(countdown[i]); 
            }
    }

public int set2() throws InterruptedException
    {
        Random r = new Random();
        Scanner s = new Scanner(System.in);
        System.out.println("press ENTER for your first set...");
        s.nextLine();
        set1(); 

        int rv = 0;
        a = r.nextInt(9) + 1;
        b = r.nextInt(9) + 1;
        c = r.nextInt(9) + 1;
        d = r.nextInt(9) + 1;
        group = "" + a  + b + c + d;
        System.out.println(group);
        for (int i = 0; i < memorize.length; i++) 
        {
            Thread.sleep(1500);
            System.out.println(memorize[i]); 
        }
        System.out.println("\n\n\n\n\n\n\n\n\n\n");
        guess = "" + s.nextLine();
        if(guess.equals(group))
        {
            System.out.println("nice +1 bruh");
            rv = 1;
        }
        else if(!guess.equals(group))
        {
            System.out.println("almost");
        }
        return rv;
    }


public int set3() throws InterruptedException
    {
        Random r = new Random();
        Scanner s = new Scanner(System.in);
        System.out.println("");
        System.out.println("press ENTER for your next set...");
        s.nextLine();
        set1(); 

        int rv = 0;
        a = r.nextInt(9) + 1;
        b = r.nextInt(9) + 1;
        c = r.nextInt(9) + 1;
        d = r.nextInt(9) + 1;
        f = r.nextInt(9) + 1;
        group = "" + a  + b + c + d + f;  
        System.out.println(group);
        for (int i = 0; i < memorize.length; i++) 
        {
            Thread.sleep(1500);
            System.out.println(memorize[i]); 
        }
        System.out.println("\n\n\n\n\n\n\n\n\n\n");
        guess = s.nextLine();      
        if(group.equals(guess))     
        {
            rv = 1;
            System.out.println("good");
        }
        else if(!guess.equals(group))
        {
            System.out.println("almost");
        }
        return rv;
    }

public int set4() throws InterruptedException
{
    Random r = new Random();
    Scanner s = new Scanner(System.in);
    System.out.println("");
    System.out.println("press ENTER for your final set...");
    s.nextLine();
    set1(); 

    int rv = 0;
    a = r.nextInt(9) + 1;
    b = r.nextInt(9) + 1;
    c = r.nextInt(9) + 1;
    d = r.nextInt(9) + 1;
    e = r.nextInt(9) + 1;
    f = r.nextInt(9) + 1;
    group = "" + a  + b + c + d + f + e;
    System.out.println(group);
    System.out.println("");
    for (int i = 0; i < memorize.length; i++) 
    {
        Thread.sleep(1500);
        System.out.println(memorize[i]); 
    }
    System.out.println("\n\n\n\n\n\n\n\n\n\n");
    guess = "" + s.nextLine();
    if(group.equals(guess))
        {
            rv = 1;
            System.out.println("great");
        }
    else if(!group.equals(guess))
        {
        System.out.println("eeeh buzer sound");
        }
    return rv;
}

public int total(int x, int y, int z)
    {
    System.out.println("");
    int totalpoints = (x + y + z);
    if(totalpoints == 3)
    {
        System.out.println("YOU GOT THEM ALL");
    }
    if(totalpoints <= 2 && totalpoints >= 1)
    {
        System.out.println("YOU MISSED A TOTAL OF  " + (3 - totalpoints));
    }
    if(totalpoints == 0)
    {
        System.out.println("HA! YOU MISSED THEM ALL");
    }
    return totalpoints;
}

}

EDIT: class3

public class match3 {
    public static int iq;

    String countupdown [] = {
            "READY...", 
            "SET.....",
            ""
            };
    String memorize [] = {
            ""
            };

public match3() throws InterruptedException
    {
        int mem1 = memory1();
        int mem2 = memory2();
        int mem3 = memory3();
        totalMemory(mem1, mem2, mem3);
    }

public void methodCountdown() throws InterruptedException
    {
    for(int i = 0; i < countupdown.length; i++)
        {
            Thread.sleep(1000);
            System.out.println(countupdown[i]);
        }

    }

public int memory1() throws InterruptedException
    {
        int rv = 1;
        Scanner s = new Scanner(System.in);
        System.out.println("Press ENTER when ready");
        s.nextLine();
        methodCountdown();
        String a = word1();
        String b = word2();
        System.out.println("The " + a + " ate the " + b);
        String wordgroup = "" + a + " " + b;
        for (int i = 0; i < memorize.length; i++) 
        {
            Thread.sleep(1500);
            System.out.println(memorize[i]); 
        }
        System.out.println("\n\n\n\n\n\n\n\n\n\n");
        String wordguess = "" + s.nextLine(); 
        if(wordgroup.equals(wordguess))
            {
                System.out.println("awesome cock muncher a match");
                rv = 1;
            }
        else if(!wordgroup.equals(wordguess))
            {
            System.out.println("nope");
            }
        return rv;
    }

public int memory2() throws InterruptedException
    {
        int rv = 0;
        Scanner s = new Scanner(System.in);
        System.out.println("");
        System.out.println("Press ENTER for your next set");
        s.nextLine();
        methodCountdown();
        String a = word1();
        String c = word3();
        System.out.println("The " + a + " drove the " + c);
        String wordgroup = "" + a + " " + c;
        for (int i = 0; i < memorize.length; i++) 
        {
            Thread.sleep(1500);
            System.out.println(memorize[i]); 
        }
        System.out.println("\n\n\n\n\n\n\n\n\n\n");
        String wordguess = "" + s.nextLine(); 
        if(wordgroup.equals(wordguess))
            {
                System.out.println("awesome cock muncher a match");
                rv = 1;
            }
        else if(!wordgroup.equals(wordguess))
            {
            System.out.println("nope");
            }
        return rv;
    }

public int memory3() throws InterruptedException
    {
        int rv = 0;
        Scanner s = new Scanner(System.in);
        System.out.println("");
        System.out.println("Press ENTER for your next set");
        s.nextLine();
        methodCountdown();
        String a = word1();
        String d = word4();
        System.out.println("The " + a + " visited the " + d);
        String wordgroup = "" + a + " " + d;
        for (int i = 0; i < memorize.length; i++) 
        {
            Thread.sleep(1500);
            System.out.println(memorize[i]); 
        }
        System.out.println("\n\n\n\n\n\n\n\n\n\n");
        String wordguess = "" + s.nextLine(); 
        if(wordgroup.equals(wordguess))
            {
                System.out.println("awesome cock muncher a match");
                rv = 1;
            }
        else if(!wordgroup.equals(wordguess))
            {
            System.out.println("nope");
            }
        return rv;
    }

public static String word1()
    {
        String word = "";
        Random r = new Random();
        int cv = r.nextInt(3) + 1;
        if(cv == 1)
        {
            word = "DOG";
        }
        else if(cv == 2)
        {
            word = "CAT";
        }
        else if(cv == 3)
        {
            word = "BIRD";
        }
    return word;
}

    public static String word2()
        {
            String word = "";
            Random r = new Random();
            int cv = r.nextInt(3) + 1;
            if(cv == 1)
            {
                word = "FOOD";
            }
            else if(cv == 2)
            {
                word = "MUD";
            }
            else if(cv == 3)
            {
                word = "GRAINS";
            }
            return word;
        }

    public static String word3()
        {
            String word = "";
            Random r = new Random();
            int cv = r.nextInt(3) + 1;
            if(cv == 1)
            {
                word = "TRAM";
            }
            else if(cv == 2)
            {
                word = "BUS";
            }
            else if(cv == 3)
            {
                word = "BICYCLE";
            }
        return word;
        }

    public static String word4()
        {
            String word = "";
            Random r = new Random();
            int cv = r.nextInt(3) + 1;
            if(cv == 1)
            {
                word = "MALL";
            }
            else if(cv == 2)
            {
                word = "PARK";
            }
            else if(cv == 3)
            {
                word = "POOL";
            }
        return word;
    }

public void totalMemory(int mem1, int mem2, int mem3)
    {   
        int iq = (mem1 + mem2 + mem3);
        System.out.println("");
        if(iq == 3)
            {
            System.out.println("YOU GOT THEM ALL");
            }
        else if(iq <= 2 || iq >= 1)
            {
            System.out.println("YOU MISSED  A TOTAL OF " + (3 - iq));
            }
        else if(iq == 0)
            {
            System.out.println("HA! YOU MISSED THEM ALL");
            }
    }

}

total points is a variable from match2 class and iq from match3 class. Any help with any methods I could use to make this happen would be much appreciated. Thank You

4
  • Read ericlippert.com/2014/03/05/how-to-debug-small-programs for tips to debug your code. Commented Oct 17, 2019 at 21:56
  • It would be helpful if you could post the code for the other classes as well. You should have getters in your match2 and match3 classes to access the variables you want to use in the main class. Commented Oct 17, 2019 at 21:58
  • We'll need a minimal reproducible example to help here. Commented Oct 17, 2019 at 22:11
  • I added the other classes I was using: match2 and match3. I tried some new stuff but I cacn't ever seem to get a value from the variables I am trying to bring over to match1 class Commented Oct 18, 2019 at 15:34

4 Answers 4

1

Well ... besides the fact you are not following any code convention, Like class names should start with a capital letter and public static final fields (like totalpoints and iq) should be all Uppercase (code conventions, you are not sharring match2 and match3 codes, without it we can't understand what is happening inside those classes.

But you can do a simple test and assign a value to match2.totalpoints and match3.iq and you are going to see the summing of these two values being printed by the last system.out you put.

good luck and good Java studies!

Sign up to request clarification or add additional context in comments.

Comments

0

Are the int variables you're trying to use inside child classes of your main parent class? Did you extend the child classes in your main parent class?

Class #1:

public int firstVar(int someNum) {
  //code here
  return someNum;
}

Class #2:

public int secondVar(int otherNum) {
  //code here
  return otherNum;
}

Class #3 Class with Main Method -

public class mainClass extends class#1; //etc
//code here and finally print out the finished number

You could try extending one of the classes that has an int you need into another one of the classes with the other int you need and then just extending that second class into your main, OR you could try completely redefining your classes and just placing all the ints you need into one separate class and then extending that single one into your main.

Comments

0

So I finally got it to work. The problem, I guess, was that I was trying to add together the variables from the other classes(match2 and match3) outside the main function within the class(match1) I was trying to add them together. All I did was move the expression adding the variables together from the top to inside the main function like this:

public static int finished;
    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        Scanner s = new Scanner(System.in);
        System.out.println("THIS IS YOUR OWN EXCLUSIVE IQ TEST OR MEMORY QUIZ OR WHATEVER....");
        System.out.println("");
        System.out.println("When taking the quiztest you have only two seconds before making each guess");
        match2 m = new match2();

        finished = (match2.totalpoints + match3.iq);
        System.out.println("THAT MEANS ACCORDING TO YOUR QUIZTEST YOU'VE GOT AN IQ OF " + finished + " POINTS");
        }

}

Thanks for all the help from everyone.

Comments

0

Smth. like this. You have lot's of code duplication

public class MatchRunner {

    public static void main(String... args) throws InterruptedException {
        new MatchRunner().start();
    }

    public void start() throws InterruptedException {
        System.out.println("THIS IS YOUR OWN EXCLUSIVE IQ TEST OR MEMORY QUIZ OR WHATEVER....");
        System.out.println();
        System.out.println("When taking the quiztest you have only two seconds before making each guess");

        int totalPoints = new Match2().getTotalPoints();

        System.out.println("For the next part of your IQ Assesment");
        System.out.println("just type back the words in CAPSLOCK in CAPSLOCK");

        int iq = new Match3().getIQ();

        System.out.println("THAT MEANS ACCORDING TO YOUR QUIZTEST YOU'VE GOT AN IQ OF " + (totalPoints + iq) + " POINTS");
    }
}

public class Match2 {

    public int getTotalPoints() throws InterruptedException {
        try (Scanner scan = new Scanner(System.in)) {
            int totalPoints = calc(scan, "first", "nice +1 try", "almost");
            totalPoints += calc(scan, "next", "good", "almost");
            totalPoints += calc(scan, "final", "great", "eeeh buzer sound");
            printTotalPoints(totalPoints);
            return totalPoints;
        }
    }

    private static void countdown() throws InterruptedException {
        for (int i = 5; i > 0; i--) {
            Thread.sleep(750);
            System.out.println(i + "...");
        }
    }

    private static int calc(Scanner scan, String strSet, String strEqual, String strNotEqual) throws InterruptedException {
        System.out.println("press ENTER for your " + strSet + " set...");

        Random random = new Random();
        scan.nextLine();
        countdown();

        int sum = 0;

        for (int i = 0; i < 4; i++)
            sum += random.nextInt(9) + 1;

        System.out.println(sum);

        for (int i = 0; i < 10; i++)
            System.out.println();

        int guess = scan.nextInt();
        System.out.println(guess == sum ? strEqual : strNotEqual);

        return guess == sum ? 1 : 0;
    }

    private static void printTotalPoints(int totalPoints) {
        System.out.println();

        if (totalPoints == 3)
            System.out.println("YOU GOT THEM ALL");
        else if (totalPoints <= 2 && totalPoints >= 1)
            System.out.println("YOU MISSED A TOTAL OF  " + (3 - totalPoints));
        else if (totalPoints == 0)
            System.out.println("HA! YOU MISSED THEM ALL");
    }

}

public class Match3 {

    public int getIQ() throws InterruptedException {
        try (Scanner scan = new Scanner(System.in)) {
            Random random = new Random();
            Supplier<String> getWord1 = () -> getWord(random, "DOG", "CAT", "BIRD");
            Supplier<String> getWord2 = () -> getWord(random, "FOOD", "MUD", "GRAINS");
            Supplier<String> getWord3 = () -> getWord(random, "TRAM", "BUS", "BICYCLE");
            Supplier<String> getWord4 = () -> getWord(random, "MALL", "PARK", "POOL");

            int iq = calc(scan, getWord1, getWord2, "ate the");
            iq += calc(scan, getWord1, getWord3, "drove the");
            iq += calc(scan, getWord1, getWord4, "visited the");
            printIq(iq);
            return iq;
        }
    }

    private static void countdown() throws InterruptedException {
        System.out.println("READY...");
        Thread.sleep(1000);
        System.out.println("SET...");
        Thread.sleep(1000);
    }

    public int calc(Scanner scan, Supplier<String> wordOne, Supplier<String> wordTwo, String strMsq) throws InterruptedException {
        System.out.println("Press ENTER when ready");
        scan.nextLine();

        countdown();

        String a = wordOne.get();
        String b = wordTwo.get();

        System.out.println("The " + a + ' ' + strMsq + ' ' + b);

        String wordGroup = a + ' ' + b;

        for (int i = 0; i <= 10; i++)
            System.out.println();

        String wordGuess = scan.nextLine();
        System.out.println(wordGroup.equals(wordGuess) ? "awesome cock muncher a match" : "nope");

        return wordGroup.equals(wordGuess) ? 1 : 0;
    }

    private static String getWord(Random random, String one, String two, String three) {
        int cv = random.nextInt(3) + 1;

        if (cv == 1)
            return one;
        if (cv == 2)
            return two;
        if (cv == 3)
            return three;
        return "";
    }

    public void printIq(int iq) {
        System.out.println();

        if (iq == 3)
            System.out.println("YOU GOT THEM ALL");
        else if (iq <= 2 || iq >= 1)
            System.out.println("YOU MISSED  A TOTAL OF " + (3 - iq));
        else if (iq == 0)
            System.out.println("HA! YOU MISSED THEM ALL");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.