Page 1 of 2 12 LastLast
Results 1 to 10 of 12
  1. #1

    Default how to use a methods in a switch case? I need it so badly


    import java.util.Arrays;
    import java.util.*;


    public class menu
    {
    final static int robz = 5;
    static Scanner console = new Scanner(System.in);
    public static void main(String args[])
    {
    // int num[] = { 0 ,0 ,0 ,0} ;//new int [robz];
    //int num [] = new int [robz];
    int ans=0;
    char anser;

    int []test;
    test= new int[robz];

    int insert = 0;
    int j,x=0 ;

    int temp = test[3];
    test[4] = 66;
    //do

    //{
    int testLength = test.length;
    //int testLength = inttest.length;
    System.out.print("******** Menu********");
    System.out.println("Select the following number 1-5");
    System.out.println();
    System.out.println();
    System.out.println("[1]Intializing array");
    System.out.println();
    System.out.println("[2]Insert");
    System.out.println();
    System.out.println("[3]Display");
    System.out.println();
    System.out.println("[4]Sort");
    System.out.println();
    System.out.println("[5]Quit");
    //num[counter] = console.nextInt();


    System.out.println();
    System.out.println();

    System.out.println(" Press[1] Intializing [2] insert [3] Display [4] Sort [5] quit :");
    ans = console.nextInt();



    switch(ans)
    {


    case 1:{

    menu_case( test);

    System.out.println("Intializing array");

    System.out.print("enter a 5 numbers:");
    // testing
    //nameAndAddress();
    // for (int i=x ;i<test.length;i++)
    // test[i] = console.nextInt();


    //int a=0;

    //{
    // for (int z = a; z < test.length; z++)
    {
    System.out.print(test[i]);
    System.out.print(", ");
    }
    } break;


    case 2:
    public void menu_main(int[] test){

    System.out.println("Insert");
    }
    //System.out.print("enter a 5 numbers:");

    // testing
    // for (int i=x ;i<test.length;i++)
    // test[i] = console.nextInt();

    //test[0] = 77;
    //test[1] = 99;
    //test[2] = 44;
    //test[3] = 55;
    //test[4] = 22;
    //test[5] = 88;

    //insert = 5;



    //for(j=0; j<insert; j++)
    //System.out.print(test[j] + " ");


    //System.out.println("");


    break;

    case 3: System.out.print("Display");
    break;
    case 4: System.out.println("Sort");
    break;

    case 5: System.out.print("Quit");
    System.out.println();

    System.out.print("Please! Press [Q] in order for you to Quit this Menu ");

    // while(anser == 'Q')
    //{

    break;

    default:System.out.println("Your out of Range and You need to review your logic ");
    }
    }
    }

  2. #2
    you can't do that in Java or in any other language. you have to create a separate method to do something and call that method within your switch statements. example:

    Code:
    class Example {
    	public static void print() {
    	 //... do some printing
    	}
    
    	public static void solve() {
    	 //... do some solving
    	}
    
    	public static void main(String[] args) {
    	
    	   switch(ans) {
    		   case 1:
    			solve();
    			break;
    		   case 2:
    			print();
    			break;
    		   default:
    			// just nothing
    	   }
    	}
    }
    Last edited by aozora; 07-12-2010 at 06:38 PM. Reason: code corrections from silent-kill :)

  3. #3
    I tried to use it but It won't work man bro...
    Last edited by skeptic_rob; 07-11-2010 at 06:33 PM.

  4. #4
    Quote Originally Posted by skeptic_rob View Post
    I tried to use it but It won't work man bro...
    Is there any error / exception? Please post it.

  5. #5
    Quote Originally Posted by aozora View Post
    you can't do that in Java or in any other language. you have to create a separate method to do something and call that method within your switch statements. example:

    Code:
    class Example {
    	public void print() {
    	 //... do some printing
    	}
    
    	public void solve() {
    	 //... do some solving
    	}
    
    	public static void main(String[] args) {
    	
    	   switch(ans) {
    		   case 1:
    			solve();
    			break;
    		   case 2:
    			print();
    			break;
    		   default:
    			// just nothing
    	   }
    	}
    }
    you cant call solve(), break() because your main is static.

    better make solve() , break as static too.

    so you call it like Example.solve(), Example.break() etc.

  6. #6
    ^^ i wonder why TS does not have a problem with it. or does he even bother his problem from the start? hihi how rude.

    yup you are correct. thanks for the heads-up!!!

    will just declare solve() and print() static para ma call as it is sa main. i'm learning too in this thread too. haha

  7. #7
    murag sa db1 tingali ni cya

  8. #8
    mas maau bro i post ang error ngari ma kahibawo ta why dli mo run imong script. but try the solution of silent-kill he has a point.

  9. #9
    non-static method solve() cannot be referenced from a static context ang error sa solve(); ug print();

  10. #10
    @TS
    the best way to learn syntax (in any language) is to read up on it. there's just no substitute to reading.

    so here straight from the horses mouth...

    switch-case
    The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    static keyword
    Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    anyone who doesn't have a beginner book about java but want to learn it should start here.
    Trail: Learning the Java Language (The Java™ Tutorials)

  11.    Advertisement

Page 1 of 2 12 LastLast

Similar Threads

 
  1. How to Use ms access as database in C#
    By reyarita in forum Programming
    Replies: 3
    Last Post: 01-27-2010, 04:22 PM
  2. How To use the DLL's in VB.net
    By pitbull147 in forum Programming
    Replies: 3
    Last Post: 09-23-2008, 06:11 PM
  3. how to use the poledit in windows 98?
    By AMD_earl in forum Software & Games (Old)
    Replies: 2
    Last Post: 08-17-2006, 01:02 PM
  4. VIVO (video in, video out) HOW TO USE?
    By weedmeister in forum Computer Hardware
    Replies: 6
    Last Post: 05-08-2006, 08:56 AM
  5. how to use ADODC in Visual Basic
    By arbie_mon in forum Software & Games (Old)
    Replies: 0
    Last Post: 05-05-2006, 02:59 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
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top