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

Thread: Ranom Strings

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Location
    Goulds, FL
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Ranom Strings

    Hello! I am Chris Bethel an I am proud to be a member of this forum for the first time today. I am creating this thread to ask for assistance with a program involving arrays. Whenever I use the System output command, a random string of characters will appear instead of the array. What's happening?

    Here is the code for reference:

    package ChipsandSalsa;

    import java.util.Scanner;

    public class ChipsAndSalsa
    {

    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);

    int[] Salsas = new int[5];
    String[] Sauces = {"Mild","Medium","Sweet","Hot","Zesty"};

    System.out.print("Enter the amount of jars sold for each sauce:");

    for(int i = 0;i < Salsas.length;i++)
    {
    Salsas[i] = input.nextInt();
    if(Salsas[i] < 0)
    System.out.print("Invalid total. Try again!");
    }

    int max = Salsas[0];
    for(int i=0; i < Salsas.length; i++)
    {
    if (Salsas[i] > max)
    max = Salsas[i];
    }
    System.out.print("Highest selling sauce:" + Sauces[i]);

    int IndexOfMax = 0;
    for(int i=0; i < Salsas.length; i++)
    {
    if (Salsas[i] > max)
    max = Salsas[i];
    IndexOfMax = i;
    }
    System.out.print("Lowest selling sauce:" + Sauces[i]);

    int TotalSales = Salsas[1] + Salsas[2] + Salsas[3] + Salsas[4] + Salsas[5];
    System.out.print(Salsas);
    System.out.print("Total Sales: " + TotalSales);

    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,453
    My Mood
    Hungover
    Thanks
    144
    Thanked 638 Times in 546 Posts

    Default Re: Ranom Strings

    Java isn't smart enough to know what you want to do when you pass an array into the println() method. Not every array is an array of Strings, so it's not clear what printing an array out means. That's why you get the "random strings", which are the array's hashcode (which is from the Object.toString() method).

    What exactly do you want to print out? Each value? Should they be separated by commas? Something else? See, that's why Java doesn't do it for you, because there are too many options. You have to write the code yourself.

    You could also check out the Arrays class in the API, which has some useful functions you might be able to use here.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,765
    My Mood
    Mellow
    Thanks
    234
    Thanked 712 Times in 698 Posts

    Default Re: Ranom Strings

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly per the above link.

  4. #4
    Junior Member
    Join Date
    Sep 2014
    Location
    Goulds, FL
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ranom Strings

    I figured out the problem: I have to individually display each element of the array in a concatenated System.out.print statement.

Similar Threads

  1. Sorting lowercase strings before uppercase strings
    By keepStriving in forum Java Theory & Questions
    Replies: 4
    Last Post: March 26th, 2014, 03:33 PM
  2. Strings
    By NewCoder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 27th, 2014, 03:51 AM
  3. Strings
    By Leeds_Champion in forum Algorithms & Recursion
    Replies: 3
    Last Post: November 3rd, 2009, 09:09 PM
  4. Strings
    By BeSwift21 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 13th, 2009, 07:02 PM
  5. Replies: 2
    Last Post: June 19th, 2008, 03:58 AM

Tags for this Thread