• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reordering Alphabets

 
feda alshahwan
Ranch Hand
Posts: 170
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case, solving-problem time is far less than waiting-solution time.
 
feda alshahwan
Ranch Hand
Posts: 170
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want hints
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
feda alshahwan
Ranch Hand
Posts: 170
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try it it's not that hard
 
feda alshahwan
Ranch Hand
Posts: 170
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will try but how?
 
Paul Clapham
Sheriff
Pie
Posts: 20170
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
feda alshahwan
Ranch Hand
Posts: 170
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ehm... what do you think to simplify your code using something like this below?
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not a valid Java instruction.



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

Henry
 
feda alshahwan
Ranch Hand
Posts: 170
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic