getting chararrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • noideanoclue
    New Member
    • Jul 2007
    • 5

    #1

    getting chararrays

    I have written a method which is supposed to get a string from the console and turn it into a char array, but it only returns gibberish, I can't see the problem either.
    Code:
        public static char[] readchararray()
        {
            BufferedReader dataIn = new BufferedReader( new InputStreamReader(System.in));
            String temp = "";
            int length = 1;
            char[] tempchar = new char[1];
            try
            {
                temp = dataIn.readLine();
                tempchar = temp.toCharArray();
            }
            catch(IOException e)
            {
                System.out.println("Error in reading input");
            }
            
            return tempchar;
        }
  • blazedaces
    Contributor
    • May 2007
    • 284

    #2
    Originally posted by noideanoclue
    I have written a method which is supposed to get a string from the console and turn it into a char array, but it only returns gibberish, I can't see the problem either.
    Code:
        public static char[] readchararray()
        {
            BufferedReader dataIn = new BufferedReader( new InputStreamReader(System.in));
            String temp = "";
            int length = 1;
            char[] tempchar = new char[1];
            try
            {
                temp = dataIn.readLine();
                tempchar = temp.toCharArray();
            }
            catch(IOException e)
            {
                System.out.println("Error in reading input");
            }
            
            return tempchar;
        }
    Try adding a line that prints temp before you assign tempchar. In general printing every step doesn't hurt...

    Also, you say it only returns gibberish... how do you know? I don't see you using the method anywhere, what does it return? What comes out when you try to print each character from the array?

    Lastly, why don't you print the entire stack trace for the caught error? It would tell you where the error originated:

    [code=java]
    catch(IOExcepti on e)
    {
    System.out.prin tln("Error in reading input");
    e.printStackTra ce();
    }
    [/code]

    Good luck,

    -blazed

    Comment

    Working...