Click here to Skip to main content
15,918,742 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a problem where the user inputs integers until they type "done", at which point the even and odd averages are calculated and displayed. I've nearly got it, but can't figure out how to get around the scanner expecting an int input, but gets a string, so it crashes there (mismatched type exception).

Java
static void problem4()
	    {
	    	int evenCount = 0;
	    	int oddCount = 0;
	    	int evenTotal = 0;
	    	int oddTotal = 0;
	    	double evenAverage;
	    	double oddAverage;
	    	String endKey = "DONE";
	    	int entry;
	    	
	    	while (true)
	    	{
	    		System.out.print("Enter a number or 'done' to quit: ");
	    		
	    		entry = input.nextInt();
	    		
	    		if (input.nextLine().toUpperCase().equals(endKey))
	    		{	
	    			break;
	    		}
	    		    		
	    		if (entry % 2 == 0)
	    		{
	    			evenCount++;
	    			evenTotal = evenTotal + entry;
	    		}
	    		else
	    		{	
	    			oddCount++;
	    			oddTotal = oddTotal + entry;
	    		}
	    	}		
	    		
	    	evenAverage = (double) evenTotal / (double) evenCount;
	    	oddAverage = (double) oddTotal / (double) oddCount;
	    	System.out.println("Even average: " + evenAverage);
	    	System.out.println("Odd average: " + oddAverage);
	    	
	    }


What I have tried:

I've tried instead of doing nextInt(), just doing next(), but that didn't work. Still quite new to Java.
Posted
Updated 1-Feb-22 10:52am
v2

Take input as string because you are checking with "Done" which is string and when you take string as input through input.nextInt() it will throw an exception. I had modified you code little bit:
static void problem4()
{
	int evenCount = 0;
	int oddCount = 0;
	int evenTotal = 0;
	int oddTotal = 0;
	double evenAverage;
	double oddAverage;
	String endKey = "DONE";
	String entryVal;
	int entry;
		
	while (true)
	{
		System.out.print("Enter a number or 'done' to quit: ");
		entryVal = entryVal.nextLine();
		if (entryVal.nextLine().toUpperCase().equals(endKey))
		{	
			break;
		}
				
		entry = Integer.parseInt(entryVal);		
		if (entry % 2 == 0)
		{
			evenCount++;
			evenTotal = evenTotal + entry;
		}
		else
		{	
			oddCount++;
			oddTotal = oddTotal + entry;
		}
	}		
		
	evenAverage = (double) evenTotal / (double) evenCount;
	oddAverage = (double) oddTotal / (double) oddCount;
	System.out.println("Even average: " + evenAverage);
	System.out.println("Odd average: " + oddAverage);
	
}
 
Share this answer
 
v2
You should get a string as input and then try to parse it as a number.
Java
System.out.print("Enter a number or 'done' to quit: ");

entry = input.nextLine();

if (entry.toUpperCase().equals(endKey))
{
  break;
}

int n;
try
{
  n = Integer.parseInt(entry);
  if (n% 2 == 0)
  {
    evenCount++;
    evenTotal = evenTotal + n;
  }
  else
  {
    oddCount++;
    oddTotal = oddTotal + n;
  }
}
catch (NumberFormatException e)
{
  System.out.println("invalid entry");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900