I am trying to get my method to return to the main. I have tried return statements but when i run it all it gives me is nothing. It just follows what the main method has but the other methods do not get to return to the main method so what i have on the other methods do not show up in the output. Here is my code

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package project2;

import java.util.Scanner;

/**
 *
 * @author Jonathan
 */
public class Project2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        Scanner keyboard = new Scanner(System.in);



        flipcoin();

    }
   public static int GetNum(String msg) {
        Scanner keyboard= new Scanner(System.in);

        int getNum = GetNum ("Enter the number of coins ");
         getNum = keyboard.nextInt();
         if (getNum < 0) 
         {
        throw new IllegalArgumentException("Age cannot be negative.");
         // setter logic
         }
        return getNum;

    }
    private static int flipcoin() {
        int randomNum = (int)(Math.random()*2) +1;
        return randomNum;
    }
    public static String getFlipType(int sideID)
    {
        if (sideID ==1)
        {
            return "heads";
        }
        else if (sideID ==2)
        {
            return "tails";
        }
        return null;  
    }
    public static void printNumCoins(int headsOrTails, int numHeadsOrTails)
    {
        System.out.println(headsOrTails);
    }


}

Recommended Answers

All 8 Replies

You need to save the returned value from flipcoin() into an integer.
Because right now, you're just generating a value and then completely forgetting it.
Try something like:

int coin = flipcoin();

and what about the GetNum method

You didn't even call that method in your main.
To the compiler, it doesn't exist.
Also, I recommend using the random class (in your case).
Math.random generates a value between 0 and 1.
Random r = new Random(2); generates either 0 or 1.
So you could adjust your getFlipType method to 0s and 1s.

how do u suppose i do that because i tried that but it will not let me do it

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

       int work = GetNum(msg);

       int coin = flipcoin();

    }
   public static int GetNum(String msg) {
        Scanner keyboard= new Scanner(System.in);

        int getNum = GetNum ("Enter the number of coins ");
         getNum = keyboard.nextInt();
         if (getNum < 0) 
         {
        throw new IllegalArgumentException("Numer of coins cannot be negative.");
         // setter logic
         }
        return getNum;

    }

I could write the code for you, but that wouldn't be helping you.

You need to save the input from Scanner keyboard into a variable as well.
Later, you pass that variable into your GetNum method (to see if the variable is less then 0). We'll call that variable int numberOfCoins.

If it isn't you should continue into a for loop that iterates numberOfCoins times.

for (int i = 0; i <= numberOfCoins; i++){
    // 1. save coin value into a variable
    // 2. print head or tails based on coin value   
}

Bare in mind, I'm just guessing here based on your methods because your original post didn't specify what you're trying to do.

EDIT:
You never used System.out.println(); so your program never prints any results in the console.

wat i am trying to do is to get the program to ask the user for a whole number then it determines if the number is positive or negative if positive it flips the coins that many times but if it negative it gives an error and then asks again for a number

Thats what I thought you're doing.
Try following the hints I gave you and report back with changes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.