Results 1 to 7 of 7
  1. #1
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Simple problem: Changing an array to a string, weird output

    Java Code:
    public static String arrToString(int[] x){
    			String number = "";
    			for(int i = 0; i < x.length; i++){
    				number += x[i];
    			}
    			System.out.println();
    			System.out.println(number);
    			return number;
    		}
    Simple code, really simple thing... but whenever I do it, it orders my number for some reason... I would like to not do that...
    So for my code if I would print out the number before the string number this is what happens:

    618769
    it will change that array to this string: 987661

    I want to preserve the order... so how can I do that

  2. #2
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Changing an array to a string

    Okay, this is really weird...

    So i put a System.out.print in my main method, so I had a:

    Java Code:
    System.out.println();
    System.out.println("This number is a string: " + arrToString(x));
    and when I do that it orders the number, however, when I take out that first print line it preserves the order...

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Simple problem: Changing an array to a string, weird output

    I can`t believe that! Are you sure that you don`t sort the array at any other part of your program? Can you give us a sample program? Something like:
    Java Code:
    	public static void main(String[] args){
    		int[] x = {6,1,8,7,6,9};
    		System.out.println("This number is a string: " + arrToString(x));
    	}
    
    	public static String arrToString(int[] x) {
    		String number = "";
    		for (int i = 0; i < x.length; i++) {
    			number += x[i];
    		}
    		System.out.println();
    		System.out.println(number);
    		return number;
    	}
    output:
    Java Code:
    618769
    This number is a string: 618769

  4. #4
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Simple problem: Changing an array to a string, weird output

    I have a bunch of code after that DOES at some point sort the ARRAY, however no where do I have anything that sorts the string. But regardless, all of that code comes after those statements.

    With that first System.out.println(); and then the This number is a string... it will sort it, and the all I do is put // before the first print statement to skip the line and then it prints an unsorted number...

  5. #5
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Simple problem: Changing an array to a string, weird output

    This is the print statement I have in my main method... I hid the rest of the code in my main method and now it sorts it no matter what...
    Java Code:
    int [] number = randomNumber();
    			System.out.println();
    			System.out.println("This number is a string: " + arrToString(number));

  6. #6
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Simple problem: Changing an array to a string, weird output

    ok, I found the problem. In my randomNumber method I was printing out the number, but later in that method was sorting it and I didn't realize it. Sorry abuot that.

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Simple problem: Changing an array to a string, weird output

    It has to be your code, although you're not seeing it at the moment.

    Quote Originally Posted by Java Docs
    append
    public StringBuffer append(String str) Appends the string to this string buffer.
    The characters of the String argument are appended, in order, to the contents of this string buffer, increasing the length of this string buffer by the length of the argument...

Similar Threads

  1. Weird output from Array program
    By zniightmare in forum New To Java
    Replies: 2
    Last Post: 03-07-2012, 01:22 AM
  2. Weird array output
    By Army in forum New To Java
    Replies: 3
    Last Post: 01-17-2012, 06:58 AM
  3. returning an array weird problem
    By mcleanj in forum New To Java
    Replies: 4
    Last Post: 01-28-2011, 05:29 AM
  4. Changing a String array into a String?
    By BennyJass in forum New To Java
    Replies: 6
    Last Post: 01-16-2011, 02:42 PM
  5. JSP to output Java String Array
    By irenavassilia in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-31-2008, 04:11 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
  •