Results 1 to 5 of 5
  1. #1
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default getting a strange string array..?

    Sorry couldn't come up with a better thread title.
    When i try to create and fill string arrays from an input file, i get some strange value when i try to print the array. There is an array for nouns, verbs, prepositions, and articles.

    My input text file contains:
    3
    cat dog horse
    a an the
    over under from
    jump hike run

    my fillArray method:
    Java Code:
    public static String [] fillArray(int words, Scanner fin)
    	{
    		String n = fin.nextLine();
    		String [] temp = new String [words];
    		for(int i = 0; i < words; i++)
    		{
    			temp [i] = n;
    		}
    		return temp; 
    	}// end fillArray
    my method to display the words of each type:
    Java Code:
    public static void DisplayWords(String [] nounArray, String [] verbArray, String [] prepArray, String [] artArray)
    	{
    		SortSearchUtil2.sort(nounArray);
    		SortSearchUtil2.sort(verbArray);
    		SortSearchUtil2.sort(prepArray);
    		SortSearchUtil2.sort(artArray);
    		System.out.println("Articles: " + artArray);
    		System.out.println("Nouns: " + nounArray);
    		System.out.println("Prepositions: " + prepArray);
    		System.out.println("Verbs: " + verbArray);
    	}//end DisplayWords
    I get an output of:
    Articles: [Ljava.lang.String;@69b332
    Nouns: [Ljava.lang.String;@173a10f
    Prepositions: [Ljava.lang.String;@530daa
    Verbs: [Ljava.lang.String;@a62fc3

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    11

    Default

    the output is ok, since you are not printing the content of the array, but its reference which consist of a hashcode. you should use the toString() method to print the content of the object or with arrays you can use Arrays.toString(nounArray).

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    10

    Wink

    OR, the old traditional way:
    Java Code:
    int[] x = { 1, 2, 3 };
    for (int i = 0; i < x.length; i++) {
    	System.out.println(x[i]);
    }

    Or the foreach loop added in java 5. :)

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default

    edit: nevermind, got it. just had to go each array length-1 up to the length of the array
    Java Code:
    for (int i = nounArray.length-1; i < nounArray.length; i++)				
    			System.out.println("Nouns: " + nounArray[i]);
    		for (int i = artArray.length-1; i < artArray.length; i++)
    			System.out.println("Articles: " + artArray[i]);
    		for (int i = artArray.length-1; i < verbArray.length; i++)
    			System.out.println("Verbs: " + verbArray[i]);
    		for (int i = prepArray.length-1; i < prepArray.length; i++)
    			System.out.println("Prepositions: " + prepArray[i]);
    Last edited by hiei_yasha; 02-18-2011 at 09:40 PM.

  5. #5
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default

    so if i wanted to print an array consisting of all the words, how would i go about doing that? our teacher gave us the first part of this

    Java Code:
    public static String [] printSortedArray(String [] nounArray, String [] artArray, String [] verbArray, String [] prepArray)
    	{
    		String [] temp = new String [nounArray.length+artArray.length+verbArray.length+prepArray.length];
    		int place = 0;
    		for (int i = 0; i<nounArray.length;i++)
    		{
    			temp[place]= nounArray[i];
    			place++;
    		}
    		for (int i = 0; i<artArray.length;i++)
    		{
    			temp[place]= artArray[i];
    			place++;
    		}
    		for (int i = 0; i<verbArray.length;i++)
    		{
    			temp[place]= verbArray[i];
    			place++;
    		}
    		for (int i = 0; i<prepArray.length;i++)
    		{
    			temp[place]= prepArray[i];
    			place++;
    		}
    		System.out.println(temp);
    		return temp;
    	}//end printArray
    also trying to generate a sentence of random noun, verb, prep, art, noun, with first letter of the first noun capitalized
    atm just trying to generate a random noun- i only get the entire noun array, with the first letter of first word capitalized
    Java Code:
    String [] randomStrings = new String[5];
    		Random rand = new Random();
    		int valN = rand.nextInt(nounArray.length);
    		String n = nounArray[valN];
    		String ltrN = n.charAt(0)+ "";
    		ltrN = ltrN.toUpperCase();
    		System.out.println(ltrN + n.substring(1, n.length()));
                    return randomStrings;
    	}//end GenerateSent
    Last edited by hiei_yasha; 02-18-2011 at 11:12 PM.

Similar Threads

  1. Changing a String array into a String?
    By BennyJass in forum New To Java
    Replies: 6
    Last Post: 01-16-2011, 02:42 PM
  2. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:01 PM
  3. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  4. strange alternating array
    By jarvis in forum New To Java
    Replies: 2
    Last Post: 04-23-2009, 09:42 AM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •