Hi all, I have written a basic program with 3 option using a switch statement, the 2nd option being a reverse string code using charAt() when you select option 2 and press enter it then switchs to case 2: which then asks for the string you wish to reverse, is there a way to be able to enter the option and the string in one input line.

Any help will be greatly appreciated.

Sorry forgot to mention that this program runs via eclispe the modified version will run via command prompt.

Cheers.

Edited 6 Years Ago by hooky75: n/a

import java.util.Scanner;

public class menu 
{
    public void display_menu() 
    {
    	System.out.println("Menu: ");
		System.out.println("\n");
		System.out.println("   1.    Character pattern printing (using loops)");
		System.out.println("   2.    Reversing a string");
		System.out.println("   3.    Exit the program");
		System.out.println("\n");
		System.out.println("Select one of the options above: ");
    }
    {
	Scanner in = new Scanner(System.in);
        display_menu();
  
	switch (in.nextInt()) 
	{
	    case 1:
	    Scanner lines = new Scanner(System.in);
		System.out.print("Enter number of lines: ");
		int num = lines.nextInt();
		
		
		
		Scanner printchar = new Scanner(System.in);
		System.out.print("Enter character to be printed: ");
		String ch = printchar.next();
		
		

	 
		System.out.print( ch + " ");
		System.out.println("\n");
	 
		
		
	for( int a =1; a < num; a++ )  

	 {  

	    for( int b = 1; b <= a; b++ )  

	      {  

	         System.out.print( ch + " ");  
	         
	         if( a == b )  

	             System.out.print( ch + " ");  
	      }  

		System.out.println("\n");  

	  }    
			
	    new menu();
	    break;
  
	    case 2:
	    	Scanner line=new Scanner(System.in); 
	        System.out.print("Enter a line of text to be Reversed:"); 
	        String text = line.nextLine(); 
	        char forward; 
	        String rev =""; 
	        int a=text.length(); 
	        int b; 
	        for (b=a-1; b>=0; b--) 
	        { 
	        	forward=text.charAt(b); 
	            rev= rev+forward; 
	        } 
	      
	        System.out.println("\n" + rev + "\n"); 
	        
            new menu();
	        break;
  
	    case 3:
	    	System.out.println ( "Program terminated." );
	    	break;
		  

	    default:
	    System.err.println ( "Please enter a valid option." );
	    new menu();
	    
	}
    }
 
    public static void main (String[]args) 
    {
	new menu();
    }
}

So where you enter option 2 it then displays "Enter a line of text to be Reversed:" i would like to have this run from command line where you can type the file name the option and the string to be reversed then press enter it will not show the menu just show the string reversed.

actually, what you want to do, is to read an array of Strings (of which you'll transform the first one to an int)
it's not different to using command line parameters, if I understand your question correctly

This article has been dead for over six months. Start a new discussion instead.