I'm trying to create a confusion matrix however I'm not sure how to properly return my method where I can index both predicted and actual values, here's what I have tried thus far:
int[][] matrixConfusion(int[] predicted, int[] actual){
int[] count = new int [10];
int counts = 0;
int counts1 = 0;
int counts2 = 0;
int counts3 = 0;
for(int i = 0; i < count.length; i++) {
if(actual[i] == count[i] && predicted[i] == count[i]) {
counts++;
}else if(actual[i] != count[i] && predicted[i] != count[i]) {
counts1++;
}else if(actual[i] != count[i] && predicted[i] == count[i]) {
counts2++;
}else if(actual[i] == count[i] && predicted[i] != count[i]) {
counts3++;
}
}
System.out.println("\t"+ "Actual = 0:"+"\t"+"Actual = 1:"+"\n" + "Predic = 0:" + "\t" + counts + "\t" + counts2 + "\n" + "Predic = 1:" + "\t" + counts3 + "\t" + counts1);
return null;
}
The code works by printing out the statements above however, I cannot find a way to properly return the values from print,so that I can return the values rather than print them.
so I'm testing out this piece of code, and when I read a file, the output when being printed halts and I get java.lang.ArrayIndexOutOfBoundsException: -1
at WordLengths.countWordLengthsWithIsLettermethod(WordLengths.java:126)
at WordLengths.testWordLengths(WordLengths.java:152).
I'm wondering if somewhere I inadvertently added a limit to how many results I get, otherwise I can't think of what could be wrong.
This is the bit that's causing trouble (max = freqs[i];):
public int maxIndex(int[] freqs) {
int max = freqs[0];
for (int i = 1; i < freqs.length; i++) {
if (freqs[i] > max) {
max = freqs[i];
}
}
System.out.println("max number of words of certain lengths: "+max);
return freqs[max];
//return max;
}
Here's the whole code with the problem area included:
import edu.duke.FileResource;
public class WordLengths {
public void countWordLengths(FileResource resource, int[] counts) {
String abc = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (String word : resource.words()) {
String trim = word.trim();
int wordSize = trim.length();
//System.out.println("word" + "\t" + trim);
int lastInd = trim.length()-1;
//System.out.println("lastIndexOf word" + "\t" + lastInd);
//abc.contains(trim.charAt(lastInd));
char firstChar = trim.charAt(0);
char endChar = trim.charAt(trim.length()-1);
//System.out.println("firstChar" + "\t" + firstChar + "\t" + "endChar" + "\t" + endChar);
int idx = abc.indexOf(firstChar);
int idx_e = abc.indexOf(endChar);
int edx_sum = idx + idx_e;
//System.out.println("indexes abc" + "\t" + edx_sum);
//int idx_s = small.indexOf(firstChar);
//int idx_s_e = small.indexOf(endChar);
//int edx_s_sum = idx_s + idx_s_e;
//System.out.println("indexes small" + "\t" + edx_s_sum);
//System.out.println("indexes" + "\t" + idx + "\t" + idx_e + "\t" + idx_s + "\t" + idx_s_e);
//System.out.println("indexes" + "\t" + idx + "\t" + idx_e + "\t");
if (idx == -1 && idx_e == -1) {
wordSize -= 2;
} else
if (idx == -1 || idx_e == -1) {
wordSize -= 1;
}
if(wordSize>=counts.length) {
counts[counts.length-1] += 1;
} else
//right algorithm
if( counts[wordSize] != 0) {
counts[wordSize] += 1;
} else {
counts[wordSize] = 1;
}
}
//test
/*for(int i : counts) {
System.out.println(i);
}*/
}
/**
* the method countWordLengths(FileResource resource, int[] counts) with isLetter method
*
* #param resource
* #param counts
*/
public void countWordLengthsWithIsLettermethod(FileResource resource, int[] counts) {
for (String word : resource.words()) {
String trim = word.trim();
int wordSize = trim.length();
char firstChar = trim.charAt(0);
char endChar = trim.charAt(trim.length()-1);
if (!Character.isLetter(firstChar) && !Character.isLetter(endChar)) {
wordSize -= 2;
} else
if (!Character.isLetter(firstChar) || !Character.isLetter(endChar)) {
wordSize -= 1;
}
if(wordSize>=counts.length) {
counts[counts.length-1] += 1;
} else
//right algorithm
if( wordSize> 0 && counts[wordSize] != 0 ) {
counts[wordSize] += 1;
} else if ( wordSize> 0) {
counts[wordSize] = 1;
}
System.out.println(counts[wordSize] + " words with length " + wordSize + ": " + word);
}
//test
/*for(int i : counts) {
System.out.println(i);
}*/
}
public int maxIndex(int[] freqs) {
int max = freqs[0];
for (int i = 1; i < freqs.length; i++) {
if (freqs[i] > max) {
max = freqs[i];
}
}
System.out.println("max number of words of certain lengths: "+max);
return freqs[max];
//return max;
}
public void testWordLengths() {
WordLengths wl = new WordLengths();
FileResource fr = new FileResource();
int counts[] = new int[100];
//wl.countWordLengths(fr, counts);
wl.countWordLengthsWithIsLettermethod(fr, counts);
wl.maxIndex(counts);
}
}
What do think would happen if the word was empty (or just filled with blanks)
String trim = word.trim();
int wordSize = trim.length();
char firstChar = trim.charAt(0);
char endChar = trim.charAt(trim.length()-1);
Im at a point in my program where I need to set marks in a string before I pass it through another method, I have it so that every 4th char will have a "|" inserted, this is to mark a row break. Not I want to take each char in between the outter marks "|" and put a ",". that two char array method wont work here otherwise I would have tryed to use that but I'm not looking for a char array.
public static String matrixFormatter(String x){
x = x.substring(0, 4) + "|" + x.substring(4, x.length());
return x;
}
this works so far, now I want to add a "," between each char, I thought the code below would work and this would be easy but I was wrong.
public static String matrixFormatter(String x){
for(int i = 0; i<=x.length(); i+=4){
for(int j = 0; j<=x.length(); i++){
x = x.substring(0, i) + "|" + x.substring(i, x.length());
x = x.substring(0, j) + "|" + x.substring(j, x.length());
}
}
return x;
}
The code below adds a "," between characters in the string.
public static String matrixFormatter(String x){
String result;
for(int i = 0; i<x.length()-1; i++){
result += x.substring(i, i+1) + ",";
}
return result+",";
}
try this regex
s = s.replaceAll("(?<=.)(?=.)", ",");
It can be done with StringBuffer and also with Joiner of Guava library:
public static void main(String[] args) {
String s = "example";
System.out.println(withBuilder(s));
System.out.println(withJoiner(s));
}
private static String withJoiner(String s) {
return Joiner.on(",").join(Chars.asList(s.toCharArray()));
}
private static String withBuilder(String s)
{
StringBuilder builder = new StringBuilder(s);
int index = 1;
for (int i = 0; i < s.length() ; i++)
{
builder.insert(index, ",");
index +=2;
}
return builder.toString();
}
Output is:
e,x,a,m,p,l,e,
e,x,a,m,p,l,e
It can be done in one method:
public static String matrixFormatter(String x) {
List<String> chars = Arrays.asList(x.split(""));
String result = chars.get(0);
for (int i = 1; i < chars.size(); i++) {
if (i % 4 == 0)
result += "|" + chars.get(i);
else
result += "," + chars.get(i);
}
return result;
}
calling with:
System.out.println(matrixFormatter("12345678"));
outputs:
1,2,3,4|5,6,7,8
public static String matrixFormatter(String x) {
resultstr = "";
int i = 0;
while(i < x.length()) {
// If end of string: only add character.
if (i == x.length() - 1) {
resultstr += x.substring(i, i + 1);
} else {
if ( ((i + 1) % 4) == 0) {
resultstr += x.substring(i, i + 1) + "|";
} else {
resultstr += x.substring(i, i + 1) + ",";
}
}
i++;
}
return resultstr;
}
Haven't got Java installed but tested the concept via PHP-code:
function matrixFormatter($x) {
$resultstr = "";
$i = 0;
while($i < strlen($x)) {
if ($i == strlen($x) - 1) {
$resultstr .= $x[$i];
} else {
if ( (($i + 1) % 4) == 0) {
$resultstr .= $x[$i] . "|";
} else {
$resultstr .= $x[$i] . ",";
}
}
$i++;
}
return $resultstr;
}
matrixFormatter("abcdefghijklmnopq") returns "a,b,c,d|e,f,g,h|i,j,k,l|m,n,o,p|q".
I'm not sure I understand you question correctly, you should probably add some input and expected output to be more clearer.
String a = "abcdefghijklmnop";
String a2 = "";
for (int i = 0; i < a.length(); i++) {
if (i != 0) {
if(i % 4 == 0){
a2 += "|";
} else{
a2 += ",";
}
}
a2 += a.charAt(i);
}
System.out.println(a2);
This will produce the output a,b,c,d|e,f,g,h|i,j,k,l|m,n,o,p
This is my code.
public static void countContinuosOccurence() {
String first = "ABBCDDDEFGGH";
StringBuffer result = new StringBuffer();
int count = 1;
for (int i = 1; i < first.length(); i++) {
if (first.charAt(i) == (first.charAt(i - 1))) {
count++;
} else {
if (count > 1) {
result.append(String.valueOf(count) + first.charAt(i - 1));
} else {
result.append(first.charAt(i - 1));
}
count = 1;
}
}
System.out.println("First String is:"+ first);
System.out.println("Result is:" + result);
}
The result is:
First String is:ABBCDDDEFGGH
Result is:A2BC3DEF2G
It is missing the last character? May someone help me to solve this?
Not top-performing, but simplest code:
final String in = "ABBCDDDEFGGH" + '\u0000';
final StringBuilder b = new StringBuilder();
char prev = in.charAt(0);
int rpt = 0;
for (int i = 1; i < in.length(); i++) {
final char curr = in.charAt(i);
if (curr == prev) rpt++;
else {
b.append(rpt == 0? prev : "" + (rpt + 1) + prev);
rpt = 0; prev = curr;
}
}
System.out.println(b);
After the for loop ends, you'll need to append the count and the character of the last run of character(s) to the result:
public static void countContinuosOccurence() {
String first = "ABBCDDDEFGGH";
StringBuffer result = new StringBuffer();
int count = 1;
int i;
for (i = 1; i < first.length(); i++) {
if (first.charAt(i) == (first.charAt(i - 1))) {
count++;
} else {
if (count > 1) {
result.append(String.valueOf(count) + first.charAt(i - 1));
} else {
result.append(first.charAt(i - 1));
}
count = 1;
}
}
// ADD THIS - to take care of the last run.
if (count > 1) {
result.append(String.valueOf(count) + first.charAt(i - 1));
} else {
result.append(first.charAt(i - 1));
}
System.out.println("First String is:"+ first);
System.out.println("Result is:" + result);
}
public static void countContinuosOccurence() {
String[] input = "ABBCDDDEFGGH".split("");
String out = "";
for (int i = 0; i < input.length; i++) {
int repeatedCharCount = 1;
String currentChr = input[i];
if (!(i == input.length - 1)) {
while (input[i].equals(input[i + 1])) {
repeatedCharCount++;
i++;
}
}
out = out + repeatedCharCount + currentChr;
}
System.out.println(out);
}
There is also a hidden problem, that is that if you are terminating with a sequence with more than one occurrence, you will not write anything.
The simplest way to solve this problem and the problem you detected is to add a final check after the for block
[...]
}
int l = first.length();
if (count > 1) {
result.append(String.valueOf(count) + first.charAt(l - 1));
} else {
result.append(first.charAt(l - 1));
}
}
System.out.println("First String is:"+ first);
System.out.println("Result is:" + result);
}
import java.util.*;
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World");
String first = "ABBCDDDEFGGHhhhhh456456456{{{67}}}";
StringBuffer result = new StringBuffer();
result.append(first);
System.out.println(result);
Map<Character,Integer> map = new HashMap<Character,Integer>();
for(int i = 0; i < first.length(); i++) {
char c = first.charAt(i);
if (map.containsKey(c)) {
int cnt = map.get(c);
map.put(c, ++cnt);
} else {
map.put(c, 1);
}
}
Set set = map.entrySet();
// Get an iterator
Iterator itr = set.iterator();
// Display elements
while(itr.hasNext()) {
Map.Entry me = (Map.Entry)itr.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println("Hello World1");
}
}
I am stuck on how to find duplicate entries in an ArrayList and then manipulate them. I am implementing a polynomial class and I want to add monomials of the same order to create a new polynomial. Monomials have a degree and a coefficient. I want to cycle through a collection of monomials and find the monomials that have the same power and add the coefficients. The sum of all these like powered monomials will be the polynomial.
ArrayList (or any List) accepts duplicates.
However, since you want to group Monomials by their power, you might consider using a Map<Integer,Foo> where the key is the power. Foo has a lot of options. Foo could be an ArrayList<Monomial>, an ArrayList<Double>, holding only the coefficiants, that you add later. This requires some code writing on your part or else using a 3rd partly library for a MultiMap.
Or, Foo could be a Double which represents the summed coefficient, in which case you need to write an add(Monomial) method which updates the Double everytime.
If the possible range of powers is small and known, you could use a simple array too.
Here's my solution, you can add two polynomials of the same degree. Watch for bugs.
public class Polynome {
private double[] coefficients;
private int degre;
public Polynome(int degre) {
if (degre < 0) {
System.out.println("Invalid Degree");
}
this.degre = degre;
coefficients = new double[degre + 1];
for (int i = 0; i < degre; i++)
coefficients[i] = 0;
coefficients[degre] = 1;
}
public void setCoefficient(int degre, double coefficient) {
if (degre < 0 || degre > this.degre) {
System.out.println("Invalid Degree");
}
if (coefficient == 0 && degre == this.degre && this.degre != 0) {
System.out.println("Null Degree");
}
coefficients[degre] = coefficient;
}
/*
* Returns the coeff giving the degree of the element
*/
public double getCoefficient(int degre) {
if (degre < 0 || degre > this.degre) {
return 0;
}
if (degre == this.degre && this.degre != 0) {
return coefficients[this.getDegre()];
}
return this.coefficients[degre];
}
public String ToString() {
if (degre == 0)
return "" + this.coefficients[0];
String result = "" + this.coefficients[degre]+" x^" + degre;
for (int i = degre-1 ; i > 0 ; i--){
if (this.coefficients[i] < 0) {
result += "-" + Math.abs(this.coefficients[i]) + " x^" + i;
}
else {
if (this.coefficients[i] > 0){
result += " + " + this.coefficients[i] + " x^" + i;
}
}
}
if (this.coefficients[0]!= 0) result += " + " + this.coefficients[0] ;
return result;
}
/*
* Returns the degree of the current poly
*/
public int getDegre() {
return degre;
}
/*
* Adds two Polys with the same degrees
*/
public Polynome add(Polynome p) {
Polynome result = new Polynome(p.getDegre());
int deg = result.getDegre();
for(int i = deg ; i >0 ; i--) {
result.coefficients[i] = this.getCoefficient(i) + p.getCoefficient(i);
}
return result;
}
public static void main(String...args) {
Polynome p = new Polynome(2);
p.setCoefficient(2, 7);
Polynome p1 = new Polynome(2);
p1.setCoefficient(2, 2);
System.out.println(p.ToString() + "\n" + p1.ToString() + "\n\n" + (p.add(p1)).ToString());
}
}