Results 1 to 6 of 6

Thread: how can I use conditions in switch statement

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    how can I use conditions in switch statement

    well?

    also something like this (I'm showing the VB example, hope someone can tell me how to do this in java )
    VB Code:
    1. Select case num
    2.        case is > 5
    3.              ' do something
    4.      
    5.        ' ......
    6. end select
    Last edited by MrPolite; Jul 6th, 2002 at 03:14 PM.

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Here is some code that i had which contains a switch statement.
    The main thing to remember when using switch statements is that the expression associated with it must have a byte, char, short or int value. The floting point and boolean types are not supported, and neither is long, even though long is an integer type.
    Code:
      import java.io.*; 
      import java.util.*;
      
     class VowelGrabber{ 
        public static void main(String[] args){
    
       try{
       File f = new File("C://Java/text.txt");
       BufferedReader buff = new BufferedReader(new FileReader(f));
       
       while(true){
           String x = buff.readLine();
            if(x == null) break; 
             x.toLowerCase(); 
              StringTokenizer st = new StringTokenizer(x); 
                while(st.hasMoreTokens()){
                   grabVowels(st.nextToken()); 
                }
        }
       }catch(IOException e){System.err.println(e);}
      }
       public static void grabVowels(String s){
          int numofvowels = 0; 
      
          for(int i = 0; i < s.length(); ++i){
           char c = s.charAt(i);  
           switch(c){
            case 'a': 
                numofvowels++;
                 break;
            case 'e': 
                numofvowels++;
                 break;
            case 'i': 
                numofvowels++;
                  break;
            case 'o': 
                numofvowels++;
                  break;
            case 'u': 
                numofvowels++;
                  break;
            }
          } 
         System.out.println(" String " + s + " encountered " + numofvowels + " vowels ");
         numofvowels = 0;      
       }
      }

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    tnx Dilenger4 for the reply, but I'm looking for something that does exactly the same thing as the VB code that I posted I know how to use the case the way you said it, but I want it to check for a condition, kinda like an if statement. see the example in the first post plz
    thank you

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I guess i would juast wrap up the switch statement in an if.
    Code:
      public class test{
       public static void main(String[] args){
          selectCase(12);
      }
      public static void selectCase(int i){
       if(i > 5){
        switch(i){     
          case 6: System.out.println("six"); 
          break; 
          case 7: System.out.println("seven"); 
          break; 
          default: 
          System.out.println("number is greater then 5 but not six or seven"); 
        }
       }
      }
     }

  5. #5

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    So basically Java doesnt support that? right?


    umm tnx for the help btw

  6. #6
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by MrPolite
    So basically Java doesnt support that? right?
    Correct...in Java, as well as C/C++ and other languages, the case statements must be a constant integral value. Example...
    Code:
    int a = 1, b = 1;
    
    switch (a)
    {
        case 1: //legal
    
        case 1.5: //illegal - not an integral
    
        case b: //illegal - not a constant
    }
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

Posting Permissions

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



Click Here to Expand Forum to Full Width