#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1

    Obtaining Additional Value in Byte_Array for some reason


    Hello,

    So in the Java book I have by Ivor Horton, I'm currently learning on how to extract characters from a mutable string using charAt() and getChars().

    The author then mentioned a second route, which he didn't really follow with an example but I figured I would follow his reasoning to maybe learn an alternate way of obtaining characters from a string in Java.

    Anyway, so the reasoning is as follows:

    Obtain a String object from a CharBuffer object by calling its toString() method, then use the getBytes() method for the String object to obtain the byte[] array that will correspond to that StringBuffer object

    My main problem lies within the very first line of my code. I followed similar examples that I found through various documentation, so I'm pretty sure the first line lies within my misunderstanding of just how CharBuffer objects work.

    Here's my code:

    Code:
    CharBuffer char_s = new Character('s');
    		
    		// make a String variable called y that will convert the value from
    		// the char_s CharBuffer object into a string using the .toString() method
    		
    		String y = char_s.toString();
    		
    		System.out.println("Here is the string form of the char_s CharBuffer object: " + y ); 
    		
    		// getBytes(): This method is used to obtain the byte[] 
    		// array corresponding to the StringBuffer object. 
    		
    		// Syntax for getBytes() method: 
    		// public byte[] getBytes(String charsetName) throws 
    		// UnsupportedEncodingException 
    		
    		// OR: public byte [] getBytes()
    		
    		byte[] byte_array = y.getBytes();
    		
    		// Make a for loop that will now print out each byte value
    		// of the byte_array:
    		
    		for (int i = 0; i < byte_array.length; i++)
    		{
    			System.out.print(byte_array[i]);
    			
    		}
    What I want to say by that first line to the program is that, "Hey create a new CharBuffer object that will allocate memory for just a single character"

    I came to the new Character() idea through the intellisence feature on my Eclipse Java compiler, so obviously its wrong...

    I would refer to my book, but there's no example in this context, hence why I'm asking in the first place.

    Thanks for reading my post and have a good one!
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Do you get any compiler errors when you compile the posted code?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Originally Posted by NormR
    Do you get any compiler errors when you compile the posted code?
    Yes, on Line 98, its how I created the CharBuffer object, obviously I'm trying to create a new instance of a CharBuffer object using the constructor... but obviously I'm not doing the implementation correctly.

    Any ideas on how to correct Line 98?

    Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    
    Type mismatch: cannot convert from Character to CharBuffer at ExtractingCharactersFromAMutableString.main(ExtractingCharactersFromAMutableString.java:98)
    Here's my total code as to pinpoint Line 98 before (I think I cut it off since I usually include similar examples in the same program to learn one new topic at a time):

    Code:
    import java.nio.CharBuffer;
    
    
    public class ExtractingCharactersFromAMutableString 
    {
    	
    	public static void main(String[] args)
    	{
    		// charAt(): This method extracts the character at a given index position. 
    		
    		// The syntax for charAt() is as follows:
    		
    		// public char charAt(int index)
    		
    		// index: obviously the index of the character you want to find
    		
    		
    		// This is just an example string that I made to extract the first
    		// character from using the .charAt() method:
    		String example_string = "What a nice day in paradise!";
    		
    		// This will use the .charAt() method to extract the first character
    		// of the example_string:
    		char first_character = example_string.charAt(0);
    		
    		System.out.println("Here is the contents of the example_string String object: \n" + example_string);
    		
    		System.out.println("Here is the first character of the example_string with the .charAt() method: \n" + first_character);
    		
    	
    		System.out.println("Now entering the getChars() portion of the program: ");
    		
    		
    		// getChars(): This method extracts a range of characters and stores 
    		// them in a char array. 
    		
    		// Syntax for getChars() method:
    		
    		// public void getChars(int srcBegin,
            // int srcEnd,
            // char[] dst,
            // int dstBegin)
    		
    		// So it specifies the index values for the start and stop of whatever
    		// section of the string you want to get the characters from
    		
    		// It then specifies whatever character array you want to store this 
    		// result in, as well as the index element of that array where you want
    		// to begin to store those characters
    		
    		// Example of getChars() method:
    		
    		String example_string2 = new String("Wow, what a cool way to surf!");
    		
    		System.out.println("Here is the length of the example_string2: " + example_string2.length());
    		
    		char[] destination_char_array = new char[29];	
    		
    		// Call the getChars() method using the example_string2 String object:
    		
    		example_string2.getChars(0, 20, destination_char_array, 0);
    		
    		// Print out the contents of the example_string2:
    		
    		System.out.println("Here are the contents inside the example_string2 String object: \n" + example_string2);
    		
    		// Make a for loop that will print out the values of the destination_char
    		// _array:
    		
    		System.out.println("Here is the contents of the destination_char_array: ");
    		
    		for (int i = 0; i < 20; i++)
    		{
    			System.out.print(destination_char_array[i]);
    			
    		}
    		
    		// SUCCESS!
    		
    		
    		// toString(): This method allows you to obtain a String object 
    		// from a CharBuffer object by calling its toString() method and 
    		// then you can use the getBytes() for the string object in order 
    		// to obtain the byte[] array corresponding to the StringBuffer object.
    		
    		// Syntax for toString():
    		// String toString()
    		// static String toString(int i)
    		
    		// Example of using toString():
    		
    		System.out.println("Now entering the toString portion of the program: ");
    		
    		// In order to use this toString() method, you have to specify the
    		// data type with a capital, as well as its full name for the integer
    		// variable in question:
    		
    		CharBuffer char_s = new Character('s');
    		
    		// make a String variable called y that will convert the value from
    		// the char_s CharBuffer object into a string using the .toString() method
    		
    		String y = char_s.toString();
    		
    		System.out.println("Here is the string form of the char_s CharBuffer object: " + y ); 
    		
    		// getBytes(): This method is used to obtain the byte[] 
    		// array corresponding to the StringBuffer object. 
    		
    		// Syntax for getBytes() method: 
    		// public byte[] getBytes(String charsetName) throws 
    		// UnsupportedEncodingException 
    		
    		// OR: public byte [] getBytes()
    		
    		byte[] byte_array = y.getBytes();
    		
    		// Make a for loop that will now print out each byte value
    		// of the byte_array:
    		
    		for (int i = 0; i < byte_array.length; i++)
    		{
    			System.out.print(byte_array[i]);
    			
    		}
    	
    	}
    
    }
    Last edited by HauntJemimah; October 20th, 2013 at 01:30 PM.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    cannot convert from Character to CharBuffer
    Look at the API doc for the CharBuffer class to see how to create an instance of the class with a given contents.


    Your code uses a Character class constructor which returns a Character object, not a CharBuffer object so it can't be assigned to a CharBuffer variable.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Oh okay, good idea about the documentation.

    I'm using this link:

    http://docs.oracle.com/javase/7/docs/api/java/nio/CharBuffer.html

    Anyway, so I looked through several of the functions, and the only relevant one to this situation would be to maybe store that single character into a char[] array using the put() function.

    The format is as follows:

    public CharBuffer put(char[] src,
    int offset,
    int length)


    Here's my implementation in the program, but obviously its giving errors...

    I tried placing the 's' character into a separate character variable. I then created a character array that will basically only hold 1 character (kind of unnecessary actually, because why would I create an array that only holds 1 element... ah... this is flawed)

    I then tried using the put() function to place the character from s into the s_array:

    Here's my code:

    Code:
    public class ExtractingCharactersFromAMutableString 
    {
    
    public static void main(String [] args)
    {
    
    char s = 's';
    		
    		char s_array [];
    		
    		public CharBuffer get(s_array, 0, 1);
    		
    		
    		// make a String variable called y that will convert the value from
    		// the char_s CharBuffer object into a string using the .toString() method
    		
    		String y = char_s.toString();
    		
    		System.out.println("Here is the string form of the char_s CharBuffer object: " + y ); 
    		
    		// getBytes(): This method is used to obtain the byte[] 
    		// array corresponding to the StringBuffer object. 
    		
    		// Syntax for getBytes() method: 
    		// public byte[] getBytes(String charsetName) throws 
    		// UnsupportedEncodingException 
    		
    		// OR: public byte [] getBytes()
    		
    		byte[] byte_array = y.getBytes();
    		
    		// Make a for loop that will now print out each byte value
    		// of the byte_array:
    		
    		for (int i = 0; i < byte_array.length; i++)
    		{
    			System.out.print(byte_array[i]);
    			
    		}
    		
    		// Why is it obtaining the value 53?: Big Question
    		// Wouldn't it just have the value 5 stored from the integer variable
    		// x?
    
    }
    
    }
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    What happens when you try to compile and execute the code?
    If you get any error messages you need to copy the full text and paste it here.

    Hint: When working on finding the correct syntax and technique for a new class and/or methods, write a small testing program of 10 to 20 lines just for testing. Don't try to embed the new stuff in the middle of a much larger program until you get the small test program to work. Also its easier to post a small test program here to get help.
    Last edited by NormR; October 21st, 2013 at 02:53 PM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    This is the error I'm getting, I think it has to deal with the data type I'm calling in to the get() function, and that it should be an integer? (I'm not sure)

    Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	Illegal modifier for parameter get; only final is permitted
    	Syntax error on token "(", = expected
    	The method s_array(int, int) is undefined for the type ExtractingCharactersFromAMutableString
    	Syntax error on token ",", ( expected
    	char_s cannot be resolved
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    What source line(s) is/are those error messages for? It looks like there are several compiler errors but the IDE's error messages seems to hide some important information.

    Here is a sample of an error message from the java compiler.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    Code:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    It's on Line 104 apparently, here is my code, ignore the top because it involves another example from my book:

    Code:
    import java.nio.CharBuffer;
    
    import org.omg.CORBA.PUBLIC_MEMBER;
    
    
    public class ExtractingCharactersFromAMutableString 
    {
    	
    	public static void main(String[] args)
    	{
    		// charAt(): This method extracts the character at a given index position. 
    		
    		// The syntax for charAt() is as follows:
    		
    		// public char charAt(int index)
    		
    		// index: obviously the index of the character you want to find
    		
    		
    		// This is just an example string that I made to extract the first
    		// character from using the .charAt() method:
    		String example_string = "What a nice day in paradise!";
    		
    		// This will use the .charAt() method to extract the first character
    		// of the example_string:
    		char first_character = example_string.charAt(0);
    		
    		System.out.println("Here is the contents of the example_string String object: \n" + example_string);
    		
    		System.out.println("Here is the first character of the example_string with the .charAt() method: \n" + first_character);
    		
    	
    		System.out.println("Now entering the getChars() portion of the program: ");
    		
    		
    		// getChars(): This method extracts a range of characters and stores 
    		// them in a char array. 
    		
    		// Syntax for getChars() method:
    		
    		// public void getChars(int srcBegin,
            // int srcEnd,
            // char[] dst,
            // int dstBegin)
    		
    		// So it specifies the index values for the start and stop of whatever
    		// section of the string you want to get the characters from
    		
    		// It then specifies whatever character array you want to store this 
    		// result in, as well as the index element of that array where you want
    		// to begin to store those characters
    		
    		// Example of getChars() method:
    		
    		String example_string2 = new String("Wow, what a cool way to surf!");
    		
    		System.out.println("Here is the length of the example_string2: " + example_string2.length());
    		
    		char[] destination_char_array = new char[29];	
    		
    		// Call the getChars() method using the example_string2 String object:
    		
    		example_string2.getChars(0, 20, destination_char_array, 0);
    		
    		// Print out the contents of the example_string2:
    		
    		System.out.println("Here are the contents inside the example_string2 String object: \n" + example_string2);
    		
    		// Make a for loop that will print out the values of the destination_char
    		// _array:
    		
    		System.out.println("Here is the contents of the destination_char_array: ");
    		
    		for (int i = 0; i < 20; i++)
    		{
    			System.out.print(destination_char_array[i]);
    			
    		}
    		
    		// SUCCESS!
    		
    		
    		// toString(): This method allows you to obtain a String object 
    		// from a CharBuffer object by calling its toString() method and 
    		// then you can use the getBytes() for the string object in order 
    		// to obtain the byte[] array corresponding to the StringBuffer object.
    		
    		// Syntax for toString():
    		// String toString()
    		// static String toString(int i)
    		
    		// Example of using toString():
    		
    		System.out.println("Now entering the toString portion of the program: ");
    		
    		// In order to use this toString() method, you have to specify the
    		// data type with a capital, as well as its full name for the integer
    		// variable in question:
    		
    		char s = 's';
    		
    		char s_array [];
    		
    		public CharBuffer get(s_array, 0, 1);
    		
    		
    		// make a String variable called y that will convert the value from
    		// the char_s CharBuffer object into a string using the .toString() method
    		
    		String y = char_s.toString();
    		
    		System.out.println("Here is the string form of the char_s CharBuffer object: " + y ); 
    		
    		// getBytes(): This method is used to obtain the byte[] 
    		// array corresponding to the StringBuffer object. 
    		
    		// Syntax for getBytes() method: 
    		// public byte[] getBytes(String charsetName) throws 
    		// UnsupportedEncodingException 
    		
    		// OR: public byte [] getBytes()
    		
    		byte[] byte_array = y.getBytes();
    		
    		// Make a for loop that will now print out each byte value
    		// of the byte_array:
    		
    		for (int i = 0; i < byte_array.length; i++)
    		{
    			System.out.print(byte_array[i]);
    			
    		}
    		
    		// Why is it obtaining the value 53?: Big Question
    		// Wouldn't it just have the value 5 stored from the integer variable
    		// x?
    		
    		
    		
    	}
    
    }
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can you copy the full text of the error messages and paste it here?

    It will be easier if you isolate the problem into as small a program as possible for compiling and testing.
    Last edited by NormR; October 23rd, 2013 at 02:45 PM.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Syntax error on token "(", = expected
    The method s_array(int, int) is undefined for the type ExtractingCharactersFromAMutableString
    Syntax error on token ",", ( expected
    char_s cannot be resolved

    at ExtractingCharactersFromAMutableString.main(ExtractingCharactersFromAMutableString.java:104)
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    What is on line 104?

    char_s cannot be resolved
    The compiler can't find the variable: char_s. Where is it defined?

    method s_array(int, int) is undefined
    Where is the method: s_array() defined?


    It will be easier if you isolate the problem into as small a program as possible for compiling and testing.

IMN logo majestic logo threadwatch logo seochat tools logo