Results 1 to 2 of 2
  1. #1
    erin.ctm is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default 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++;
    }}}

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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

  1. Increment Counter for ID
    By blkshrk81 in forum New To Java
    Replies: 6
    Last Post: 12-01-2010, 02:25 AM
  2. How to Add a Counter to a Variable?
    By Bbob in forum New To Java
    Replies: 22
    Last Post: 06-05-2010, 04:14 PM
  3. Counter
    By ks1615 in forum New To Java
    Replies: 6
    Last Post: 02-20-2009, 04:02 AM
  4. Frequency Counter
    By justlearning in forum New To Java
    Replies: 0
    Last Post: 05-07-2008, 11:50 PM
  5. [SOLVED] BST Frequency Counter
    By theonly in forum Advanced Java
    Replies: 7
    Last Post: 04-30-2008, 12:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •