I'm using Netbeans, and I've written a method that's not doing exactly what it should:
private ArrayList<String[]>ProductsInStock;
public void DisplayStock() {
ArrayList<String[]> Sort = new ArrayList<String[]>();
System.out.println("");
for (int i = 0; i < ProductsInStock.size(); i++) {
if (ProductsInStock.get(i)[2].equals(Products.get(ProductCodeCB.getSelectedIndex())[1])) {
boolean foundColor = false;
int size = Sort.size();//Since the size will differ dynamically
for (int k = 0; k < size; k++) {
if (Sort.get(k)[3].equals(ProductsInStock.get(i)[3])) {
foundColor = true;
if (Sort.get(k)[4].equals(ProductsInStock.get(i)[4])) {
String S[] = Sort.get(k);
S[5] = (Integer.parseInt(Sort.get(k)[5]) + Integer.parseInt(ProductsInStock.get(i)[5])) + "";
Sort.set(k, S);
break;
}
if (k == Sort.size() - 1) {
Sort.add(ProductsInStock.get(i));
}
} else if (foundColor == true) {
Sort.add(k, ProductsInStock.get(i));
break;
}
}
System.out.print(ProductsInStock.get(0)[5]+" ");
if (foundColor == false) {
Sort.add(ProductsInStock.get(i));
}
}
}
}
}
The method should NOT change the value of ProductsInStock.get(0)[5], yet it is incrementing it by 1 everytime the method is called, I've placed the "System.out.println()" to show you how the value is actually changing. Here are the results: 1 1 1 1 1 1 1 1 2
And when i added the line "S[5]=ProductsInStock.get(i)[5];", the result changed to: 1 1 1 1 1 1 1 1 1 (as it should be):
public void DisplayStock() {
ArrayList<String[]> Sort = new ArrayList<String[]>();
System.out.println("");
for (int i = 0; i < ProductsInStock.size(); i++) {
if (ProductsInStock.get(i)[2].equals(Products.get(ProductCodeCB.getSelectedIndex())[1])) {
boolean foundColor = false;
int size = Sort.size();//Since the size will differ dynamically
for (int k = 0; k < size; k++) {
if (Sort.get(k)[3].equals(ProductsInStock.get(i)[3])) {
foundColor = true;
if (Sort.get(k)[4].equals(ProductsInStock.get(i)[4])) {
String S[] = Sort.get(k);
S[5] = (Integer.parseInt(Sort.get(k)[5]) + Integer.parseInt(ProductsInStock.get(i)[5])) + "";
Sort.set(k, S);
S[5]=ProductsInStock.get(i)[5]; //<<<<HERE>>>>
break;
}
if (k == Sort.size() - 1) {
Sort.add(ProductsInStock.get(i));
}
} else if (foundColor == true) {
Sort.add(k, ProductsInStock.get(i));
break;
}
}
System.out.print(ProductsInStock.get(0)[5]+" ");
if (foundColor == false) {
Sort.add(ProductsInStock.get(i));
}
}
}
}
As you can see, there is not a single "ProductsInStock.set()" or "ProductsInStock.get()[]= " to change any value in the arraylist.
When you write this :
Sort.add(ProductsInStock.get(i));
you are adding a reference of the ProductsInStock.get(i) array to the Sort list. Any changes done in Sort.get(Sort.size()-1) will affect the original array.
Therefore code such as
String S[] = Sort.get(k);
S[5] = ...
modifies one of the arrays of ProductsInStock List.
In order to avoid that, you should create a copy of your array before adding it to the other List :
Sort.add(Arrays.copyOf(ProductsInStock.get(i),ProductsInStock.get(i).length));
Related
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.
I have a program that takes in a file list of bands and albums. I need to determine the number of album each band makes and then print out a list of the the bands and the number of albums they made in descending order. I have looked around and seen it done using mapping and collections. I want to know how to do it without either. Here is what I have so far:
public static void processFile(String filename)
{
String bandname = "";
String[][] data = read_spreadsheet(filename);
//takes the file and converts it to a 2d array
ArrayList<String> bands = new ArrayList<String>();
for(int rows = 0; rows < data.length; rows++)
{
bands.add(data[rows][0]);
}
for(int i = 0; i<bands.size()-1;i++)
{
int albumcount = 0;
for(int j = i+1; j<bands.size();j++)
{
if(bands.get(i).equals(bands.get(j)))
{
albumcount++;
}
}
}
}
input example:
band1 -album
band2 -album
band1 -album
band3 -album
band1 -album
band2 -album
output example:
band1: 3
band2: 2
band3: 1
Without collections? You want to use arrays?
String [] names = new String[data.length];
int [] counts = new int[data.length];
int j = 0;
for (int i = 0; i < data.lenght; i++ ) {
while (j < data.length) {
if (data[i][0].equals(names[j])) {
found = true;
counts[j]++;
break;
} else if (names[j] == null) {
names[j] = data[i][0];
counts[j]=1;
break;
}
j++;
}
}
// find max count
// println
// nullify element
// repeat
for (int i = 0; i < j; i++) {
int max = -1;
int k = i;
int pos = -1;
while ( k < j ) {
if ( counts[k] > max ) {
pos = k;
max = counts[k];
}
k++;
}
if ( pos != -1 ) { // we found
System.out.println ( names[pos] + ": " + counts[pos]);
counts[pos] = -1;
}
}
If you sort the list of band names (with duplicates) and then count how many of each band name is in the list, you will get the album count for each band:
public static void processFile(String filename)
{
//takes the file and converts it to a 2d array
String[][] data = read_spreadsheet(filename);
// get all the bands (with duplicates)
ArrayList<String> bands = new ArrayList<String>();
for(int rows = 0; rows < data.length; rows++) {
bands.add(data[rows][0]);
}
// sort the bands alphabetically
Collections.sort(bands);
int albumCount = 1;
String currentBand = bands.remove(0);
while(bands.size() > 0) {
String nextBand = bands.remove(0);
if(currentBand.equals(nextBand)) {
albumCount++;
} else {
// print the current band album count and setup for the next band
System.out.println(currentBand + ": " + albumCount);
currentBand = nextBand;
albumCount = 1;
}
};
// print the final band album count
System.out.println(currentBand + ": " + albumCount);
}
I'm trying to solve a palindrome problem that the input consists of Strings , if the concatenation of two strings represent a palindrome word(A palindrome is a word which can be read the same way in either direction. For example, the following
words are palindromes: civic, radar, rotor, and madam)
then save it into array to print it latter otherwise print "0"
but I'm having a problem in filling the null index with zeros , here I get Exception
for (int re = 0; re < result.length; re++) {
if (result[re].equals(null)) {
result[re] = "0";
}
}
"Exception in thread "main" java.lang.NullPointerException"
here is my full code
import java.util.Scanner;
public class Palindrome {
public static String reverse(String R2) {
String Reverse = "";
String word_two = R2;
int ln = word_two.length();
for (int i = ln - 1; i >= 0; i--) {
Reverse = Reverse + word_two.charAt(i);
}
return Reverse;
}
public static void main(String[] args) {
Scanner inpoot = new Scanner(System.in);
int stop = 0;
String pal1;
int Case = inpoot.nextInt();
String result[] = new String[Case];
String Final;
int NumberofWords;
for (int i = 0; i < Case; i++) {
NumberofWords = inpoot.nextInt();
String words[] = new String[NumberofWords];
for (int array = 0; array < words.length; array++) {
words[array] = inpoot.next();
}
for (int word1 = 0; word1 < NumberofWords; word1++) {
if (stop > Case) {
break;
}
for (int word2 = 0; word2 < NumberofWords; word2++) {
if (word1 == word2) {
continue;
}
Final = "" + words[word1].charAt(0);
if (words[word2].endsWith(Final)) {
pal1 = words[word1].concat(words[word2]);
} else {
continue;
}
if (pal1.equals(reverse(pal1))) {
result[i] = pal1;
stop++;
break;
} else {
pal1 = "";
}
}
}
}
// HERE IS THE PROBLEM
for (int re = 0; re < result.length; re++) {
if (result[re].equals(null)) {
result[re] = "0";
}
}
for (int x = 0; x < result.length; x++) {
System.out.println("" + result[x]);
}
}
}
A test such as anObject.equals(null) makes no sense. Indeed, if anObject is null, it will throw a NullPointerException (NPE), and if it is not, it will always return false.
To test if a reference is null, just use anObject == null.
If you want to check whether result[re] is null, you cannot use equals. Use the identity comparison:
if (result[re] == null) {
result[re] = "0";
}
For those who aren't familiar, the game is a number guessing game, where a number is chosen (non repeating; e.g 1223 is NOT chosen) and the user makes a guess and obtains information whether the number AND digit is correct, number is correct but in a wrong digit, or the number is not contained. http://en.wikipedia.org/wiki/Bulls_and_cows
(e.g number chosen => 1234, guessing 3789 will give 1 cow)
Instead of the computer choosing the number and telling the properties and player guesses, I would like to do the reverse; I type in a number and the properties - the computer gives a list of possible numbers.
Anyways, my method is:
Add all numbers that do not repeat itself to the arraylist
Delete numbers that do not satisfy the conditions.
Here is how the cow cases are done :
//Case 5: property is 4 cows;
if (property.equals("040")) {
//delete if numbers don't appear EXACTLY 4 times
if (contains != 4) { numbers.remove(i); }
//removes if the digits of the number tried corresponds with the actual number (Cow!)
else if (n.charAt(0) == first.charAt(0)) { numbers.remove(i); }
else if (n.charAt(1) == second.charAt(0)) { numbers.remove(i); }
else if (n.charAt(2) == third.charAt(0)) { numbers.remove(i); }
else if (n.charAt(3) == fourth.charAt(0)) { numbers.remove(i); }
}
It has worked for cows. Upon trying to implement bulls, it seems like using this sort of approach won't be possible. How can I do a method for bulls!? Would I need to create four more arraylists and calculate for each case? Or is ArrayList not the way to go?
For example, 1234 with 1bull would mean the number to guess is 1XXX, X2XX, XX3X or XXX4 but
I can't use this approach as it will delete all number except the input.
Thanks.
You can try this algorithm for solving -
String userAnswer = // ... getUserAnswer();
if(userAnswer == null || userAnswer.equals("")
|| !userAnswer.matches("^-?\\d+$")
|| userAnswer.split("(?<=\\G.{1})").length < 4) {
// error
}
int[] secret = (int[])// ... getSecret(request);
int[] seq = {1,2,3,4};
for(int i = 0; i < userAnswer.split("(?<=\\G.{1})").length; i++) {
seq[i] = Integer.parseInt(s[i]);
}
int bullCount = 0;
int cowCount = 0;
for(int i = -1; ++i < secret.length;) {
if(secret[i] == seq[i]) {
bullCount++;
}
}
for(int i = -1; ++i < secret.length;) {
for(int j = -1; ++j < secret.length;) {
if(secret[i] == seq[j] && i != j) {
cowCount++;
}
}
}
String snswer = bullCount + "b" + cowCount + "c";
if(Arrays.equals(secret, seq))
// win!
else
// fail!
It's wrong as if the code is 1634 and the user enters 6113, the program returns 4 cows when there is in fact 3.
Try this:
private static String findNumberOfCowsAndBulls(String firstString, String secondString) {
if(firstString.equals(secondString))
return "All Bulls:" + firstString.length();
char[] fSArr = firstString.toCharArray();
char[] sSArr = secondString.toCharArray();
int countCow = 0;
int countBull = 0;
Map<String, Integer> fSMap = new HashMap<>();
Map<String, Integer> sSMap = new HashMap<>();
for (int i = 0; i < fSArr.length; i++) {
if(i < sSArr.length){
if(fSArr[i] == sSArr[i]){
countBull++;
}
else{
updateMapOfCharsCount(fSMap, fSArr[i]);
updateMapOfCharsCount(sSMap, sSArr[i]);
}
}
else{ //fSArr is bigger than sSArr
updateMapOfCharsCount(fSMap, fSArr[i]);
}
}
if(fSArr.length < sSArr.length){ //fSArr is shorter than sSArr
for(int i = fSArr.length; i < sSArr.length - fSArr.length; i++){
updateMapOfCharsCount(sSMap, sSArr[i]);
}
}
for (Map.Entry<String, Integer> entry : fSMap.entrySet()) {
String key = entry.getKey();
if(sSMap.containsKey(key)){
if(sSMap.get(key) <= fSMap.get(key))
countCow = countCow + sSMap.get(key);
else
countCow = countCow + fSMap.get(key);
}
}
return "countCow = " + countCow + " countBull = " + countBull;
}
private static void updateMapOfCharsCount(Map<String, Integer> fsMap, char c) {
String key1 = String.valueOf(c);
if (fsMap.containsKey(key1)) {
fsMap.put(key1, fsMap.get(key1) + 1);
} else
fsMap.put(key1, 1);
}
I am doing recursion for a given phone number and print all the possible string representation of the number. The problem is in loop for (int j=0;j<ops;j++) { the size of the "perm" ArrayList keep increasing in every iteration. I want to get fixed pattern and add new number e.g perm = 11 and call recursion with tperm=110,111,112.
import java.util.*;
public class phoneNum {
public static void getSt ( List<Integer>list , List<Integer> perm ) {
Integer len = list.size();
Integer len1 = perm.size();
Integer ops = 0;
if (len == len1) {
for(int k=0;k<len;k++) {
System.out.print(" " + list.get(k));
}
for(int k=0;k<len;k++) {
System.out.print(" " + perm.get(k));
}
System.out.print("====");
System.out.print(getPattrn(list,perm));
System.out.println("\n");
} else {
for (int i=0; i<len1+1; i++) {
if(list.get(i) == 7 || list.get(i) == 9) {
ops = 4;
} else {
ops = 3;
}
for (int j=0;j<ops;j++) {
List<Integer> tperm = new ArrayList<Integer>(perm);
tperm.add(i,j);
System.out.println("Size=" + tperm.size() + " ---" + perm.size());
getSt(list,tperm);
}
}
}
}
You can add tperm.remove(i) at the end of inner for loop.