Add Two Polynomials Java Program - java

I am working on this simple program that adds two polynomials. However, I am getting wrong results and could not spot the mistake.
import java.util.LinkedList;
public class Polynomial {
private LinkedList<Term> terms = new LinkedList<Term>();
private class Term {
private int coef;
private int exp;
public Term(int coef, int exp) {
this.coef = coef;
this.exp = exp;
}
public int getCoef() {
return coef;
}
public int getExp() {
return exp;
}
public String toString() {
return (this.coef + "x^" + this.exp);
}
}
public String addPoly(String first, String second) {
LinkedList<Term> otherTerms = new LinkedList<Term>();
String result = "";
String [] termsArray1 = first.split(";");
String [] termsArray2 = second.split(";");
for (int i = 0; i < termsArray1.length; i++) {
String [] temp = termsArray1[i].split("x\\^");
int currentCoef = Integer.parseInt(temp[0]);
int currentExp = Integer.parseInt(temp[1]);
Term currentTerm = new Term(currentCoef, currentExp);
terms.add(currentTerm);
}
for (int i = 0; i < termsArray2.length; i++) {
String [] temp = termsArray2[i].split("x\\^");
int currentCoef = Integer.parseInt(temp[0]);
int currentExp = Integer.parseInt(temp[1]);
Term currentTerm = new Term(currentCoef, currentExp);
otherTerms.add(currentTerm);
}
int i = 0;
int j = 0;
while (true){
if(i == terms.size() || j == otherTerms.size()) {
break;
}
if(terms.get(i).getExp() < otherTerms.get(j).getExp()) {
result += (otherTerms.get(j).toString() + ";");
j++;
}
if(terms.get(i).getExp() > otherTerms.get(j).getExp()) {
result += (terms.get(i).toString() + ";");
i++;
}
if(terms.get(i).getExp() == otherTerms.get(j).getExp()) {
Term temp = new Term((terms.get(i).getCoef() + otherTerms.get(j).getCoef()), terms.get(i).getExp());
result += (temp.toString() + ";");
i++;
j++;
}
}
result = result.substring(0, result.length()-1);
return result;
}
}
::Test::
String s3 = "5x^2;-4x^1;3x^0";
String s4 = "6x^4;-1x^3;3x^2";
Polynomial p = new Polynomial();
System.out.println(p.addPoly(s4, s3));
Expected result: 6x^4;-1x^3;7x^2;-4x^1;3x^0
Actual result: 3x^4;7x^2;-1x^1;10x^0

The problem is that when your loop exits, one of the following can still be true:
i < terms.size()
j < j == otherTerms.size()
And this is the case with your example input. This means that part of one of the terms has not been processed and integrated into the output.
A second problem is that your multiple if statements are not exclusive; after the first if block is executed and j++ has executed, it might well be that j is an invalid index in otherTerms when the second if is evaluated. This should be avoided by turning the second and third if into else if.
Here is a fix for that loop:
while (i < terms.size() || j < otherTerms.size()) {
if(i == terms.size() || j < otherTerms.size() && terms.get(i).getExp() < otherTerms.get(j).getExp()) {
result += (otherTerms.get(j).toString() + ";");
j++;
}
else if(j == otherTerms.size() || i < terms.size() && terms.get(i).getExp() > otherTerms.get(j).getExp()) {
result += (terms.get(i).toString() + ";");
i++;
}
else if(terms.get(i).getExp() == otherTerms.get(j).getExp()) {
Term temp = new Term((terms.get(i).getCoef() + otherTerms.get(j).getCoef()), terms.get(i).getExp());
result += (temp.toString() + ";");
i++;
j++;
}
}
Better approach
Your approach is not really OOP. Ideally, the first expression should serve to create one instance of Polynomial and the other expression should serve to create another instance of Polynomial. Then there should be a method that can add another Polynomial instance to the own instance. Finally there should be a toString method that returns the instance as a string in the required format. Your driver code would then look like this:
Polynomial a = new Polynomial("5x^2;-4x^1;3x^0");
Polynomial b = new Polynomial("6x^4;-1x^3;3x^2");
Polynomial sum = a.addPoly(b);
System.out.println(sum.toString());
This is much more object oriented, and will automatically avoid the code repetition that you currently have.

Related

How to use/modify Knuth-Morris-Pratt algorithm to convert any given string to palindrome

I have been given a task to create a class that given a String will create a palindrome with minimum number of assertions.
Example Runs:
Input: 123333
Output: 12333321
Input: 789
Output: 78987
Input: 1221
Output: 1221221
**Note a Palindrome should NOT return the same Palindrome.
I tried using a modified KMP algorithm as stated here.
I revert the string and compare it to the reverse + original string and then add the mismatches to the original string.
However my function only works for inputs with trailing digits (first example input) however an input like 1234 will return 1234123, '92837465' will return '928374659283746'
public static int[] computelps(String sample){
int[] lps = new int[sample.length()];
lps[0] = 0;
int i = 1;
int len = 0; // length of previous longest prefix suffix
while (i < sample.length()) {
if (sample.charAt(i) == sample.charAt(len)) {
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0) {
len = lps[len - 1];
}
else {
lps[i] = 0;
i++;
}
}
}
return lps;
}
public static void Solution(File samplefile) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(samplefile));
String firstline = br.readLine();
String line;
while ((line = br.readLine()) != null) {
String reverse_str = "";
String newline = line.replace(".", "");
for (int i = newline.length() - 1; i >= 0; i--) {
reverse_str += newline.charAt(i);
}
int [] lps = computelps(reverse_str); // computes the lps of the pattern string
String tot = reverse_str + newline;
// KMP Algorithm modified.
int x = 0; // index for total_string(tot)
int y = 0; // index for pattern
String palindrome = newline;
while (x < tot.length()){
if(reverse_str.charAt(y) == tot.charAt(x)){
y++;
x++;
}
if(y == reverse_str.length()) {
y = lps[y - 1];
}
else if( x < tot.length() && (reverse_str.charAt(y) != tot.charAt(x))){
palindrome += tot.charAt(x);
if ( y!= 0){
y = lps[y-1];
}
else{
x += 1;
}
}
}
System.out.println(palindrome);
}
}
I would appreciate any help. I find algorithms very challenging so please bear with me if my approach or code is sub-par.
*I fixed sample inputs and outputs as well as added my results.
It helps to split this problem in smaller problems, implement a separate method for each and check to see if each method works as expected. What will really help you will be to learn to use the debugger in your Ide. But until you do that you can test that each part of your code works as expected. So I simplified a little your code and split it up :
public static void main(String[] args){
System.out.println("computelps " + ("[0, 0, 0, 0]".equals(Arrays.toString(computelps("4321"))) ? "works" : "doesn't work" ));
System.out.println("reverse " + ("4321".equals(reverse("1234")) ? "works" : "doesn't work" ));
System.out.println("Solution " + ("1234321".equals(Solution("1234")) ? "works" : "doesn't work" ));
}
public static int[] computelps(String sample){
int[] lps = new int[sample.length()];
lps[0] = 0;
int i = 1;
int len = 0; // length of previous longest prefix suffix
while (i < sample.length()) {
if (sample.charAt(i) == sample.charAt(len)) {
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0) {
len = lps[len - 1];
}
else {
lps[i] = 0;
i++;
}
}
}
return lps;
}
public static String Solution(String line) {
String newline = line.replace(".", "");
String reverse_str = reverse(newline);
int [] lps = computelps(reverse_str); // computes the lps of the pattern string
// KMP Algorithm modified.
return kpmModified(newline, reverse_str, lps);
}
private static String kpmModified(String newline, String reverse_str, int[] lps) {
int x = 0; // index for total_string(tot)
int y = 0; // index for pattern
String tot = reverse_str + newline;
String palindrome = newline;
while (x < tot.length()){
if(reverse_str.charAt(y) == tot.charAt(x)){
y++;
x++;
}
if(y == reverse_str.length()) {
y = lps[y - 1];
}
else if( x < tot.length() && (reverse_str.charAt(y) != tot.charAt(x))){
palindrome += tot.charAt(x);
if ( y!= 0){
y = lps[y-1];
}
else{
x += 1;
}
}
}
return palindrome;
}
private static String reverse(String newline) {
String reverse_str = "";
for (int i = newline.length() - 1; i >= 0; i--) {
reverse_str += newline.charAt(i);
}
return reverse_str;
}
And the result is
computelps works
reverse works
Solution doesn't work
So your bug is in kpmModified method. I can't spend more time and I'm not familiar with the algorithm but you should continue like this and figure our what part of that method has the bug.
I think you overthink the problem. The question is basically adding a string's reversed version back to it's original, but not every character, right? So you might need to find something like a pointer to tell the function where to start to reverse.
One example. Let the string be 12333. If we add every character from the index string.length() to 0, it will be 1233333321, which is not correct, since there are duplicated 3's. We need to ignore those, so we need to add characters from string.length() - numOfDuplicateAtEnd to 0.
public String palindromic(String num) {
int i = num.length() - 1;
while (i > -1 && num.charAt(i) == num.charAt(num.length() - 1))
i--;
for (int k = i; k > -1; --k)
num += num.substring(k, k + 1);
return num;
}

How to fix "StringIndexOutOfBoundsException" error?

I need to make a program that prints the longest common sub-string out of two strings.
for example:
String str1 = "abcdef";
String str2 = "abcgef";
the longest common string should be "abc".
I can only use loops, strings, and arrays! no methods/functions etc.. I'm a beginner and although I know functions I am not allowed to use it.
I tried using a count variable so that the last letter wont be compared to others chars from the second string over and over but the same error occurs.
String com = "";
String com2 = "";
int a;
int b;
for (i = 0; i < str1.length(); i++) {
int count1 = 0;
int count2 = 0;
for (int j = 0; j < str2.length(); j++) {
a = i;
b = j;
com2 = "";
while (str1.charAt(a) == str2.charAt(b)) {
com2 = com2 + str1.charAt(a);
if (com2.length()>com.length()) {
com = com2;
}
if (a<str1.length()-1) {
a++;
}
if (b<str2.length()-1) {
b++;
}
}
}
}
System.out.println(com);
like I said, the result should be "abc" and that's it, but I get a runtime error saying StringIndexOutOfBoundsException out of range 6.
thanks!
You have your exception because of you loop till a<str1.length() and b<str2.length(). You should change it to a<str1.length()-1.
It happens because your string has length =6, but you start from 0. So the 6th element will be 5.
Also, in while{} you have endless loop when a and b reach last index of str1 and str2, so, be carefull.
P.S.
You can change it to
public void method() {
StringBuilder com = new StringBuilder();
String str1 = "abcdef";
String str2 = "abcgef";
if (str1.length() == str2.length()) {
for (int i = 0; i < str1.length() - 1; i++) {
if (str1.charAt(i) == str2.charAt(i)) {
com.append(str2.charAt(i));
continue;
} else {
break;
}
}
System.out.println(com);
} else {
System.out.println("They have different length");
}
}
as mentioned above there are some compile-errors (try to use an IDE, that helps).
After cleaning those up, I made some changes and it should work:
String str1 = "abcdef";
String str2 = "abcgef";
String com = "";
String com2 = "";
int a;
int b;
for (int i = 0; i < str1.length(); i++) {
//counts removed (never used)
for (int j = 0; j < str2.length(); j++) {
a = i;
b = j;
com2 = ""; // Reset before start a new Check Loop
while (str1.charAt(a) == str2.charAt(b)) {
com2 = com2 + str1.charAt(a);
if (com2.length() > com.length()) {
com = com2;
}
/**
* length() goes from 0 (empty String) to n
* index 0 is the first char in that String
* so you need to adjust that (the simple way is -1)
*/
if(a < str1.length()-1) {
a++;
}
if(b < str2.length()-1) {
b++;
}
//check for end of String -> Exit loop
if(a >= str1.length()-1 && b >= str2.length()-1) {
break;
}
}
}
}
System.out.println(com);
}
You get an exception because you access str1.charAt(a) after incrementing a, without checking if it is still in bounds. Same for str2.charAt(b).
Change the while loop guard to:
while (a < str1.length() && b < str2.length() && str1.charAt(a) == str2.charAt(b))
Two mistakes on your code :
You increment your while loop variables if they are lower than the associated string length. For str1 with a length of 6, if a is equal to 5, which is the last index of str1, you will have the StringIndexOutOfBoundsException (same on b / str2 )
You don't reinitialize com2 at the end of the while loop
Your code should be :
String com = "";
String com2 = "";
int a;
int b;
for (i=0; i<str1.length(); i++) {
int count1 = 0;
int count2 = 0;
for (int j=0; j<str2.length(); j++) {
a = i;
b = j;
while (str1.charAt(a) == str2.charAt(b)) {
com2 = com2 + str1.charAt(a);
if (com2.length()>com.length()) {
com = com2;
}
if (a<str1.length() - 1) {
a++;
}
if (b<str2.length() - 1) {
b++;
}
}
com2 = "";
}
}
System.out.println(com);
public class Main
{
public static void main (String[]args)
{
String com = "";
String com2 = "";
String str1 = "bowbowbowbow"; // took the liberty of initializiating
String str2 = "heloobowbowhellooo";
int a;
int b;
for (int i = 0; i < str1.length (); i++)
{
// removed redundant declaration and initializiation of count 1 and count 2
for (int j = 0; j < str2.length (); j++)
{
a = i;
b = j;
com2 = ""; // com2 should be made empty for each iteration
while ( ( str1.charAt (a) == str2.charAt (b) ) && (a < str1.length() - 1 ) && ( b < str2.length() -1) )
{
com2 = com2 + str1.charAt (a);
if (com2.length () > com.length ())
{
com = com2;
}
a++;
b++;
}
}
}
System.out.println (com);
}
}
Made some changes and have commented about it in the code. Seems to be working fine
you look something like this.
String str1="abcdef";
String str2="abcgefghj";
String com = "";
int min =Math.min(str1.length(), str2.length());
for (int i =0; i< min ; i++)
{
if(str1.charAt(i) == str2.charAt(i))
{
com = com + str1.charAt(i);
}
else {
break;
}
}
System.out.println(com);

Make parenthesis the first priority on an arithmetic expression on TAC

So I have here my code implementing Three Address Code in arithmetic expression.
class ThreeAddressCode {
private static final char[][] precedence = {
{'/', '1'},
{'*', '1'},
{'+', '2'},
{'-', '2'}
};
private static int precedenceOf(String t)
{
char token = t.charAt(0);
for (int i=0; i < precedence.length; i++)
{
if (token == precedence[i][0])
{
return Integer.parseInt(precedence[i][1]+"");
}
}
return -1;
}
public static void main(String[] args) throws Exception
{
int i, j, opc=0;
char token;
boolean processed[];
String[][] operators = new String[10][2];
String expr="", temp;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter an expression: ");
expr = in.readLine();
processed = new boolean[expr.length()];
for (i=0; i < processed.length; i++)
{
processed[i] = false;
}
for (i=0; i < expr.length(); i++)
{
token = expr.charAt(i);
for (j=0; j < precedence.length; j++)
{
if (token==precedence[j][0])
{
operators[opc][0] = token+"";
operators[opc][1] = i+"";
opc++;
break;
}
}
}
System.out.println("\nOperators:\nOperator\tLocation");
for (i=0; i < opc; i++)
{
System.out.println(operators[i][0] + "\t\t" + operators[i][1]);
}
//sort
for (i=opc-1; i >= 0; i--)
{
for (j=0; j < i; j++)
{
if (precedenceOf(operators[j][0]) > precedenceOf(operators[j+1][0]))
{
temp = operators[j][0];
operators[j][0] = operators[j+1][0];
operators[j+1][0] = temp;
temp = operators[j][1];
operators[j][1] = operators[j+1][1];
operators[j+1][1] = temp;
}
}
}
System.out.println("\nOperators sorted in their precedence:\nOperator\tLocation");
for (i=0; i < opc; i++)
{
System.out.println(operators[i][0] + "\t\t" + operators[i][1]);
}
System.out.println();
for (i=0; i < opc; i++)
{
j = Integer.parseInt(operators[i][1]+"");
String op1="", op2="";
if (processed[j-1]==true)
{
if (precedenceOf(operators[i-1][0]) == precedenceOf(operators[i][0]))
{
op1 = "t"+i;
}
else
{
for (int x=0; x < opc; x++)
{
if ((j-2) == Integer.parseInt(operators[x][1]))
{
op1 = "t"+(x+1)+"";
}
}
}
}
else
{
op1 = expr.charAt(j-1)+"";
}
if (processed[j+1]==true)
{
for (int x=0; x < opc; x++)
{
if ((j+2) == Integer.parseInt(operators[x][1]))
{
op2 = "t"+(x+1)+"";
}
}
}
else
{
op2 = expr.charAt(j+1)+"";
}
System.out.println("t"+(i+1)+" = "+op1+operators[i][0]+op2);
processed[j] = processed[j-1] = processed[j+1] = true;
}
}
}
Sample Output
Input : a * b / c
t1 = a * b
t2 = t1 / c
What the program does is evaluate the arithmetic expression and shows them step by step by operators.
Can you help me to include parenthesis in the priorities? and achieve an output like this
Sample Output
Input : a * ( b + c )
t1 = b + c
t2 = a * t2
Right now, the parenthesis is treated like an operand.
I did not use any of your code. Sorry.
This was a fun one to think about. I have never considered how you would do something like this. It does not follow all of the best practices to a "T", but the question inspired me to consider how you would do this in a rudimentary way.
You could make much of this code smaller by using more Java Frameworks, but it was enjoyable to strictly try to work this out logically.
This code is missing most validation (i.e. The user inputs an erroneous expression)
It does however check if there are an equal number of open and close parenthesis.
Lastly, I had to wrap things up so I did not extend into expressions with nested parenthesis.
Example a * ( b * ( c / d ) - e ) >> this code does not handle this scenario, and would have to be enhanced to accommodate for this.
Otherwise, it should give a pretty good idea as to one way you could go about building a program to work through parenthesis.
I hope it helps
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MathPriority {
public static void main(String[] args) {
String expression = "a * (b * c) + (d / e)"; //You can work out how you want input to com in
List<String> priorityList = getPriorityList(expression);//Find parenthesis and sets priority.
expression = expression.replace(" ", "").replace("(", "").replace(")", "");//Take out any spaces and parenthesis
for (int i = 0; i < priorityList.size(); i++) {//Replaces the piece in parenthesis with var and outputs var
expression = expression.replace(priorityList.get(i), "t" + (i + 1));
System.out.println("t" + (i + 1) + " = " + priorityList.get(i));
}
System.out.println("t" + (priorityList.size() + 1) + " = " + expression);//pushes final variable set
}
//You can use this to build a List of indexes of a character within a String
public static List<Integer> getAllOccurencesOfChar(List<String> expression, String indexO) {
List<Integer> parIndexList = new ArrayList<>();
for (int i = 0; i < expression.size(); i++) {
if (expression.get(i).contains(indexO)) {
if (!parIndexList.contains(i) && i > 0) {
parIndexList.add(i);
}
}
}
return parIndexList;
}
//Outputs a list of substrings. They can later be used to parse through the inital string
public static List<String> getPriorityList(String expression) {
List<String> priorityList = new ArrayList<>();
expression = expression.replace(" ", "");
String[] eParts = expression.split("");
List<String> expressionParts = new ArrayList<>();//Get expression into single chars
for (String e : eParts) {//If you change this to an Array.List, it will not work. This type of list is fixed in size
expressionParts.add(e);
}
List<Integer> parIndexList = getAllOccurencesOfChar(expressionParts, "(");//find all open paranthesis
List<Integer> rParIndexList = getAllOccurencesOfChar(expressionParts, ")");//find all close paranthesis
if (parIndexList.size() != rParIndexList.size()) {
System.out.println("Your Equation does not have an equal number of open and close parenthesis");
System.exit(0);
}
//Work out the parenthesis
int loopIterator = parIndexList.size();//This will change as we iterate
for (int pars = loopIterator - 1; pars >= 0; pars--) {
int start = parIndexList.get(pars); //Define a start
int end = 0; //and End
//int end = rParIndexList.get(pars);
for (int contemplate = 0; contemplate < loopIterator; contemplate++) {//contemplate where given parenthesis starts and where its closing tag is
if (parIndexList.get(pars) < rParIndexList.get(contemplate)) {
end = rParIndexList.get(contemplate);//find first occurence and set true end
break;//then stop
}
}
String expre = "";
for (int concat = start + 1; concat < end; concat++) {
expre += expressionParts.get(concat);//put the priorityList's subExpression together
}
priorityList.add(expre);//add that subExpression to the list
expressionParts.subList(start, end + 1).clear();//remove these expressionParts
/*Re-establish where the parenthesis are, since we removed parts of the expression in the list*/
parIndexList = getAllOccurencesOfChar(expressionParts, "(");//find all open paranthesis
rParIndexList = getAllOccurencesOfChar(expressionParts, ")");//find all close paranthesis
loopIterator = parIndexList.size();//resize the forLoop
}
return priorityList;
}
public static List<Integer> getStartEndPosition(String fullExpression, String subExpression) {
List<Integer> sAndE = new ArrayList<>();
String[] eParts = subExpression.split("");
List<String> wordParts = new ArrayList<>();
wordParts.addAll(Arrays.asList(eParts));
/*Find multiples of same operand*/
List<Integer> add = getAllOccurencesOfChar(wordParts, "+");
List<Integer> subtract = getAllOccurencesOfChar(wordParts, "-");
List<Integer> divide = getAllOccurencesOfChar(wordParts, "/");
List<Integer> multiply = getAllOccurencesOfChar(wordParts, "*");
/*Find single Operands*/
int plus = subExpression.indexOf("+");
int minus = subExpression.indexOf("-");
int div = subExpression.indexOf("/");
int mult = subExpression.indexOf("*");
int multiOperands = plus + minus + div + mult;//See if multiples exist
int startingPosition = 0;
if (add.size() > 1 || subtract.size() > 1 || divide.size() > 1 || multiply.size() > 1
|| multiOperands > 0) {
//expression has multiple opreands of different types
String findStart = wordParts.get(0) + wordParts.get(1);
String findEnd = wordParts.get(wordParts.size() - 2) + wordParts.get(wordParts.size() - 1);
startingPosition = fullExpression.indexOf(findStart);
sAndE.add(startingPosition);
int endPosition = fullExpression.indexOf(findEnd);
sAndE.add(endPosition);
} else {
startingPosition = fullExpression.indexOf(subExpression);
sAndE.add(startingPosition);
sAndE.add(startingPosition + subExpression.length());
}
return sAndE;
}
}
String expression = "a * (b * c) + (d / e)"
Outputs:
t1 = d/e
t2 = b*c
t3 = a*t2+t1

Java Substring in if statement does not work

I'm trying to remove the last char in the string using substring method.
Outside the if statement works normally. But inside it just goes through the if statement and returns original String.
public String getElkon(int p, int zbytek) {
System.out.println(zbytek);
for (int i = 0; i < p; i++) {
vysledek += elkon[i];
vysledek += " ";
System.out.println(vysledek);
}
***if(zbytek != 0){
vysledek = vysledek.substring(0, vysledek.length() - 1);
return vysledek;
}
else{
return vysledek;
}***
}
Your code would be more straightforward if you didn't have to trim the trailing space from appending your String(s). Also, it isn't clear why you expect zbytek to control the trim. I think you wanted if (p != 0) (since that is your loop sentinel). I would use a StringJoiner to implement this like
public String getElkon(int p, int zbytek) {
System.out.println(zbytek);
StringJoiner sj = new StringJoiner(" ");
for (int i = 0; i < p; i++) {
sj.append(elkon[i]);
}
return sj.toString();
}
The last character in the string is a space, since you added it last time in the for loop.
To trim the last space before if statement you could use the code
public String getElkon(int p, int zbytek) {
System.out.println(zbytek);
for (int i = 0; i < p; i++) {
vysledek += elkon[i];
vysledek += " ";
System.out.println(vysledek);
}
if (p > 0)
vysledek = vysledek.substring(0, vysledek.length() - 1);
if(zbytek != 0){
...
}
return vysledek;
}

java sort string array according to kurdish characters

Is there any short way to sort a string array by Kurdish characters? I've looked at some source on internet but I couldn't find any solution. There is a way to sort. Writing a code alike a novel but it is a very long work.
kurdish characters: a,b,c,ç,d,e,ê,f,g,h,i,î,j,k,l,m,n,o,p,q,r,s,ş,t,û,u,v,w,x,y,z
The Collator class should come in-handy here. To quote from the doc,
The Collator class performs locale-sensitive String comparison. You use this class to build searching and sorting routines for natural language text.
So try something like this:
Collator unicodeCollator = Collator.getInstance(Locale.UNICODE_LOCALE_EXTENSION);
Collections.sort(yourListOfCharacters, unicodeCollator);
Note that we are able to call java.util.Collections.sort directly as above, because Collator implements the Comparator interface.
If for whatever reasons Locale.UNICODE_LOCALE_EXTENSION doesn't work, here's the full list of supported locales. And you can create your own locale using the Locale constructor.
I've solved my problem: content of my file was like this:
*Nîzamettîn Ariç - Kardeş Türküler - Rojek Tê
Bê xem bê şer welat azad rojek tê
Rojek ronahî rojek bişahî rojek tê
Roj Roja me ye....
*Koma Çiya - Tolhildan ^ Daketine Meydanê
Daketine meydanê gerilayên dînemêr
Ji bona tolhildanê wek baz û piling û şêr...
My solution: thîs letters is proper for toLowerCase function:
ABCÇDEÊFGĞHİÎJKLMNOÖPQRSŞTÛUÜVWXYZ
just I was problem. because lowerCase(I) for turkish is ı; but for kurdish it is i.
code:
in onCreate():
...
alfabetBike();
...
public static void alfabetBike() {
for (int i = 0; i < tips.length(); i++) {
String[] derbasi_arr = sernavs[i];
String[] derbasi_got = gotins[i];
for (int j = 0; j < hejmar[i] - 1; j++) {
int indeks = j;
String yaMezin = derbasi_arr[j];
for (int k = j + 1; k < hejmar[i]; k++) {
if (compareTwoString(yaMezin.substring(1), derbasi_arr[k].substring(1)) > 1) {
yaMezin = derbasi_arr[k];
indeks = k;
}
}
if (indeks != j) {
derbasi_arr[indeks] = derbasi_arr[j];
String derbasi = derbasi_got[indeks];
derbasi_got[indeks] = derbasi_got[j];
derbasi_arr[j] = yaMezin;
derbasi_got[j] = derbasi;
}
}
gotins[i] = derbasi_got;
sernavs[i] = derbasi_arr;
}
}
private static void printFile(){
alfabetBike();
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/alfabetfolder");
dir.mkdirs();
File file = new File(dir, "alfabet_title.txt");
File file2 = new File(dir, "alfabet.txt");
try {
FileOutputStream f = new FileOutputStream(file,false);
PrintWriter pw = new PrintWriter(f);
FileOutputStream f2 = new FileOutputStream(file2,false);
PrintWriter pw2 = new PrintWriter(f2);
for (int i = 0; i < tips.length(); i++) {
for (int j = 0; j < hejmar[i]; j++) {
Log.d("ssdddddd", "add" + hejmar[i] + "-" + j + " " + sernavs[i][j].trim());
pw.println(sernavs[i][j]);
pw.flush();
pw2.println(sernavs[i][j] + "\n" + gotins[i][j].trim());
pw2.flush();
}
}
pw.close();
f.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.i("erroooor", "******* File not found. Did you" +
" add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
} catch (IOException e) {
e.printStackTrace();
}
}
public static int compareTwoString(String yek, String du) {
String d1 = yek, d2 = du;
d1 = strLower(d1, d1.charAt(0));
d2 = strLower(d2, d2.charAt(0));
int length, yaDirej;
if (yek.length() > du.length()) {
yaDirej = 1;
length = yek.length();
} else if (yek.length() < du.length()) {
yaDirej = 2;
length = du.length();
} else {
yaDirej = 0;
length = yek.length();
}
for (int i = 0; i < length; i++) {
int id1 = -1, id2 = -1;
if (i == d1.length() || i == du.length()) {
return yaDirej;
}
for (int j = 0; j < tips.length(); j++) {
if (d1.charAt(i) == tips.charAt(j)) id1 = j;
if (d2.charAt(i) == tips.charAt(j)) id2 = j;
}
if (id1 > id2)
return 2;
else if (id2 > id1)
return 1;
else
continue;
}
return 0;
}
public static String strLower(String str, char ziman){
final StringBuilder mutable = new StringBuilder(str);
final StringBuilder yedek = new StringBuilder(str.toLowerCase());
for (int i = 0; i < str.length(); i++) {
if (ziman == '?' && mutable.charAt(i) == 'I')
mutable.setCharAt(i, 'i');
else if (ziman == '*' && mutable.charAt(i) == 'I')
mutable.setCharAt(i, 'ı');
else mutable.setCharAt(i,yedek.charAt(i));
}
return mutable.toString();
}
edit:
in AndroidManifest.xml
<manifest...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
....
</manifest...>
You can build your own comparison so that, no matter what characters you are dealing with, it is going to sort the way you want. As you can see from the following code, I have set the comparison value by counting from a-z so that a=0, b=1...etc Then, I used the bubble sort strategy, which is basically switching the smallest elements continuously to the left and shifting others to the right.
public class Sort {
public static String compare(String compare1, String compare2) {
for (int i = 0; i < compare1.length(); i++) {
if (letterValue(compare1, i) < letterValue(compare2, i)) {
return compare1;
} else if (letterValue(compare1, i) > letterValue(compare2, i)) {
return compare2;
} else if (letterValue(compare1, i) == -1 || letterValue(compare2, i) == -1) {
System.out.print("Some letters are not within the alphabet!");
}
}
return compare1;
}
public static boolean smaller(String compare1, String compare2) {
if (compare(compare1, compare2).equalsIgnoreCase(compare1)) {
return true;
} else {
return false;
}
}
public static int letterValue(String input, int letterPosition) {
String order = "abcçdeêfghiîjklmnopqrsştûuvwxyz";
int value = -1;
for (int i = 0; i < order.length(); i++) {
if (input.toLowerCase().charAt(letterPosition) == order.charAt(i)) {
value = i;
}
}
return value;
}
public static void main(String[] args) {
String[] input = {"BARÊZ", "ÇÊneR", "ASTÛ", "badîn", "BADÎN"};
String swap;
int i, d;
for (i = 0; i < (input.length - 1); i++) {
for (d = 0; d < input.length - i - 1; d++) {
if (!smaller(input[d], input[d + 1])) {
swap = input[d];
input[d] = input[d + 1];
input[d + 1] = swap;
}
}
}
System.out.println("Sorted list: ");
for (i = 0; i < input.length; i++) {
System.out.print(input[i] + " ");
}
}
}
Output
Sorted list:
ASTÛ badîn BADÎN BARÊZ ÇÊneR

Categories

Resources