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

Thread: Strange outcome

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Location
    Modena, Italy
    Posts
    5
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Strange outcome

    I wrote a little exercise, it's supposed to print the months of the year in which a given fruit (passed to main as a runtime argument) can be found. It consists of two classes:

    public class FruttaEVerdura {
        private String mese[];
        public FruttaEVerdura (String... mese) {
            this.setMese(mese);
        }
        public void setMese(String... mese) {
            this.mese = mese;
        }
        public String[] getMese() {
            return mese;
        }
    }
     
    public class TrovaStagionalita {
        public static void main(String args[]) {
            FruttaEVerdura albicocca = new FruttaEVerdura("giugno","luglio");
            FruttaEVerdura ciliegia = new FruttaEVerdura("maggio","giugno","luglio");
            FruttaEVerdura pesca = new FruttaEVerdura("giugno","luglio","agosto","settembre");
            FruttaEVerdura susina = new FruttaEVerdura("giugno","luglio","agosto","settembre");
            FruttaEVerdura uva = new FruttaEVerdura("luglio","agosto","settembre","ottobre","novembre","dicembre");
            FruttaEVerdura melone = new FruttaEVerdura("luglio","agosto","settembre");
            FruttaEVerdura cocomero = new FruttaEVerdura("giugno","luglio","agosto");
            FruttaEVerdura pera = new FruttaEVerdura("gennaio","febbraio","marzo","aprile","agosto","ottobre","novembre","dicembre");
            FruttaEVerdura mela = new FruttaEVerdura("gennaio","febbraio","marzo","aprile","settembre","ottobre","novembre","dicembre");
            FruttaEVerdura kiwi = new FruttaEVerdura("gennaio","febbraio","marzo","aprile","maggio","novembre","dicembre");
            FruttaEVerdura arancia = new FruttaEVerdura("gennaio","febbraio","marzo","aprile","maggio","novembre","dicembre");
            FruttaEVerdura fragola = new FruttaEVerdura("aprile","maggio","giugno");
            FruttaEVerdura caco = new FruttaEVerdura("ottobre","novembre");
            FruttaEVerdura castagna = new FruttaEVerdura("ottobre","novembre");
            switch (args[0]) {
                case "albicocca":
                System.out.println(albicocca.getMese());
                break;
                case "ciliegia":
                System.out.println(ciliegia.getMese());
                break;
                case "pesca":
                System.out.println(pesca.getMese());
                break;
                case "susina":
                System.out.println(susina.getMese());
                break;
                case "uva":
                System.out.println(uva.getMese());
                break;
                case "melone":
                System.out.println(melone.getMese());
                break;
                case "cocomero":
                System.out.println(cocomero.getMese());
                break;
                case "pera":
                System.out.println(pera.getMese());
                break;
                case "mela":
                System.out.println(mela.getMese());
                break;
                case "kiwi":
                System.out.println(kiwi.getMese());
                break;
                case "arancia":
                System.out.println(arancia.getMese());
                break;
                case "fragola":
                System.out.println(fragola.getMese());
                break;
                case "caco":
                System.out.println(caco.getMese());
                break;
                case "castagna":
                System.out.println(castagna.getMese());
                break;
                default:
                System.out.println("La voce inserita non e' stata trovata, riprova!");
            }
        }
    }

    But this instead is the outcome:
    [Ljava.lang.String;@867e89
    no matter which argument I pass to main method.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Strange outcome

    Each switch() branch results in a call to the fruit's method, getMese(). In the clall FruttaEVerdura, the variable mese[] is an array of strings. The method getMese() returns a the variable "mese", a reference to the array mese[] which looks like a memory address.

    You might try changing the getMese() method to:
    public String getMese() {
        return Arrays.toString( mese );
    }
    Note that the method returns a String object rather than a String[] array thanks to the Arrays.toString() method.

    There are other options, but this will get you moving ahead.

    Good luck!

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    misteruaila (August 24th, 2013)

  4. #3
    Junior Member
    Join Date
    Aug 2013
    Location
    Modena, Italy
    Posts
    5
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Strange outcome

    Still isn't working...now I get an exception; for example when I run the program with the command line argument pera:

    Exception in thread "main" java.lang.NoSuchMethodError: FruttaEVerdura.getMese()[Ljava/lang/String;
    at TrovaStagionalita.main(TrovaStagionalita.java:40)

    Shouldn't that be thrown only when the JVM can't find main?

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Strange outcome

    Ahh, but it's not working differently.

    Post the updated program. Providing guesses about what may or may not be wrong with code we haven't seen is pointless.

    The error message should be helpful, and it's not complaining about not finding the main() method. It's complaining about not finding a method called in the main() method. You might want to stare at that a bit more and try to work out what it's telling you. Maybe you'll even determine the fix. If not, post the code.

  6. #5
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Strange outcome

    Hello.
    Just write your own custom method, say printStringArray(String s[]). In this method print each string element from the array individually.
    Ex: for(String str:s) System.out.println(str);
    Instead of invoking System.out.println() in the switch statement invoke printStringArray().

    Syed.

  7. The Following User Says Thank You to syedbhai For This Useful Post:

    misteruaila (August 28th, 2013)

  8. #6
    Junior Member
    Join Date
    Aug 2013
    Location
    Modena, Italy
    Posts
    5
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Strange outcome

    Thank you, I will try that ASAP.

  9. #7
    Junior Member
    Join Date
    Aug 2013
    Location
    Modena, Italy
    Posts
    5
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Strange outcome

    Quote Originally Posted by syedbhai View Post
    Hello.
    Just write your own custom method, say printStringArray(String s[]). In this method print each string element from the array individually.
    Ex: for(String str:s) System.out.println(str);
    Instead of invoking System.out.println() in the switch statement invoke printStringArray().

    Syed.
    That did it!
    Thank you

Similar Threads

  1. Predicting the Outcome without a compiler **Not a Homework Question**
    By sternfox in forum Java Theory & Questions
    Replies: 1
    Last Post: February 25th, 2013, 04:53 PM
  2. Please help this is strange.
    By MillerJLee79 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 7th, 2012, 04:01 AM
  3. A For loop that calculates every possible outcome for 2 AND 3 chars...
    By MyNamesMatt in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2012, 10:59 AM
  4. [SOLVED] What in SWINGS name do i use to get this outcome?!
    By JonLane in forum AWT / Java Swing
    Replies: 2
    Last Post: February 25th, 2012, 04:55 PM
  5. something strange
    By frozen java in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 4th, 2011, 08:58 PM