How can I use a buffer to store a string read from the keyboard? Also, I am using a case switch statement in which I cannot view the users input outside of each case scenerio. Here is what this part is supposed to do. Everything works except for Im not making it read input as a string and store in a buffer. That is what I need help with. Please see below:
0 – Exit
Will exit the program
1 – Read reference string
A reference string will be read from the keyboard and stored in a buffer. Each value of the reference string will be verified and validated (or rejected).
The user will be first asked to enter the length of the reference string, and then the string itself will be entered.
2 – Generate reference string
A reference string will be randomly generated; the length of the reference string will be given by the user interactively. The string will be stored in a buffer.
Using options 1 and 2 more than once will result in overwriting the old reference string.
3 – Display current reference string
Will display the stored reference string; if there is no reference string stored yet, an error message will be displayed.
public class simulator { public static void main (String[] args) { int str_len; //int str=0; //StringBuilder buffer = new StringBuilder(str_len); Scanner input = new Scanner(System.in); int choice; choice=menu(input); do { switch (choice) { //Exits Program case 0: System.out.println("This program will exit!"); System.exit(0); break; //Enter length/reference value of string case 1: //simulator test= new simulator(); System.out.println("Please enter length of string "); str_len=input.nextInt(); System.out.println("Please enter reference string "); for (int i=0; i<=str_len-1;i++) { int[] count = new int[str_len]; count[i] = input.nextInt(); if (count[i]<0||count[i]>9) { System.out.println(count[i] + " is Rejected"); }//ends if else if (count[i]>0||count[i]<9) { System.out.println(count[i] + " is Validated"); //StringBuilder validate = new StringBuilder(count); }//ends if }//ends for break; //choice=menu(input); //Enter length/create random string case 2: //int str_len; //simulator test2= new simulator(str_len3); System.out.println("Please enter length of string "); str_len=input.nextInt(); //Math.random(); Random random = new Random(); for (int i=1; i<=str_len; i++) { System.out.println(random.nextInt(15)); }//ends for //StringBuilder validate2 = new StringBuilder(str_len); //System.out.println("validate is " + buffer); break; //display reference string case 3: break; }//ends switch choice=menu(input); } while(choice!=0); }//ends main //menu public static int menu(Scanner input) { int n; System.out.println("MENU"); System.out.println("0 - Exit "); System.out.println("1 - Read reference string "); System.out.println("2 - Generate reference string "); System.out.println("3 - Display current reference string "); n=input.nextInt(); return n; }//ends menu }//ends class