Colin Koenig
Colin Koenig

Reputation: 65

How to pass a String value to another class in Java

Currently, I am running into a problem in my Java code. I am somewhat new to Java, so I would love it if you kept that in mind.

My problem is with passing a String value from one class to another.

Main Class:

private static void charSurvey() 
{
    characterSurvey cSObj = new characterSurvey();
    cSObj.survey();

    System.out.println();
}

Second:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;


public class characterSurvey 
{   
    public void survey(String character)
    {
        Scanner s = new Scanner(System.in);

        int smartChina = 0,smartAmerica = 0,dumbAmerica = 0;
        String answer;

        System.out.println("Are you good with girls?");
        System.out.println("y/n?");
        answer = s.nextLine();
        if(answer.equalsIgnoreCase("y"))
        {
            smartChina = smartChina - 3;
            smartAmerica = smartAmerica + 2;
            dumbAmerica = dumbAmerica + 4;
        }   
        //... 
        //ASKING SEVERAL OF ABOVE ^


        List<Integer> charSelect = new ArrayList<Integer>();
        charSelect.add(smartChina);
        charSelect.add(smartAmerica);
        charSelect.add(dumbAmerica);

        Collections.sort(charSelect);
        Collections.reverse(charSelect);

        int outcome = charSelect.get(0);

        if(smartChina == outcome)
        {
            character = "smartChina";
        }
        else if(smartAmerica == outcome)
        {
             character = "smartAmerica";
        }
        else if(dumbAmerica == outcome)
        {
            character = "dumbAmerica";
        }

        System.out.println(character);
        s.close();
        }
    }

When I call the first class I am trying to grab the value of the second.

Disclaimer* the strings in this class were not meant to harm anyone. It was a joke between myself and my roommate from China, thanks.

Upvotes: 1

Views: 22387

Answers (2)

Vince
Vince

Reputation: 15146

It seems as if you want to obtain the character in your main class after the survey has completed, so it can be printed out in the main method.

You can simply change your void survey method to a String survey method, allowing you to return a value when that method is called:

class CharacterSurvey {
    public String takeSurvey() {
        //ask questions, score points
        String character = null;

        if(firstPerson == outcome) {
            character = "First Person";
        }

        return character;
    }
}

Now, when you call this method, you can retrieve the value returned from it:

class Main {
    public static void main(String[] args) {
        CharacterSurvey survey = new CharacterSurvey();

        String character = survey.takeSurvey();
        System.out.println(character);
    }
}

Upvotes: 3

crystalfrost
crystalfrost

Reputation: 281

There are several mistakes here.

First off, in your main class as you write you call the method survey() on the CharacterSurvey object but the survey itself the way it is implemented needs a String parameter to work

public void survey(String character)

Also this method returns void. If you want somehow to grab a string out of that method you need to declare the method as

public String survey() {} 

this method returns a string now.

If i were to give a general idea, declare a String variable in the second class which will be manipulated inside the survey method and once the survey is declared as a String method return the value at the end inside the method.

By doing that you'll be able to receive the String value by calling the method on the characterSurvey object (and of course assign the value to a string variable or use it however).

Hope this helped

Upvotes: 1

Related Questions