Results 1 to 2 of 2
Thread: switch (counter) example please
- 12-03-2010, 10:46 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
switch (counter) example please
Howdy,
I'm a little lost on this. I cant seem to find any info on what the syntax is for what goes between the switch label and the break statement. I appreciate any direction you can give me!
I understand how to write a switch statment like this...
switch (grade)
{
case A : System.out.println("Splendid!"); break;
case B : System.out.println("Nice job!"); break;
case C : System.out.println("Need to do better"); break;
case D : System.out.println("Not good"); break;
case F : System.out.println("Uh oh"); break;
default : System.out.println("No grade provided");
}
but not how to fill in a
switch (counter)
Here's all of my code so far:
// Class TestEnum reads in a letter as a string, and converts it to the //appropriate LetterGrade. The number of times each grade appears is //summed, using a switch statement. The tallies are printed, using a switch //statement.
import java.io.*;
import java.util.*;
public class TestEnum
{
enum LetterGrade {A,B,C,D,F}
private static final int counter = 0;
public static void main(String[] args) throws IOException
{
Scanner inFile = new Scanner(new FileReader("Enum.dat"));
LetterGrade grade;
String strGrade;
int ACount = 0;
int BCount = 0;
int CCount = 0;
int DCount = 0;
int FCount = 0;
while (inFile.hasNext())
{
grade = LetterGrade.valueOf(inFile.nextLine());
// Add one to tally associated with grade
switch (grade)
{
case A : System.out.println("Splendid!"); break;
case B : System.out.println("Nice job!"); break;
case C : System.out.println("Need to do better"); break;
case D : System.out.println("Not good"); break;
case F : System.out.println("Uh oh"); break;
default : System.out.println("No grade provided");
}
}
for (int Count = 1; Count <= 20; Count++) /* Exercise 3 */
// Iterate through each value of LetterGrade
{
System.out.print(counter + ": ");
// Print tally associated with each LetterGrade
switch (counter)
{
/* Have NO idea what I'm doing below*/
case ACount : something needs to go here; break;
case BCount : something needs to go here?; break;
case CCount : something needs to go here?; break;
case DCount : something needs to go here?; break;
case FCount : something needs to go here?; break;
default : System.out.println(grade + "Not a valid grade"); break;
}
counter++;
}}}
- 12-03-2010, 11:21 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
It might be a good idea to edit your post or post again using code tags. Put [CODE] at the start of your code and [/CODE] at the end. (If you edit your post you may have to clean up the indents.)
What is main() in TestEnum supposed to do?
In general you can put anything after the case part - any statements or blocks of code that would make sense in a method. The "break" is very common but actually optional. Without "break" the statements associated with the next case are also carries out which is normally not what you want.
Similar Threads
-
Increment Counter for ID
By blkshrk81 in forum New To JavaReplies: 6Last Post: 12-01-2010, 01:25 AM -
How to Add a Counter to a Variable?
By Bbob in forum New To JavaReplies: 22Last Post: 06-05-2010, 03:14 PM -
Counter
By ks1615 in forum New To JavaReplies: 6Last Post: 02-20-2009, 03:02 AM -
Frequency Counter
By justlearning in forum New To JavaReplies: 0Last Post: 05-07-2008, 10:50 PM -
[SOLVED] BST Frequency Counter
By theonly in forum Advanced JavaReplies: 7Last Post: 04-29-2008, 11:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks