this has me stumped. Im using a loop to go through an array and then using ifs/else to find whether it meets the criteria. However the else statement keeps being executed even though an if is being executed aswell. Probably something really basic im missing. I need to check in the if statements twice as the user is entering a genre aswell as the item in the array meets that genre too.
cheers
for (int i = 0; i < movies.length; i++)
{
//RentalMovie movie = movies[i];
if(movies[i].getMovieGenre().equalsIgnoreCase("action") && movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if(movies[i].getMovieGenre().equalsIgnoreCase("childrens") && movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if (movies[i].getMovieGenre().equalsIgnoreCase("drama") && movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else
{
genreList += "- No rental movies were found for the genre: " + genreID;
}
}
I suspect what you mean is that you have some entries in the array that do match your search condition, and others that don't. For each match, the appropriate if clause is executing, but for each non-match, the else is executing.
Since the action in the else is to add a message indicating that no matches were found, you probably don't want to add that every time a single non-match is found. Rather, you want to do it once, after the loop, if no matches at all were found. This might look like:
String genreList = "";
for (int i = 0; i < movies.length; i++)
{
if(movies[i].getMovieGenre().equalsIgnoreCase("action") && movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if(movies[i].getMovieGenre().equalsIgnoreCase("childrens") && movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if (movies[i].getMovieGenre().equalsIgnoreCase("drama") && movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else
{
}
}
if (genreList.equals(""))
{
genreList = "No rental movies were found for the genre: " + genreID;
}
P.S. I don't understand at all why you are checking the genre for each movie against both genreID and a hardcoded value before accepting it. Are there other genres that exist in the list but you don't want a user to be able to search for? That's the only way it seems to make sense, but that doesn't seem like a very sensible requirement.
What is probably happening is that the if statement is not being evaluated correctly. This is what I would try:
for (int i = 0; i < movies.length; i++)
{
//RentalMovie movie = movies[i];
if(movies[i].getMovieGenre().equalsIgnoreCase("action") && movies[i].getMovieGenreID().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if(movies[i].getMovieGenre().equalsIgnoreCase("childrens") && movies[i].getMovieGenreID().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if (movies[i].getMovieGenre().equalsIgnoreCase("drama") && movies[i].getMovieGenreID().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else
{
genreList += "- No rental movies were found for the genre: " + genreID;
}
}
The other thing I would try is to get rid of some of the code, ie:
for (int i = 0; i < movies.length; i++)
{
//RentalMovie movie = movies[i];
if(movies[i].getMovieGenre().equalsIgnoreCase("action"))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if(movies[i].getMovieGenre().equalsIgnoreCase("childrens"))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if (movies[i].getMovieGenre().equalsIgnoreCase("drama"))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else
{
genreList += "- No rental movies were found for the genres action, childrens, or drama -";
}
}
The third thing I would suggest would be to try this:
for (int i = 0; i < movies.length; i++)
{
//RentalMovie movie = movies[i];
if(movies[i].getMovieGenre().equalsIgnoreCase("action") || movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if(movies[i].getMovieGenre().equalsIgnoreCase("childrens"))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if (movies[i].getMovieGenre().equalsIgnoreCase("drama"))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else
{
genreList += "- No rental movies were found for the genre: " + genreID;
}
}
Try stepping through it with a debugger and you can watch the state of the variables as you go.
Related
I'm trying to concatenate string values from clientGroupList to make a string variable. I cannot access to the returned variable. I'm working on shareString_8 and shareString_9.
if (uib.getClientProviderType().equals(ParamConstant.CLIENT_TYPE)) {
// ClientUserの場合の処理
//SM4|
String shareString_8 = shareStringList + ":SM" + Constant.SHARE_MODE_CLIENT_GROUP + "|";
for (int idx = 0; idx < clientGroupList.size(); idx++) {
//SM4|SHARE_CLIENT_GROUP
shareString_8 = shareString_8 + clientGroupList.get(idx);
}
return shareString_8;
} else {
// ProviderUserの場合の処理
//SM-1|
String shareString_9 = shareStringList + ":SM" + Constant.SHARE_MODE_PUBLISH + "|";
for (int idx = 0; idx < entityListClientGroup.size(); idx++) {
//SM-1|CLIENT_GROUP_ID
shareString_9 = shareString_9 + clientGroupList.get(idx);
}
return shareString_9;
}
String shareString = providerId + " AND " + "(" + shareString_1 + " OR " + shareString_2 + " OR " + shareString_3
+ " OR " + shareString_4 + " OR " + shareString_5 + " OR " + shareString_6 + " OR " + shareString_7
+ " OR " + shareString_8 + " OR " + shareString_9 + ")";
I am a novice with Java, and I am experiencing an error in method userInput, line 16, which throws a no such element exception. I need to be able to pass the values of both 'before' and 'after.' Thanks! Here is the code:
import java.util.Scanner;
public class BasketballGame {
public static String nameFirstTeam = "Bulldogs";
public static String nameSecondTeam = "Wildcats";
private static String before, character1, character2, after;
public static int scoreFirstTeam = 0;
public static int scoreSecondTeam = 0;
public int scoreOne, scoreTwo, scoreThree;
//determine end of game
//System.out.println("Enter the team that scored ('a' for Bulldogs, 'b' for Wildcats), then enter the number of points scored.");
public static void userInput(){
Scanner keyboard = new Scanner(System.in);
//keyboard.useDelimiter(" ");
character1 = keyboard.next();
before = String.valueOf(character1.charAt(0));
//System.out.println(before);
character2 = keyboard.next();
after = String.valueOf(character1.charAt(1));
//System.out.println(after);
}
public static boolean finished(){
if (scoreFirstTeam >= 50){
return true;
}
else if (scoreSecondTeam >= 50){
return true;
}
else{
return false;}
}
public static void playGame(){
while(!finished()){
if (before== "a"){
System.out.println("a");
}
if (after=="1"){
System.out.println("1");
}
System.out.println("Enter a score.");
userInput();
if (before=="a" && after=="1"){
scoreFirstTeam = (scoreFirstTeam +1);
if (scoreFirstTeam > scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " are winning.");
}
else if (scoreFirstTeam < scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " are winning.");
}
else{
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is tied.");
}
}
else if (before=="a" && after=="2"){
scoreFirstTeam = (scoreFirstTeam +2);
if (scoreFirstTeam > scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " are winning.");
}
else if (scoreFirstTeam < scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " are winning.");
}
else{
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is tied.");
}
}
else if (before=="a" && after=="3"){
scoreFirstTeam = (scoreFirstTeam +3);
if (scoreFirstTeam > scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " are winning.");
}
else if (scoreFirstTeam < scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " are winning.");
}
else{
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is tied.");
}
}
else if (before=="b" && after=="1"){
scoreSecondTeam = (scoreSecondTeam +1);
if (scoreFirstTeam > scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " are winning.");
}
else if (scoreFirstTeam < scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " are winning.");
}
else{
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is tied.");
}
}
else if (before=="b" && after=="2"){
scoreSecondTeam = (scoreSecondTeam +2);
if (scoreFirstTeam > scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " are winning.");
}
else if (scoreFirstTeam < scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " are winning.");
}
else{
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is tied.");
}
}
else {
scoreSecondTeam = (scoreSecondTeam +3);
if (scoreFirstTeam > scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " are winning.");
}
else if (scoreFirstTeam < scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " are winning.");
}
else{
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is tied.");
}
}
}
System.out.println("");
}
public static void main(String[] args){
BasketballGame game = new BasketballGame();
game.playGame();
while (finished()){
if (scoreFirstTeam > scoreSecondTeam){
System.out.println(nameFirstTeam + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " win!");
}
else if (scoreFirstTeam < scoreSecondTeam){
System.out.println(nameFirstTeam + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " win!");
}
else{
System.out.println(nameFirstTeam + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is a draw.");
}
}
}
}
The purpose of this program is to build a 10x10 array grid made out of periods (.), allow the user to designate a starting point, and then the program will randomly choose numbers which are assigned to directions for the 'walker' to go. Each time it moves it marks its spot with the next letter in the alphabet (the starting point is marked by A). I haven't done this part yet but I know how I will; if the walker crosses out of the bounds of the array (AKA > 10 or < 0) it will say "you were arrested" and if the variable alpha == 'Z' it way say "You made it home".
The main problem as far as I can tell is with the method processing(), where I draw numbers from getRand (which I have confirmed is working) and then assign them directions which are given to the array. But for some reason when it prints the grid it only shows the starting point, and not the movement of the walker.
Another question I have of lesser importance is if there is a more brief way to display my grid than how I have been (A massive brick of text in a System.out.print)
package walktester;
import java.lang.Math;
import java.util.Random;
import java.util.Scanner;
class DrunkWalker {
private char[][] walkgrid = new char[10][10];
private static int randNSEW;
private int randomnum;
private int startrow;
private int startcol;
private char alpha = 'A';
private int nextrow;
private int nextcol;
public DrunkWalker(int r, int c) {
startrow = r;
startcol = c;
nextrow = startrow;
nextcol = startcol;
for (int i = 0; i < 10; i ++) {
for (int j = 0; j < 10; j++)
walkgrid[i][j] = '.';
}
walkgrid[r][c] = alpha;
}
public static void getRand(){
int x100 = 0;
double randomNum = 0.0;
randomNum = Math.random();
x100 = (int) (randomNum * 100);
randNSEW = x100 % 4;
}
public int getNextRow(){
return nextrow;
}
public int getNextCol(){
return nextcol;
}
public void processing(){
for(int i = 0; i < 26; i ++){
getRand();
if(randNSEW == 0){
nextcol--;
walkgrid[nextrow][nextcol] = alpha++;
}
if(randNSEW == 1){
nextrow++;
walkgrid[nextrow][nextcol] = alpha++;
}
if(randNSEW == 2){
nextcol++;
walkgrid[nextrow][nextcol] = alpha++;
}
if(randNSEW == 3){
nextrow--;
walkgrid[nextrow][nextcol] = alpha++;
}
}
}
public char[][] DisplayGrid() {
System.out.print(
walkgrid[0][0] + " " + walkgrid[0][1] + " " + walkgrid[0][2] + " " + walkgrid[0][3] + " " + walkgrid[0][4] + " " + walkgrid[0][5] + " " + walkgrid[0][6] + " " + walkgrid[0][7] + " " + walkgrid[0][8] + " " + walkgrid[0][9] + "\n" +
walkgrid[1][0] + " " + walkgrid[1][1] + " " + walkgrid[1][2] + " " + walkgrid[1][3] + " " + walkgrid[1][4] + " " + walkgrid[1][5] + " " + walkgrid[1][6] + " " + walkgrid[1][7] + " " + walkgrid[1][8] + " " + walkgrid[1][9] + "\n" +
walkgrid[2][0] + " " + walkgrid[2][1] + " " + walkgrid[2][2] + " " + walkgrid[2][3] + " " + walkgrid[2][4] + " " + walkgrid[2][5] + " " + walkgrid[2][6] + " " + walkgrid[2][7] + " " + walkgrid[2][8] + " " + walkgrid[2][9] + "\n" +
walkgrid[3][0] + " " + walkgrid[3][1] + " " + walkgrid[3][2] + " " + walkgrid[3][3] + " " + walkgrid[3][4] + " " + walkgrid[3][5] + " " + walkgrid[3][6] + " " + walkgrid[3][7] + " " + walkgrid[3][8] + " " + walkgrid[3][9] + "\n" +
walkgrid[4][0] + " " + walkgrid[4][1] + " " + walkgrid[4][2] + " " + walkgrid[4][3] + " " + walkgrid[4][4] + " " + walkgrid[4][5] + " " + walkgrid[4][6] + " " + walkgrid[4][7] + " " + walkgrid[4][8] + " " + walkgrid[4][9] + "\n" +
walkgrid[5][0] + " " + walkgrid[5][1] + " " + walkgrid[5][2] + " " + walkgrid[5][3] + " " + walkgrid[5][4] + " " + walkgrid[5][5] + " " + walkgrid[5][6] + " " + walkgrid[5][7] + " " + walkgrid[5][8] + " " + walkgrid[5][9] + "\n" +
walkgrid[6][0] + " " + walkgrid[6][1] + " " + walkgrid[6][2] + " " + walkgrid[6][3] + " " + walkgrid[6][4] + " " + walkgrid[6][5] + " " + walkgrid[6][6] + " " + walkgrid[6][7] + " " + walkgrid[6][8] + " " + walkgrid[6][9] + "\n" +
walkgrid[7][0] + " " + walkgrid[7][1] + " " + walkgrid[7][2] + " " + walkgrid[7][3] + " " + walkgrid[7][4] + " " + walkgrid[7][5] + " " + walkgrid[7][6] + " " + walkgrid[7][7] + " " + walkgrid[7][8] + " " + walkgrid[7][9] + "\n" +
walkgrid[8][0] + " " + walkgrid[8][1] + " " + walkgrid[8][2] + " " + walkgrid[8][3] + " " + walkgrid[8][4] + " " + walkgrid[8][5] + " " + walkgrid[8][6] + " " + walkgrid[8][7] + " " + walkgrid[8][8] + " " + walkgrid[8][9] + "\n" +
walkgrid[9][0] + " " + walkgrid[9][1] + " " + walkgrid[9][2] + " " + walkgrid[9][3] + " " + walkgrid[9][4] + " " + walkgrid[9][5] + " " + walkgrid[9][6] + " " + walkgrid[9][7] + " " + walkgrid[9][8] + " " + walkgrid[9][9] + "\n"
);
return walkgrid;
}
}
public class WalkTester {
public static void main(String[] args) {
Scanner inpr = new Scanner(System.in);
Scanner inpc = new Scanner(System.in);
Scanner inpchoice = new Scanner(System.in);
int r = 0;
int c = 0;
char choice = 'y';
while(choice == 'y' || choice == 'Y') {
System.out.println("Please enter x coordinate between 1 and 10.");
r = inpr.nextInt();
r = r - 1;
System.out.println("Please enter y coordinate between 1 and 10");
c = inpr.nextInt();
c = c - 1;
if(r < 0 || r > 9 || c < 0 || c > 9){
System.out.println("Invalid Entry. Restart? y/n");
choice = inpchoice.next().charAt(0);
if(choice == 'y' || choice == 'Y'){
continue;
}
else if(choice == 'n' || choice == 'N'){
return;
}
else{
System.out.println("Invalid Entry. Restart? y/n");
choice = inpchoice.next().charAt(0);
}
}
DrunkWalker drunkwalker = new DrunkWalker(r, c);
drunkwalker.DisplayGrid();
System.out.println("Restart? y/n");
choice = inpchoice.next().charAt(0);
if(choice == 'y' || choice == 'Y'){
continue;
}
else if(choice == 'n' || choice == 'N'){
return;
}
else{
System.out.println("Invalid Entry. Restart? y/n");
choice = inpchoice.next().charAt(0);
}
}
}
}
The code that moves the walker is in a method called processing(), but you never call it.
You can make your DisplayGrid() method simpler (and it will still print the same stuff):
public char[][] DisplayGrid() {
for(int y = 0; y < 10; y++) {
for(int x = 0; x < 10; x++) {
System.out.print(walkgrid[x][y] + " ");
}
System.out.println();
}
return walkgrid;
}
Your processing() method needs to check if the walker leaves the bounds of the area. If you don't, you'll get an ArrayIndexOutOfBoundsException.
public boolean processing(){
for(int i = 0; i < 26; i ++){
getRand();
if(randNSEW == 0){
nextcol--;
}
if(randNSEW == 1){
nextrow++;
}
if(randNSEW == 2){
nextcol++;
}
if(randNSEW == 3){
nextrow--;
}
if(nextrow < 0 || nextrow >= 10 || nextcol < 0 || nextcol >= 10) {
return false;
}
walkgrid[nextrow][nextcol] = alpha++;
}
return true;
}
Now you need to call processing() and check the return value to see if the walker succeeded:
DrunkWalker drunkwalker = new DrunkWalker(r, c);
boolean walkerSucceeded = drunkwalker.processing();
drunkwalker.DisplayGrid();
if(walkerSucceeded) {
System.out.println("You made it home");
} else {
System.out.println("You were arrested");
}
And keep in mind (as you will see when you test this), that the walker can cross their own tracks (so you might see some letters missing).
I have a small part of code where I get an error when I try to compile. Any pointers?
The code:
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.size(); j++) {
if(fra.linjeList[i].equals(til.linjeList[j])){
Linje aktuellLinje=new Linje(linjerMap.get(linjeList[i]));
retning=aktuellLinje.stasjonsNummer(startStasjon) - aktuellLinje.stasjonsNummer(sluttStasjon);
endeStasjonsNavn=aktuellLinje.endestasjon(retning).stasjonsNavn;
System.out.println("Ta T-bane linje " + aktuellLinje.linjeNummer + " fra " + startStasjon + " til " sluttStasjon + " i retning " + endeStasjonsNavn + ". Estimert reisetid: " + tid);
}
}
}
}
And the error:
oblig5.java:132: error: ')' expected
System.out.println("Ta T-bane linje " + aktuellLinje.linjeNummer + " fra " + startStasjon + " til "
sluttStasjon + " i r etning " + endeStasjonsNavn + ". Estimert
reisetid: " + tid);
^
This is the culprit :
" til " sluttStasjon
Make it
" til " + sluttStasjon
You are missing a plus in
" til " sluttStasjon
^ HERE
You are missing a + before sluttStasjon in your print statement
Hi this is some code I've constructed for a project which calls several other methods to print a statement about the person. My problem is its printing in double and appears to be running through both if/else statements.
How can I prevent this? if needed I can add the text from my terminal. Code below;
void printPersonDescription(String name, boolean sex, int age) {
if(sex == true && yearsUntilNextMaturityLevel(age) > 1) {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" she has " + yearsUntilNextMaturityLevel(age) + " years left until she is " +
nextMaturityLevel(age) + ".");
}
else if(sex == false && yearsUntilNextMaturityLevel(age) > 1) {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" he has " + yearsUntilNextMaturityLevel(age) + " years left until he is " +
nextMaturityLevel(age) + ".");
}
if (sex == true && yearsUntilNextMaturityLevel(age) <= 1) {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" she has " + yearsUntilNextMaturityLevel(age) + " year left until she is " +
nextMaturityLevel(age) + ".");
}
else {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" he has " + yearsUntilNextMaturityLevel(age) + " year left until he is " +
nextMaturityLevel(age) + ".");
}
}
Note how similar your four cases are. You are just changing some words in the line you print. Those four if/else blocks can be simplified to this:
void printPersonDescription(String name, boolean sex, int age) {
int years = yearsUntilNextMaturityLevel(age);
String gender = sex ? "she" : "he";
String plural = years > 1 ? "s" : "";
println(name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" " + gender + " has " + years + " year" + plural + " left until " + gender + " is " +
nextMaturityLevel(age) + ".");
}
Also, you can make the println a bit more readable by using String.format (assuming that the maturity levels are strings)
String.format("%s is %s. At %d %s has %d year%s left until %s is %s.",
name, getMaturityLevel(age), age, gender, years, plural, gender,
nextMaturityLevel(age))
Third if condition should be else if in your code to avoid double printing.
Instead of:
if (sex == true && yearsUntilNextMaturityLevel(age) <= 1) {
Use:
else if (sex && yearsUntilNextMaturityLevel(age) <= 1) {
btw you don't need to use if (boolVar == true) you can just use if (boolVar)
void printPersonDescription(String name, boolean sex, int age) {
if(sex == true && yearsUntilNextMaturityLevel(age) > 1) {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" she has " + yearsUntilNextMaturityLevel(age) + " years left until she is " +
nextMaturityLevel(age) + ".");
}
else if(sex == false && yearsUntilNextMaturityLevel(age) > 1) {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" he has " + yearsUntilNextMaturityLevel(age) + " years left until he is " +
nextMaturityLevel(age) + ".");
}
else if (sex == true && yearsUntilNextMaturityLevel(age) <= 1) {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" she has " + yearsUntilNextMaturityLevel(age) + " year left until she is " +
nextMaturityLevel(age) + ".");
}
else {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" he has " + yearsUntilNextMaturityLevel(age) + " year left until he is " +
nextMaturityLevel(age) + ".");
}
}
You need add else before the third if. Otherwise, the final else statement will match the third if, thus, the else statement means
if(sex != true || yearsUntilNextMaturityLevel(age) > 1)