Results 1 to 9 of 9
  1. #1
    anonb is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default Java Help in storing a Buffer

    Need help adding a buffer. Please see below:

    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.

    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    
    public class simulator {
        
       public static void main (String[] args)
        {
             
        int str_len=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;
        
        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

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Java Help in storing a Buffer

    Sounds like a homework dump. Please ask specific questions.

  3. #3
    anonb is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default Re: Java Help in storing a Buffer

    So how can I get case 2 see the string in case 1 and so forth?

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Java Help in storing a Buffer

    When I read the #2 description, it implies to me that the string should be generated randomly and not given by the user in step 1. The only thing the user does is specify the length.

  5. #5
    anonb is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default Re: Java Help in storing a Buffer

    ok but case 3, says to display the current string. I have yet to figure that out. I cannot display the current string in 1 or 2 because the strings are local to those cases and cannot be accessed outside of those cases..

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Java Help in storing a Buffer

    Do they have to be local?

  7. #7
    anonb is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default Re: Java Help in storing a Buffer

    No they do not have to be local.

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Java Help in storing a Buffer

    Then make them instance variables. Then you can always retrieve whatever the current state of the variable is.

  9. #9
    anonb is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default Re: Java Help in storing a Buffer

    OK, I will try tonight once I get home.

Similar Threads

  1. JAVA storing objects
    By santa in forum New To Java
    Replies: 24
    Last Post: 02-24-2012, 02:53 PM
  2. Java Sound Buffer Size
    By morris4019 in forum New To Java
    Replies: 0
    Last Post: 06-10-2011, 12:51 AM
  3. java rmi buffer error
    By ans2104 in forum Networking
    Replies: 1
    Last Post: 03-06-2010, 12:09 AM
  4. Storing Images into java through beans
    By LovJava in forum New To Java
    Replies: 7
    Last Post: 02-12-2010, 09:13 AM
  5. Replies: 2
    Last Post: 08-27-2008, 03:24 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
  •