i have a problem in my code when i go to the method add i successfully go there but i cant go back to the main method someone who can help about this.
sorry for my bad english.
here is the code:

package project;

import java.util.*;
import java.lang.String;




/**
 *
 * @author students
 */
public class Main {
    private static int status;
    private static char yn;
    private static char more;

 
    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("Enter Choice");
        System.out.println("1: Add");
        System.out.println("2: Delete");
        System.out.println("3: Edit");
        System.out.println("4: Exit");
        System.out.println("");
        Scanner sc=new Scanner(System.in);
        int choice=sc.nextInt();
        switch (choice)
        {
            case 1:
                Add();
                break;
            case 2:
                Delete();
                break;
            case 3:
                Edit();
                break;
            case 4:
                Exit();
                break;
        }
       
                
        
    }
// add item
    private static void Add() {
        int counter=0;
        int[] array;
      array=new int[counter];
       Scanner sc=new Scanner(System.in);
       System.out.println("Name: ");
            String name=sc.next();
       System.out.println("Add.: ");
            String add=sc.next();              
             sc.close();
    }
// delete item
    private static void Delete() {
        throw new UnsupportedOperationException("Not yet implemented");
    }
//edit item
    private static void Edit() {
        throw new UnsupportedOperationException("Not yet implemented");
    }
//exit
    private static void Exit() {
        System.exit(status);
    }

}

Recommended Answers

All 8 Replies

Your program ends after Add because when it returns to main, there is nothing left after the switch statement. From the looks of it, you want the program to keep executing unless someone selects the Exit option. You should enclose the functionality of your main method within a loop that will execute as long as the user does not select the Exit option.

indeed, you'll need a loop to keep your program running if you want it to repeat, but it is best (and easier to read) if you put your menu and the accepting of input in a seperate method, which you call when needed.

for instance:

package project;


      import java.util.*;
      import java.lang.String;


      /**
       *
       * @author students
       */


      public class Main {

      private static int status;
      private static char yn;
      private static char more;
       

      public static void main(String[] args) {
      // TODO code application logic here

      int choice= getInput();

      while ( choice != 4 ){
      switch (choice)
      {
      case 1:
        Add();
        break;
      case 2:
        Delete();
        break;
      case 3:
        Edit();
        break;
      case 4:
        Exit();
        break;
      }
      choice = getInput();
      }


      }
      // NEW METHOD
      private static int getInput(){
      System.out.println("Enter Choice");
      System.out.println("1: Add");
      System.out.println("2: Delete");
      System.out.println("3: Edit");
      System.out.println("4: Exit");
      System.out.println("");
      Scanner sc=new Scanner(System.in);
      return sc.nextInt();
     }

      // add item
      private static void Add() {
      int counter=0;
      int[] array;
      array=new int[counter];
      Scanner sc=new Scanner(System.in);
      System.out.println("Name: ");
      String name=sc.next();
      System.out.println("Add.: ");
      String add=sc.next();
      sc.close();
      }
      // delete item
      private static void Delete() {
       throw new UnsupportedOperationException("Not yet implemented");
       }
       //edit item
       private static void Edit() {
       throw new UnsupportedOperationException("Not yet implemented");
       }
       //exit
       private static void Exit() {
       System.exit(status);
       }
        
      }
package project;


      import java.util.*;
      import java.lang.String;


      /**
       *
       * @author students
       */


      public class Main {

      private static int status;
      private static char yn;
      private static char more;
       

      public static void main(String[] args) {
      // TODO code application logic here

      int choice= getInput();

      while ( choice != 4 ){
      switch (choice)
      {
      case 1:
        Add();
        break;
      case 2:
        Delete();
        break;
      case 3:
        Edit();
        break;
      case 4:
        Exit();
        break;
      }
      choice = getInput();
      }


      }
      // NEW METHOD
      private static int getInput(){
      System.out.println("Enter Choice");
      System.out.println("1: Add");
      System.out.println("2: Delete");
      System.out.println("3: Edit");
      System.out.println("4: Exit");
      System.out.println("");
      Scanner sc=new Scanner(System.in);
      return sc.nextInt();
     }

      // add item
      private static void Add() {
      int counter=0;
      int[] array;
      array=new int[counter];
      Scanner sc=new Scanner(System.in);
      System.out.println("Name: ");
      String name=sc.next();
      System.out.println("Add.: ");
      String add=sc.next();
      sc.close();
      }
      // delete item
      private static void Delete() {
       throw new UnsupportedOperationException("Not yet implemented");
       }
       //edit item
       private static void Edit() {
       throw new UnsupportedOperationException("Not yet implemented");
       }
       //exit
       private static void Exit() {
       System.exit(status);
       }
        
      }

is working as expected?

is working as expected?

and your point is???

Basically I think what is happening is this: you have a switch statement with the user's choice -- say they select choice 1. Then it goes to case 1. Case 1 has 2 commands: Add(); and break; First Add() is executed. When add() is finished, the second command is executed (break), which breaks out of the switch statement. Then the end of the main method is reached and your program ends.

As I understand it, you want the user to have the capability to continue to select options until the exit option is selected. How about using this syntax?:

public static void main(String[] args) {
        boolean exit = false;
        do {
            System.out.println("Enter Choice");
            System.out.println("1: Add");
            System.out.println("2: Delete");
            System.out.println("3: Edit");
            System.out.println("4: Exit");
            System.out.println("");
            Scanner sc=new Scanner(System.in);
            int choice=sc.nextInt();
            switch (choice)
            {
                case 1:
                    Add();
                    break;
                case 2:
                    Delete();
                    break;
                case 3:
                    Edit();
                    break;
                case 4:
                    exit = true;
                    break;
            }
        }
        while (!exit);
    }

Basically what you are doing here is creating an looping system that will return the user back to the top of your code unless they choose option 4, which will set the boolean exit variable to true, enabling you to break from the loop. Does this help?

i mean that when i select choose 1 it go in case 1 in add method

// add item
private static void Add() {
Scanner sc=new Scanner(System.in);
System.out.println("Name: ");
String name=sc.next();
System.out.println("Add.: ");
String add=sc.next();
sc.close();

then the user enter the name and address after that when it finished it back to the main method when the user can choose another method either in edit, delete, or exit method.

Yep -- refer to the above syntax -- it uses your capability. You need to stick your switch statement in a loop as shown above.

i think you did not understand me my problem is how i can call the main method not tp loop it. i revised my code.

package project;
import java.util.*;
//import java.lang.String;



/**
 *
 * @author students
 */
public class Main {
    private static int status;

 
    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("Enter Choice");
        System.out.println("1: Add");
        System.out.println("2: Delete");
        System.out.println("3: Edit");
        System.out.println("4: Exit");
        System.out.println("");
        Scanner sc=new Scanner(System.in);
        int choice=sc.nextInt();
        switch (choice)
        {
            case 1:
                Add();
                break;
            case 2:
                Delete();
                break;
            case 3:
                Edit();
                break;
            case 4:
                Exit();
                break;
        }
       
                
        
    }
// add item
    private static void Add() {
       Scanner sc=new Scanner(System.in);
       System.out.println("Name: ");
            String name=sc.next();
       System.out.println("Add.: ");
            String add=sc.next();              
             sc.close();
           
    }
// delete item
    private static void Delete() {
        throw new UnsupportedOperationException("Not yet implemented");
    }
//edit item
    private static void Edit() {
        throw new UnsupportedOperationException("Not yet implemented");
    }
//exit
    private static void Exit() {
        System.exit(status);
    }

}

i created four methods the add();, delete();,edit();,and exit(); i dont know if it should private or public. now when i run the program it will ask first for a choice for tthe case, now when i choose 1 it goes to case 1 and it will got to add(); method then it will ask for the name and add.
after that the program was terminated, but what i want is that when it entering the name and the add. it will back to the main method and ask again for a choice in the main method

public static void main(String[] args)

so i can choice another method either of the four.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.