This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Reordering Alphabets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reordering Alphabets" Watch "Reordering Alphabets" New topic
Author

Reordering Alphabets

feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
I want someone help me in re-orders a string of letters in alphabetical order (i.e. you enter a string of letters eg �cadi� push a button �re-order�, shows the output, which in this case would lead to �acdi�.
arun nohwar
Greenhorn

Joined: Aug 05, 2008
Posts: 16
In this case, solving-problem time is far less than waiting-solution time.
feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
I just want hints
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

You can get a char[] by calling toCharArray(). You can then sort this, mostly using java.util.Arrays#sort. If this is a homework assignment, you'll probably have to implement your own sorting algorithm. This link should be able to help you then.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
Is there any ready method to compare between strings and how to read a tring and stores its contnts as an array
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

You can compare Strings using String's own compareTo and compareToIgnoreCase methods: "hello1".compareTo("hello2")

As for comparing characters: char is actually a special numeric type. As such, you can just use < and > for comparing, and even - for subtracting (although the result will be an int).
feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
How to read a string then converting its characters into values of certain array
for example read JAVA then converting each letter into value of array
ARRAY[1]=j
ARRAY[2]=A
ARRAY[3]=v
ARRAY[4]=A
Knowing that we donnot know the length of word
Gaurav Chauhan
Greenhorn

Joined: Apr 14, 2008
Posts: 27
try it it's not that hard


Don't tell me there is nothing beyond sky, There are footprints on moon.
feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
I will try but how?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You have a String, and you want to make an array of chars out of it? It's true, that's very easy. To find out how, look at the API documentation. Go to the part which describes the String class. (That's java.lang.String in full, by the way). Scan down the list of methods and see if you can find one which returns a char[]. Then read its description and see if it suits your need.
feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
I implemeted a code to read a string then output the letters of it one by one but itisnot doing it .Could you tell me what is wrong?

package stringreader;

import java.io.*;
import java.lang.Exception;
public class StringReader
{
public static void main (String[] args)
{
System.out.println("Enter your name then type Enter ");
String string="";
InputStreamReader input= new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
try
{

string = reader.readLine();

}
catch(Exception e){}

System.out.println("You typed: " + string);
for ( int count=0;count==string.length();count++)
{System.out.println(count);
System.out.println(string.charAt(count));
System.out.println();

}

// wait for user to type 'Enter' so console window won't dissapear

System.out.println("Type 'Enter' to exit.");


System.out.printf("Your Name before reordering is :%s",string);

}
}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18548
    
  40

I implemeted a code to read a string then output the letters of it one by one but itisnot doing it


Well, just what exactly is it doing instead? And based on that, can you take a guess on what you should look at?

Henry

PS. I know that it seems we are not being very helpful, but learning to debug an application is a very important skill. You need to learn it with the easy examples, as it will be harder later on.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11172
    
  16

I implemeted a code to read a string then output the letters of it one by one but itisnot doing it
Statements like this are very vague. WHAT exactly is it not doing? did it compile? Does it run? does it read in the string? Does it output some, but not all of the letters? does it output the letters in the wrong order? does it output numbers instead of letters?

The more info you can give us, the easier, and more likely, someone will help you. The more work you require from someone to help you, the less likely that help will come.

What would be best here would be if you said something like:

I wrote the following code. It reads the string in just fine, but the output only prints the first character, then dies, when I was expecting the entire string to be output one character per line. Can someone suggest where I look to try and fix this?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
Ok I wrote the following code to reorder a string but there is a compilation error The code is as follows and the error is following it:

Code:

Error:
C:\Documents and Settings\Moon\My Documents\NetBeansProjects\StringReader\src\stringreader\StringReader.java:65: unexpected type
required: variable
found : value
name.charAt(count)= smallest;
C:\Documents and Settings\Moon\My Documents\NetBeansProjects\StringReader\src\stringreader\StringReader.java:66: unexpected type
required: variable
found : value
name.charAt(index)=temporary;
2 errors
BUILD FAILED (total time: 0 seconds)


[HENRY: Added Code Tags]
[ August 07, 2008: Message edited by: Henry Wong ]
Maurizio Nagni
Ranch Hand

Joined: May 29, 2004
Posts: 75
ehm... what do you think to simplify your code using something like this below?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

As I said before, that's the perfect approach unless the sorting is a homework assignment.

feda, could you please UseCodeTags in the future? It will make your code so much easier to read.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18548
    
  40

To answer the question. These are not a valid expression in Java.



Java requires that the left side of the assignment is a variable -- which is stated by the error message.

Henry
feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
I changed my code little now it is:
package stringreader;

import java.io.*;
public class StringReader
{
public static void main (String[] args)

{
System.out.println("Enter your name then type Enter ");
String string="";
InputStreamReader input= new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
try
{

string = reader.readLine();

}
catch(Exception e){}

System.out.println("You typed: " + string);
for ( int count=0;count< string.length();count++)
{System.out.println(count);
System.out.println(string.charAt(count));
System.out.println();

}

// wait for user to type 'Enter' so console window won't dissapear

System.out.println("Type 'Enter' to exit.");


System.out.printf("Your Name before reordering is :%s",string);
sort(string);
System.out.printf("The name after reordering is :%s", string);

}
public static String sort(String string)
{
char smallest;
int index;
String name;
name=string;
StringReader reader = new(StringReader);
for (int count=0;count<name.length()-1;count++)
{

smallest=name.charAt(count);
for(int i=count+1;i<name.length();i++)
if (name.charAt(i)<smallest)
{
smallest=name.charAt(i);
index=i;
}


reader.swap(string.charAt(index),smallest);

}
return(name);

}
public void swap (char first,char second)
{
char temporary=first;
first=second;
second=temporary;

}
}
But I got the folowig error
ompiling 1 source file to C:\Documents and Settings\Moon\My Documents\NetBeansProjects\StringReader\build\classes
C:\Documents and Settings\Moon\My Documents\NetBeansProjects\StringReader\src\stringreader\StringReader.java:50: <identifier> expected
StringReader reader =new(StringReader);
1 error
BUILD FAILED (total time: 1 second)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18548
    
  40

This is not a valid Java instruction.



"new" is an operator -- it is not a method.

Henry
feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
After modifying the code I got the following error:

package stringreader;

import java.io.*;
import java.util.*;
public class StringReader
{
public static void main (String[] args)

{
System.out.println("Enter your name then type Enter ");
String string="";
InputStreamReader input= new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
try
{

string = reader.readLine();

}
catch(Exception e){}

System.out.println("You typed: " + string);
for ( int count=0;count< string.length();count++)
{System.out.println(count);
System.out.println(string.charAt(count));
}

// wait for user to type 'Enter' so console window won't dissapear

System.out.println("Type 'Enter' to exit.");


System.out.printf("Your Name before reordering is :%s\n",string);
sort(string);
System.out.printf("The name after reordering is :%s", string);

}
public static String sort(String string)
{
char smallest;
int index=0;
String name;
name=string;
StringReader reader = new StringReader();

for (int count=0;count<name.length()-1;count++)
{
System.out.println("Sort Algorithm");
smallest=name.charAt(count);
for(int i=count+1;i<name.length();i++)
if (name.charAt(i)<smallest)
{
smallest=name.charAt(i);
System.out.printf("smallest from the loop number%d inner loop%d\n",i,smallest);
index=i;
}


reader.swap(string.charAt(index),smallest);

}
return(name);

}
public void swap (char first,char second)
{
char temporary=first;
first=second;
second=temporary;

}
}

init:
deps-jar:
compile:
run:
Enter your name then type Enter
Down
You typed: Down
0
D
1
o
2
w
3
n
Type 'Enter' to exit.
Your Name before reordering is own
Sort Algorithm
Sort Algorithm
Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Character
smallest from the loop number3 inner loop
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:3992)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2708)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2660)
at java.util.Formatter.format(Formatter.java:2432)
at java.io.PrintStream.format(PrintStream.java:920)
at java.io.PrintStream.printf(PrintStream.java:821)
at stringreader.StringReader.sort(StringReader.java:59)
at stringreader.StringReader.main(StringReader.java:39)
Java Result: 1
BUILD SUCCESSFUL (total time: 7 seconds)
feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
Can someone tell me whatis wrong please?

package stringreader;

import java.io.*;

public class StringReader
{
public static void main (String[] args)

{
System.out.println("Enter your name then type Enter ");
String string="";
InputStreamReader input= new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
StringReader sorting=new StringReader();
try
{

string = reader.readLine();

}
catch(Exception e){}


System.out.println("Type 'Enter' to exit.");
System.out.printf("Your Name before reordering is :%s\n",string);
sorting.sort(string);
System.out.printf("The name after reordering is :%s", string);

}

public String sort(String string)
{
char smallest;
int index=0;
int count1=0;
char first=0;
String name;
name=string;
char stringarray[];
stringarray=new char[name.length()];
for (int arraycount=0;arraycount<name.length();arraycount++)
stringarray[arraycount]=name.charAt(arraycount);
StringReader reader = new StringReader();

for (int count=0;count<name.length()-1;count++)
{
System.out.println("Sort Algorithm");
smallest=stringarray[count];
System.out.println(smallest);
for(int i=count+1;i<name.length();i++)
if (stringarray[i]<smallest)
{
smallest=stringarray[i];
System.out.printf("smallest from the loop number%d inner loop%d\n",i,smallest);
index=i;
}
count1=count;

}


char temporary=stringarray[count1];
stringarray[count1]=stringarray[index];
stringarray[index]=temporary;
return(name);

}

}
When comiling no error but if running I get the following

deps-jar:
Compiling 1 source file to C:\Documents and Settings\Moon\My Documents\NetBeansProjects\StringReader\build\classes
compile:
run:
Enter your name then type Enter
Help
Type 'Enter' to exit.
Your Name before reordering is :Help
Sort Algorithm
H
Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Character
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:3992)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2708)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2660)
at java.util.Formatter.format(Formatter.java:2432)
at java.io.PrintStream.format(PrintStream.java:920)
at java.io.PrintStream.printf(PrintStream.java:821)
at stringreader.StringReader.sort(StringReader.java:59)
at stringreader.StringReader.main(StringReader.java:31)
smallest from the loop number4 inner loop
Java Result: 1
BUILD SUCCESSFUL (total time: 9 seconds)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18548
    
  40

Can someone tell me whatis wrong please?


From the exception....



The error occured in your sort() method at line 59. From the rest of exception, can you figure out why? Can you isolate the exact line number? Can you add some debugging messages? etc...

Henry
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Originally posted by feda alshahwan:


You can replace the above with one single line:


Also, the creation of another StringReader object inside your sort method can be removed completely. You're not using it.

Finally, check out CallByReferenceVsCallByValue. Calling the sort method does not change your String object, and you're ignoring its return value. Therefore, the name after resorting will always be the same as before.
[ August 08, 2008: Message edited by: Rob Prime ]
feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
Thank you Henry I fixed the error and there is no more syntax error but unfortunately the program is not doing what is required from it :
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41180
    
  45
In addition to what Rob said, you can also get rid of the "first" and "count1" variable; you're either not using them (first) or they're unnecessary (count1). The less code there is, the easier it becomes to understand what's going on.

Also, I think the "i" loop doesn't stop at the right position; it should loop until the end of the name.
[ August 08, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reordering Alphabets
 
Similar Threads
Strip Letters From End of String
WA #1.....word association
validLetters()
Answer Four Simple Questions
Problem with reqular expression