1

This is a program to sort every word in a sentence in ascending order.

import java.util.Scanner;
import java.util.Arrays;
class sorteveryword
   {
       void main()
       {
           Scanner sc=new Scanner(System.in);
           System.out.println("Enter the string");
           String str=sc.nextLine();
           String arr[]=str.split(" ");
           int l=arr.length;
           for(int i=0;i<l;i++)
           {
               String w=arr[i];
               char ch[]=w.toCharArray();
               Arrays.sort(ch);
               System.out.print(ch+" ");
            }
        }    
    }   

This code seems perfect to me but when I am printing something it is showing this output.

Enter the string
ab bc
[C@55fba33b [C@51c42d5c 

I really don't know why this is happening.

Please help. And also please provide an alternative.

I want the output as

Enter the string
your eyes
oruy eesy
Zootopia
  • 121
  • 8
  • 2
    Does this answer your question? [What's the simplest way to print a Java array?](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – OH GOD SPIDERS Jan 17 '22 at 12:35
  • this is because you pass your instance of your array, instead of it's contents, to the println method. for arrays, there is no implementation of toString – Stultuske Jan 17 '22 at 12:36
  • by using toStirng, the square brackets and the commas will also be printed but i want the output as a string. what should i do? – Zootopia Jan 17 '22 at 12:38
  • Note: You can also create a new String from your sorted char arrays and use those for printing: `System.out.print(String.valueOf(ch)+" ");` - which is probably what you want – OH GOD SPIDERS Jan 17 '22 at 12:40
  • @oh god spiders i did that but the output is again same – Zootopia Jan 17 '22 at 12:41
  • and pls provide an alternative if you can – Zootopia Jan 17 '22 at 12:42
  • I already gave you the solution. See: https://ideone.com/4Mgoye for an example. You see there that it uses the code i told you to, with input "cba abc" and prints it as sorted "abc abc". – OH GOD SPIDERS Jan 17 '22 at 12:44
  • @oh god spiders can you explain why the output is correct when we are using String.valueOf method i havent heard of this method till now – Zootopia Jan 17 '22 at 12:48
  • @Zootopia I have code for your anagram question that just got deleted. If you could open a chat, I can share that code with you. – hfontanez Jan 26 '22 at 16:17
  • @hfontanez thank you very much but actually i understood the problem of my code i need to sort strings and then compare them but anyways thanks – Zootopia Jan 26 '22 at 17:03
  • @Zootopia actually, you want to compare the size of the strings first. If the strings are not of equal size, they are not anagrams. Then, you need to get the `char[]` from each string and sort them. Lastly, you call `Arrays.equal(char[], char[])`. That returns a boolean value. True means they are anagrams! Oh, and don't forget to call `toLowerCase()` before you extract the `char[]`from each string. – hfontanez Jan 26 '22 at 18:30

2 Answers2

1

The easiest way to achieve what you want is to construct a new String from your sorted char array and print that String:

System.out.print(String.valueOf(ch)+" ");

See: https://ideone.com/4Mgoye for an example.

Arrays by default do not provide an override for the toString() method and will use the default implementation of java.lang.Object - See How do I print my Java object without getting "SomeType@2f92e0f4"? for more background information on this.

While Strings themself of course can get printed without problems, so using Sting.valueOf to generate a String from your char array will allow you to print that String without much hassle.

OH GOD SPIDERS
  • 3,091
  • 2
  • 13
  • 16
0

When you are trying to print ,

 System.out.print(ch+" ");

this will print the default implementation of the toString method of the Object class( unless you override the toString method present for arrays)

To solve your problem, you can first convert it into string and print.

Scanner sc=new Scanner(System.in);
           System.out.println("Enter the string");
           String str=sc.nextLine();
           String arr[]=str.split(" ");
           int l=arr.length;
           for(int i=0;i<l;i++)
           {
               String w=arr[i];
               char ch[]=w.toCharArray();
               Arrays.sort(ch);
               System.out.print(new String(ch)+" ");
            }

which gives the required output

Enter the string
your eyes
oruy eesy 
Gurkirat Singh Guliani
  • 1,009
  • 1
  • 4
  • 19