Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java String Buffer

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java String Buffer

    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
    Last edited by jps; October 15th, 2012 at 12:41 PM. Reason: code format repairs


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Java String Buffer

    Please use [code=java] for the syntax highlights, and I corrected the indents for you this time around just to make it easier for my eyes to help you. This is something you will be expected to maintain in future posts.



    How can I use a buffer to store a string read from the keyboard?
    Search keywords "java scanner class" and "java buffered reader" for the named classes, tutorials, and sample code. Plenty available to learn from in this category. After some history here, this assignment should be easier.



    Do you understand what a buffer is and how it is used? This idea of reading a string of given length and storing it in a buffer to be used later on? I don't understand what part you need help with. Can you ask a more specific question? I am trying to find a way to push you in the right direction without writing the code for you.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java String Buffer

    Thank you for the tips. Where am I supposed to use code=java?

    A buffer is to be used to store chucks of data as strings all at once and then be used again later, correct? I'm having difficulties with storing this data as a buffer to be able to use it again later in the code. I have yet to be able to append, delete to the buffer. In addition, since I am using a switch, I need to be able to display the string. I'm not sure how to do that since there are different buffers stored in case 1 and case 2.

    The buffer stored in case 1 is supposed to be selected by the user interactively. The buffer in case 2 is supposed to be stored randomly based on the length provided by the user. How am I supposed to display the string buffer in Case 1 and Case 2 respectively?

  4. #4
    Junior Member
    Join Date
    Dec 2012
    Location
    United Kingdom
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java String Buffer

    StringBuffer class is a mutable class unlike the String class which is immutable. Both the capacity and character string of a StringBuffer Class. StringBuffer can be changed dynamically. String buffers are preferred when heavy modification of character strings is involved (appending, inserting, deleting, modifying etc).
    Strings can be obtained from string buffers. Since the StringBuffer class does not override the equals() method from the Object class, contents of string buffers should be converted to String objects for string comparison.
    A StringIndexOutOfBoundsException is thrown if an index is not valid when using wrong index in String Buffer manipulations.

Similar Threads

  1. java buffer problem florida tech
    By computerbuff in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 12th, 2011, 09:40 PM
  2. [SOLVED] difference between String Concatenation and String -Buffer/Builder .append(<value>)
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2011, 08:16 AM
  3. Java Frame Buffer Line drawing
    By tcmei in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 17th, 2011, 06:55 AM
  4. PLEASE HELP ME with double buffer
    By DouboC in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2011, 05:32 PM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM