Can't see me
Can't see me

Reputation: 501

Return two Strings from method

I'm a beginner in Java programming, and I'm trying to make a voting machine program, where you can vote for Republicans or Democrats. My question is, how can I edit my method so I would be able to return two strings with two distinct values?

For example, look at my code all the way in the bottom. It's wrong, but I wanted the tester to be able to print out Democrats: (some number) and Republicans: (some number) in one method. How can I do that?

import java.lang.String;

public class VotingMachine1 {
    private double Democrats;
    private double Republicans;

    public VotingMachine1() {
        Democrats = 0;
        Republicans = 0;
    }

    public void voteRepublican() {
        Republicans = Republicans + 1;
    }

    public void voteDemocrat() {
        Democrats = Democrats + 1;
    }

    public void clearMachineState() {
        Republicans = 0;
        Democrats = 0;
    }

    //this is where I'm having difficulties. I know its wrong
    public double getTallies() {
        System.out.println("Democrats: ", return Democrats);
        System.out.println("Republicans: ", return Republicans);
    }
}

Upvotes: 3

Views: 1456

Answers (8)

Jaimin MosLake
Jaimin MosLake

Reputation: 675

you can return an array with [0] and [1] as key and devide it on the basis of your need..

like

returnArray[0]="first string"; returnArray[1]="second string";

and use it ur way...

Upvotes: 0

Mario Rossi
Mario Rossi

Reputation: 7799

A few more random comments:

  • import java.lang.String; is superfluous as all classes in package java.lang are automatically imported in every Java source file.
  • Votes can not be fractional. People can't vote 0.75 candidate A, and 0.25 candidate B. If you use integer datatypes (int or long), you will be reflecting this fact better. Also, you will be saving yourself a lot of headache when you start obtaining results like 379857.999999. This is because floating point types have a better range, but worse precision (especially noticeable when working with pure integers).
  • According to Java usual naming conventions, variable names should start with a lowecase letter.
  • A better name for function getTallies is printTallies.
  • For output purposes, it's much better to use string formatting than concatenation. Some advantages are: multiple formats supported, ease of use, and internationalization.

Putting all together:

private int democratVotes;  
private int republicanVotes;

public void printTallies() {
    System.out.format("Democrats: %,d%n",democratVotes);
    System.out.format("Republicans: %,d%n",republicanVotes);
}

In this particular case, votes will be printed with thousand separation (ex: 3,345,623 instead of 3345623). Check Java's Formatting Numeric Print Output tutorial.

Thinking better about it, there are some alternatives where getTallies would effectively be returning some form of value:

1) Make it to return a String with both tallies. It would be hard and inefficient to separate the tallies later, though.

public String getTallies() {
    return "Democrats: %,d votes.  Republicans: %,d votes.%n".format(democratVotes,republicanVotes);
}

2) Make it to return an array.

public int[] getTallies() {
    return new int[2]{ democratVotes, republicanVotes };
}
public int[] getTallies1() {  // Same as getTallies, but written step by step.
    int[] result= new int[2] ;
    result[0]= democratVotes ;
    result[1]= republicanVotes ;
    return result ;
}

3) Make it to return a class.

public VotingMachineResults getTallies() {
    return VotingMachineResults(democratVotes,republicanVotes) ;
}

public static class VotingMachineResults {
    private int democratVotes;
    private int republicanVotes;
    public VotingMachineResults(democratVotes,republicanVotes) {
        this.democratVotes= democratVotes ;    // `this` required to disambiguate field democratVotes from parameter democratVotes.
        this.republicanVotes= republicanVotes ;
    }
    public int getDemocratVotes() {
        return democratVotes ;
    }
    public int getRepublicanVotes() {
        return republicanVotes ;
    }
}

As you can see, this class is very similar to VotingMachine1, but it does not accept internal state changes. It is a "value" class.

Upvotes: 2

Fortyrunner
Fortyrunner

Reputation: 12782

A less specific answer to your question would be to return an Object called (say) Votes

public class Vote {
  int democratVotes
  int republicanVotes
} 

and then make your VotingMachine class simply return an instance of this object (suitably changed to make it immutable).

On my project we have created a generic version of this called a Tuple that returns a pair of values in a single object - it has an overloaded toString method for easy printing.

Upvotes: 0

Makoto
Makoto

Reputation: 106470

Here's something completely different: why not override toString()?

Presumably, any instance of VotingMachine1 will apply for all votes that you care about for that instance. That is to say, you don't create a new instance of a VotingMachine1 every time someone casts a vote.

So, what you can do is override the toString() method. We'll also use String.format() to handle the numerical values.

@Override
public String toString() {
    // assumes that Democrats and Republicans are declared as int
    // since it's pointless to indicate percentages of a vote
    return String.format("Democrats: %d\nRepublicans: %d", Democrats, Republicans);
}

Now, whenever you vote, you can use the toString() method to get the information (which is called whenever one does System.out.println(object).

VotingMachine1 voter = new VotingMachine1();
voter.voteDemocrat();
voter.voteRepublican();
System.out.println(voter);
/* This prints:
    Democrats: 1
    Republicans: 1
*/

Upvotes: 0

Stephen C
Stephen C

Reputation: 719259

Your code and your description are so contradictory, it is not clear that you even know what you are trying to do. I believe that this is the real root of your problems.

Here goes:

    public double getTallies() 
    {
       System.out.println("Democrats: ", return Democrats);      
       System.out.println("Republicans: ", return Republicans);
    }

First, your question says that you want to "return two strings with two values" ... but you have declared the method as returning one double.

Next, your code is printing values ... not returning them.


You've also made some major mistakes at the syntactic level, largely (I believe) because you are trying to do contradictory things:

  • return Republicans is not a valid Java expression, so you can't use it as a argument to the println method.

  • The println method can't be called with two arguments, as your code is trying to do. There is a zero argument version and a number of one argument overloads ... but no overloads with two or more arguments.


Basically, you need to start by making up your mind about what this method is supposed to do. Is it supposed to:

  • return the tallies (as two doubles)?
  • return a string representing the two tallies?
  • return nothing ... and output the two tallies to standard output?
  • do something else?

Once you've made up your mind:

  • code the method to do what you've decided it should do, and
  • chose a method name that correctly reflects what it is supposed to do. Hint: a method that starts with get is conventionally a "getter" that returns the attribute or attributes themselves ... not a String rendering.

double is a bad choice of type for a vote count too:

  • You cannot have a fractional vote.
  • You want to represent vote counts precisely and floating point types (like double) are not precise. (Or at least, not in the sense that you require.)
  • When you attempt to format or output a double, the resulting character string is likely to include a pesky decimal point ... or worse.

You should use int or long instead of double.


Finally, this is a serious Java style violation, and should get you a significant penalty if your marker is paying attention.

    private double Democrats;
    private double Republicans;

Variable names in Java should start with a LOWER CASE letter.

Upvotes: 3

lily
lily

Reputation: 2998

What you are looking for here is a format string. A format string is used when you know what your output should look like, and only have a few "holes" where unknown data should be filled in. To output your data using format strings, you would use the System.out.format(String, Object...) method:

System.out.format("Democrats: %f\n", Democrats);      
System.out.format("Republicans: %f\n", Republicans);

In this case, the %f indicates that a floating-point number (since your variables are declared as double) will be printed instead of the %f. However, you may wish to consider declaring them as int (or long) instead, in which case you would use %d instead of %f in the format strings.

Finally, you ought to change your getTallies() method to return void instead of double, as you are printing the values, not returning them.

Upvotes: 4

qaphla
qaphla

Reputation: 4743

No return is necessary there, since you aren't leaving a function. To do what you seem to want to do, just replace that last method with the following:

public void getTallies()
{
    System.out.println("Democrats: " + Double.toString(Democrats));      
    System.out.println("Republicans: " + Double.toString(Republicans));
}

Also, since your votecounts should only ever be integers, there's no reason to declare them as doubles instead of ints.

Upvotes: 6

Kon
Kon

Reputation: 10810

In Java, you concatenate Strings with the + operator. Proper syntax for what you were trying to do looks like this:

System.out.println("Democrats: " + Democrats);
System.out.println("Republicans: " + Republicans);

A return statement is only used when you want to return some object or value to a method that called your current method. It is not appropriate in this place since you're only passing a value to another method (println()).

ALSO, you need to fix your getTallies() method. Make it return void instead of double since you aren't returning anything.

Upvotes: 1

Related Questions