April 17th, 2013, 04:16 PM
-
Exception handling program error
hey guys
please help
when i run the code i get : error cannot find symbol
for outputData(actualVotes, voteTalley);
i know that im calling it in the main method and that i need to add it to a class some how but i dont know how can somebody please show me i will paste code below
thanks so much
<code>
import java.util.*;
public class RateJava {
public static void main (String [] args)
{
int [] voteTalley = {0,1,2,3,4,5,6,7,8,9,10};
int [] actualVotes = new int [11];
inputData(actualVotes);
outputData(actualVotes, voteTalley);
}
public static void inputData (int[]actualVotes)
{
Scanner myKeyboard = new Scanner (System.in);
while(true)
{
int index;
System.out.print("Please enter your votes (-1 to exit): ");
try
{
index = myKeyboard.nextInt();
}
catch(InputMismatchException e)
{
System.out.println("This is an invalid number");
myKeyboard.nextLine();
continue;
}
if(index == -1)
return;
if (index >= 0 && index < actualVotes.length)
actualVotes[index]++;
else
System.out.println("Invalid number");
}
}
}
<code>
April 17th, 2013, 04:30 PM
-
Please put your code in code tags to keep proper code format/indentation.
you will have to create the function, similar to what you did with when you created the inputData function.
April 17th, 2013, 07:32 PM
-
Here is his code in a more readable format.
Code:
import java.util.*;
public class RateJava {
public static void main (String [] args)
{
int [] voteTalley = {0,1,2,3,4,5,6,7,8,9,10};
int [] actualVotes = new int [11];
inputData(actualVotes);
outputData(actualVotes, voteTalley);
}
public static void inputData (int[]actualVotes)
{
Scanner myKeyboard = new Scanner (System.in);
while(true)
{
int index;
System.out.print("Please enter your votes (-1 to exit): ");
try
{
index = myKeyboard.nextInt();
}
catch(InputMismatchException e)
{
System.out.println("This is an invalid number");
myKeyboard.nextLine();
continue;
}
if(index == -1)
return;
if (index >= 0 && index < actualVotes.length)
actualVotes[index]++;
else
System.out.println("Invalid number");
}
}
}
The problem with this code, as MrFujin stated, is that you are calling a function that does not exist.
This line works fine because you have a method called inputData that accepts an array as an argument.
inputData(actualVotes);
The line outputData(actualVotes, voteTalley); is a compilation error because this is an undefined method.
I'm not 100% sure what the purpose is of this code, but I think your outputData method will look something like this...
Code:
public static void outputData(int[] a, int[] b)
{
for (int i = 0; i < a.length; i++)
{
System.out.println("There were "+a[i]+" votes rating Java a "+b[i]+".");
}
}