Results 1 to 18 of 18

Thread: switch

  1. #1
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default switch

    Hello to everybody i ve this code:

    Java Code:
    switch(i){
            
                case 0:
                {
                    vuthoskopisi="kulo";
                    tonometrisi=10;
                    automati_perimetria=2;
                    gwnioskopia=2;
                    
                    illness = Asth[0];
                 break;
                }
                case 1:
                {
                    vuthoskopisi="kurto";
                    tonometrisi=10;
                    automati_perimetria=2;
                    gwnioskopia=2;
                    
                    illness= Asth[1];
                 break;   
                }
    }
    i want the variables tonometrisi automati_perimetria and gwnioskopia to be statement like
    (tonometrisi<10)&(tonometrisi>0)ect.
    When i put this statement in case the compiler saint that this sentence is not a statement.So what s the problem??:cool:

  2. #2
    VeasMKII's Avatar
    VeasMKII is offline Member
    Join Date
    Jan 2009
    Posts
    18
    Rep Power
    0

    Default

    Its exactly as the compiler says, your code isn't a statment :)

    You need it to do something with the two values, you can't simply state
    Java Code:
    (tonometrisi<10)&(tonometrisi>0)
    For example, it could be used in the if statement like this

    Java Code:
    if ((tonometrisi<10) && (tonometrisi>0)){
       //do something
    }
    Also, if you're planning to use the switch statement to select based on expressions i don't think it will work how you want.
    Type "select case java" into google and check out the first link, it shows you how switches work :)

    You may want to consider using else if
    Java Code:
    if (val1 < val2) {
        //do something
    }else if (val1 = val2){
        //do something else
    }else if (val1 > val2){
        //do something else
    }
    Last edited by VeasMKII; 01-29-2009 at 12:03 PM.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by dj kourampies View Post
    i want the variables tonometrisi automati_perimetria and gwnioskopia to be statement like
    (tonometrisi<10)&(tonometrisi>0)ect.
    When i put this statement in case the compiler saint that this sentence is not a statement.So what s the problem??:cool:
    Can you show what's the exact statement you have added in your code?

  4. #4
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    This is the class named iatreio(means surgery) i have variables kinds of checkup and in the method diagnwsi i want according to the ammount of the variables
    to have as return string the name of illness(the variables is in greek).

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package ofthalmiatreio;
    import java.io.*;
    
    /**
     *
     * @author kris
     */
    public class iatreio {
        
        private String vuthoskopisi;
        private int tonometrisi;
        private int automati_perimetria;
        private int gwnioskopia;
        
        private String [] Astheneia;
        
        public iatreio(){
        
           vuthoskopisi="null";
           tonometrisi=0;
           automati_perimetria=0;
           gwnioskopia=0;
           String [] Astheneia={"null"};
        }
        public iatreio(String vuth, int ton, int per, int gwn,  String [] asth){
        
            this.vuthoskopisi=vuth;
            this.tonometrisi=ton;
            this.automati_perimetria=per;
            this.gwnioskopia=gwn;
            this.Astheneia=asth;
        }
        public void setVuthoskopisi(String vuth){
        
            this.vuthoskopisi=vuth;
        }
        public String getVuthoskopisi(){
        
            return vuthoskopisi;
        }
        public void setTonometrisi(int ton){
        
            this.tonometrisi=ton;
        }
        public int getTonometrisi(){
        
            return tonometrisi;
        }
        public void setAutomatiPerimetria(int per){
        
            this.automati_perimetria=per;
        }
        public int getAutomatiPerimetria(){
        
            return automati_perimetria;
        }
        public void setGwnioskopia(int gwn){
        
            this.gwnioskopia=gwn;
        }
        public int getGwnioskopia(){
        
            return gwnioskopia;
        }
        public void  SetAstheneia(String [] asth){
        
            this.Astheneia=asth;
        }
    
        public String diagnwsi(String vuth, int ton, int aut, int gwn) throws IOException{
        
          
            this.vuthoskopisi=vuth;
            this.tonometrisi=ton;
            this.automati_perimetria=aut;
            this.gwnioskopia=gwn;
            
            String [] Asth={"mywpia", "upermetrwpia","glaukwma","katarraktis","ogkos","epipefykitida"};
            String illness="den exeis apolutws tipota";
            int j;
            
            int i;
           for( i=0;i<6;i++){
               
            switch(i){
            
                case 0:
                {
                    vuthoskopisi="kulo";
                    tonometrisi=10;
                    automati_perimetria=2;
                    gwnioskopia=2;
                    
                    illness = Asth[0];
                 break;
                }
                case 1:
                {
                    vuthoskopisi="kurto";
                    tonometrisi=10;
                    automati_perimetria=2;
                    gwnioskopia=2;
                    
                    illness= Asth[1];
                 break;   
                }
                case 2:
                {
                    vuthoskopisi="kulo";
                    tonometrisi=20;
                    automati_perimetria=4;
                    gwnioskopia=4;
                    
                    illness= Asth[2];
                 break;   
                }
                case 3:
                {
                    vuthoskopisi="kurto";
                    tonometrisi=20;
                    automati_perimetria=4;
                    gwnioskopia=4;
                    
                    illness=Asth[3];
                 break;   
                }
                case 4:
                {
                    vuthoskopisi="kulo";
                    tonometrisi=40;
                    automati_perimetria=10;
                    gwnioskopia=8;
                    
                    illness=Asth[4];
                 break;   
                }
                case 5:
                {
                    vuthoskopisi="kurto";
                    tonometrisi=40;
                    automati_perimetria=10;
                    gwnioskopia=8;
                    
                    illness=Asth[5];
                 break;   
                }
            }
           }
            System.out.println("To apotelesma tis exetasis einai: "+illness);
            
            return illness;
        }
        public static void main(String [] atgs)throws IOException{
        
            iatreio a=new iatreio();
            a.diagnwsi("kurto",40,10,6);
            
        }
    }

  5. #5
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    i cant do what i want with if because i cant put 2 boolean expressions inside if.:(

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default hhhmmm...

    ...i cant put 2 boolean expressions inside if.
    I don't understand you're problem. I thought you wanted what VeasMKII posted:
    Java Code:
    if ((tonometrisi<10) && (tonometrisi>0)){
       //do something
    }
    The above will be true if the tonometrisi variable is between 1-9. Do you need something else?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    (tonometrisi<10)&(tonometrisi>0)
    you need an extra &. so its &&

    VeasMKII posted the code for you already. check it out.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  8. #8
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    ok i want tonometrisi, gwnioskopia, automati_perimetria to be statement for example {(tonometrisi<10)&&(tonometrisi>0)} &&{(gwnioskopia<5)&&(gwnioskopia>0)}&&{(automati_p erimetria<10)&&(automati_perimetria>0)}. When i use more tha one boolean expressions in if it doesnt work.

  9. #9
    VeasMKII's Avatar
    VeasMKII is offline Member
    Join Date
    Jan 2009
    Posts
    18
    Rep Power
    0

    Default

    If you want to chain that many ANDs it shouldn't be a problem, but you need to remove those closing bracers and surround the statment with a final container:
    Java Code:
    if ((tonometrisi<10) && (tonometrisi>0) && 
            (gwnioskopia<5) && (gwnioskopia>0) && 
            (automati_p erimetria<10) && (automati_perimetria>0))
    {
        //code here
    }
    Last edited by VeasMKII; 01-30-2009 at 11:52 AM.

  10. #10
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    ok thank tou guys it work.:)

  11. #11
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    ok i find another one problem with if. I want to put inside if another one expression (String vuthoskopisi="Kulo") or ( String vuthoskopisi="kurto") when i put this expression inside if with the others tonometrisi gwnioskopia it said : operator && cannot applied to java.lang.String.boolean

  12. #12
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Please post the code you are woriking with. Are you trying to compare two strings? Are you trying to assign a value to a string?

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  13. #13
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    I want to have 4 boolean expressions(4 variables) inside if. My variables is String vuthoskopisi it takes 2 rates "kulo"and "kurto"
    , int tonometrisi from 0to10 in the first if in the second(else if ) it takes rates from 10to20 and tin 3rd from 20to 30 ect. Also the variables gwnioskopia and automati_perimetria take rates from xto y in every if .

  14. #14
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Stilll not understanding...

    Is this what you what to do?
    Java Code:
    if ((tonometrisi<10) && (tonometrisi>0) && 
            (gwnioskopia<5) && (gwnioskopia>0) && 
            (automati_p erimetria<10) && (automati_perimetria>0))
    {
       vuthoskopisi="kulo";
       tonometrisi=10;
       automati_perimetria=2;
       gwnioskopia=2;
       illness = Asth[0];
    }
    I suggest you look at the following link about the "if" statement or post the code you are trying to make work:
    The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  15. #15
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    inside the first if statements i want to put the variable vuthoskopisi which has two cases the first is "kulo"and the second is "kurto" not in then statements.

  16. #16
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    this is the code:

    [CODE]

    public String diagnwsi(String vuth, int ton, int aut, int gwn) throws IOException{


    this.vuthoskopisi=vuth;
    this.tonometrisi=ton;
    this.automati_perimetria=aut;
    this.gwnioskopia=gwn;

    String [] Asth={"mywpia", "upermetrwpia","glaukwma","katarraktis","ogkos","e pipefykitida"};
    String illness="dn exeis tipota";


    if (
    (vuthoskopisi="kulo") &&
    (tonometrisi<=10) && (tonometrisi>=0) &&
    (gwnioskopia<=5) && (gwnioskopia>=0) &&
    (automati_perimetria<=10) && (automati_perimetria>=0))

    {
    illness=Asth[0];
    }
    else if (
    (vuthoskopisi="kulo") &&
    (tonometrisi<=20) && (tonometrisi>10) &&
    (gwnioskopia<=10) && (gwnioskopia>5) &&
    (automati_perimetria<=20) && (automati_perimetria>10))
    {
    illness=Asth[1];
    }
    else if (
    (vuthoskopisi="kulo") &&
    (tonometrisi<10) && (tonometrisi>0) &&
    (gwnioskopia<=15) && (gwnioskopia>10) &&
    (automati_perimetria<=30) && (automati_perimetria>20))
    {
    illness=Asth[2];
    }

    else if (
    (vuthoskopisi="kurto") &&
    (tonometrisi<=10) && (tonometrisi>=0) &&
    (gwnioskopia<=5) && (gwnioskopia>=0) &&
    (automati_perimetria<=10) && (automati_perimetria>=0))

    {
    illness=Asth[3];
    }
    else if (
    (vuthoskopisi="kurto") &&
    (tonometrisi<=20) && (tonometrisi>10) &&
    (gwnioskopia<=10) && (gwnioskopia>5) &&
    (automati_perimetria<=20) && (automati_perimetria>10))
    {
    illness=Asth[4];
    }
    else if (
    (vuthoskopisi="kurto") &&
    (tonometrisi<10) && (tonometrisi>0) &&
    (gwnioskopia<=15) && (gwnioskopia>10) &&
    (automati_perimetria<=30) && (automati_perimetria>20))
    {
    illness=Asth[5];
    }
    System.out.println("To apotelesma einai:" +illness);
    return illness;
    }
    [\CODE]

  17. #17
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default ah... OK...

    OK, you want to compare string variable (you can't assign variables in the if statement). When you compare strings, you have to use the equals() method:
    Java Code:
    if ( 
    (vuthoskopisi.[COLOR="blue"][B]equals("kulo") [/B][/COLOR]&&
    (tonometrisi<=10) && (tonometrisi>=0) && 
    (gwnioskopia<=5) && (gwnioskopia>=0) && 
    (automati_perimetria<=10) && (automati_perimetria>=0))
    
    {
    illness=Asth[0];
    }
    else if (
    (vuthoskopisi[B][COLOR="blue"].equals("kulo")[/COLOR][/B] &&
    (tonometrisi<=20) && (tonometrisi>10) && 
    (gwnioskopia<=10) && (gwnioskopia>5) && 
    (automati_perimetria<=20) && (automati_perimetria>10))
    {
    illness=Asth[1];
    }
    .
    .
    .
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  18. #18
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

Similar Threads

  1. [SOLVED] Should I use switch or if else
    By hungdukie in forum New To Java
    Replies: 14
    Last Post: 12-25-2011, 01:49 AM
  2. switch
    By dj kourampies in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 08:46 AM
  3. Switch help please!!!!
    By soc86 in forum New To Java
    Replies: 6
    Last Post: 11-23-2008, 07:25 PM
  4. What is wrong with my switch??
    By soc86 in forum New To Java
    Replies: 3
    Last Post: 11-20-2008, 12:34 AM
  5. How to use Switch keyword
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:07 PM

Posting Permissions

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