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 3 of 3

Thread: Problem with string split method to string array

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Problem with string split method to string array

    So I'm creating a class which when given three inputs uses them as sides of a triangle and tells ther user what type of triangle it is, or if the input is invalid, tells them why it is invalid. I'm readin the input as a string and then trying to split it into a string array, from there checking to see if it has 3 elements.. in which the data is good at that point, and then converting them to ints and checking to see if they're negative ansd finally checking to see if they can work as sides of a triangle ie a+b >c, a+c >b , b+c >a.

    I'm trying to split it into an array of strings but am getting an error, and can't seem to figure out why as this should be working from what I've read of the string.split method online.

    import java.util.*;
     
    public class TriangleTest{
     
     
     private int sideA;
     private int sideB;
     private int sideC;
     
     
     public static void main(String[] args){
      TriangleTest triangle = new TriangleTest("3 4 5");
     
     
     
     
     
      } //end main
     
     public TriangleTest(String input)
      { 
     
      String[] toAnalyze = input.split(" ");
      // System.out.println(toAnalyze);
       TestAmount(toAnalyze);
     
      }// end triangletest
     
      public void TestAmount(String[] input)
      { 
     
        if (input.length > 3)
          {System.out.println("Please enter three valid side lengths seperated by spaces, you've entered more then 3");
          }
        else if (input.length < 3)
          { System.out.println("Please enter 3 valids side lengths, you've entered more then 3");
          }
     
     
        // System.out.println(sideA + sideB + sideC);
       }//end testAmount
     
     
      public void TestNegative(char[] input)
      {
     
      }

    the output reads [Ljava.lang.String;@15db9742


  2. #2
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Problem with string split method to string array

    What error are you getting?

    Rigth now you are just printing the address of the array of strings. you want to print the contents.

    System.out.println(toAnalyze[0]); // prints the first element

  3. The Following User Says Thank You to camel-man For This Useful Post:

    Helplessdrowningpuppy (September 21st, 2014)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,400
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Problem with string split method to string array

    output reads [Ljava.lang.String;@15db9742
    That is the String returned by a String array's toString() method.

    If you want to see the full contents of the array, use this:
     System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Helplessdrowningpuppy (September 21st, 2014)

Similar Threads

  1. New Method array to String or just print as Array?
    By Necator in forum What's Wrong With My Code?
    Replies: 19
    Last Post: February 20th, 2014, 03:09 PM
  2. Split a String into Ints
    By spunkyk014 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 7th, 2013, 09:35 PM
  3. Replies: 3
    Last Post: October 26th, 2012, 02:19 PM
  4. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  5. Replies: 3
    Last Post: June 14th, 2009, 09:31 PM

Tags for this Thread