Variable Mismatch with case

0 votes
asked May 15, 2017 by jandrakin

I am having difficulty with a java problem I am attempting to make, I am very new and still learning if anyone can spot what I am doing wrong I would appreciate any help.

I am attempting to have the user type in a year from 2000 to 2016 and the output would be the players statistics.

Thank You

import java.util.Scanner;
public class NflPlayers{ public static void main(String[]args) { Scanner input = new Scanner(System.in); System.out.print("Enter a Year (2000-2016): " ); String year = input.next(); input.close(); if (yearNumber(year) == 0) { System.out.println("Year 2000-2016"); System.out.println("Invalid year input."); System.exit(0); } System.out.println(year + " Passing touchdowns"); } int getNumberOfPasses(int year) { switch (yearNumber(null)) { case 1: return 28; case 2: return 36; case 3: return 33; case 4: return 25; case 5: return 34; case 6: return 39; case 7: return 36; case 8: return 28; case 9: return 0; case 10: return 50; case 11: return 24; case 12: return 26; case 13: return 28; case 14: return 23; case 15: return 28; case 16: return 18; case 17: return 0; default: return 0; } } public static int yearNumber(String year) { int yearNumber; switch (year){ case"2016": yearNumber = 1; break; case"2015": yearNumber = 2; break; case"2014": yearNumber = 3; break; case"2013": yearNumber = 4; break; case"2012": yearNumber = 5; break; case"2011": yearNumber = 6; break; case"2010": yearNumber = 7; break; case"2009": yearNumber = 8; break; case"2008": yearNumber = 9; break; case"2007": yearNumber = 10; break; case"2006": yearNumber = 11; break; case"2005": yearNumber = 12; break; case"2004": yearNumber = 13; break; case"2003": yearNumber = 14; break; case"2002": yearNumber = 15; break; case"2001": yearNumber = 16; break; case"2000": yearNumber = 17; break; default: yearNumber = 0; break; } return yearNumber; }
}

2 Answers

0 votes
answered May 15, 2017 by ycf-l

You ca use do{}while() loop instead for example :

Scanner input = new Scanner(System.in);
String year;
boolean pass = false;
do { System.out.print("Enter a Year (2000-2016): "); year = input.next(); try { if (Integer.parseInt(year) >= 2000 && Integer.parseInt(year) <= 2006) { System.out.println("true"); pass = true; } } catch (NumberFormatException e) { System.out.println("Not correct try again"); }
} while (!pass);

So if the user enter a year between 2000 and 2006 you can pass, else repeat again until the user enter the correct year.

0 votes
answered Dec 23, 2018 by devlin-wyatt

I've run your code and examined it.

It appears you've left out a method call for getNumberOfPasses(), which is the main problem I can tell.

with System.out.println(year + " Passing touchdowns"); you don't actually want the year displayed, i believe the correct code you're looking for is:

 System.out.println(getNumberOfPasses(yearNumber(year)) + " Passing touchdowns");

This will run through both methods to display the info you need. However, you will need to fix the method header of int getNumberOfPasses(int year) to static int getNumberOfPasses(int year)

Furthermore, you could eliminate one of the methods entirely and just have the return values for passes tied to the year you input. Rather than passing digits from 1-17 to another method.

Hope this helps and good luck with your projects!

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...