Counting output appearances on screen - java

How to count number of occurrences when System.out prints something? In my example user input TR* and lets say program founds TRR; TRC; TRD, ect, but I can't manage to count number of outputs (3 in this example). I want to make so if 0 solutions are found to print out "No solutions found".
Any tips on how to make this work.
if (length == 3) {
System.out.println("Possible solutions:");
n = 0;
for (int i = 0; i < list3.size(); i++) {
String s = (String) list3.get(i);
if (s.matches(user_input))
System.out.println(s);
}
System.out.println(n);
if (n == 1) {
System.out.println("No solutions found");
}
}

Try this:
package stackoverflow.counting;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<String> list3 = new ArrayList<>();
list3.add("TRA");
list3.add("XXX");
int length = 3;
String user_input = "TR.*";
if (length == 3) {
System.out.println("Possible solutions:");
int n = 0;
for (int i = 0; i < list3.size(); i++) {
String s = list3.get(i);
if (s.matches(user_input)) {
System.out.println(s);
n++;
}
}
System.out.println(n);
if (n == 0) {
System.out.println("No solutions found");
}
}
}
}
Output is:
Possible solutions:
TRA
1
Please also note that TR* is probably not the kind of regular expression you need. Use TR.* instead.

Try with this approach:
int countOfSystemPrintedOutput = 0;
if (length == 3) {
System.out.println("Possible solutions:");
for (String s : list3) {
if(s.matches(user_input)){
System.out.println(s);
countOfSystemPrintedOutput++;
}
}
if (countOfSystemPrintedOutput == 0) {
System.out.println("No solutions found");
}
else{
System.out.println("System.out.println called " + countOfSystemPrintedOutput + "times");
}
}

Related

How do I Input comma sign without Number format in java

I wrote this code with out number format class.
My Question Is: Is there any way to make this process shorter without Number Format Class.
String num=JOptionPane.showInputDialog("enter number");
Int len=num.length();
String res="";
if (len==4){
res=num.charAt(0)+","+num.substring(1);
}
else if(len==5){
res=num.substring(0,2)+","+num.substring(2);
}
else if(len==6){
res=num.substring(0,3)+","+num.substring(3);
}
else if(len==7){
res=num.charAt(0)+","+num.substring(1,4)+","+num.substring(4);
}
else if(len==8){
res=num.substring(0,2)+","+num.substring(2,5)+","+num.substring(5);
}
else if(len==9){
res=num.substring(0,3)+","+num.substring(3,6)+","+num.substring(6);
}
else if(len==10){
res=num.charAt(0)+","+num.substring(1,4)+","+num.substring(4,7)+","+num.substring(7);
}
System.out.println(res);
Something like the following:
public static void main(String[] args) {
int myInt = 1234567890;
StringBuilder str = new StringBuilder(String.valueOf(myInt));
int length = str.length();
int count = 0;
for (int i = length; i > 0; i--) {
count++;
if (count % 3 == 0 && count != length) {
str.insert(i - 1, ",");
}
}
System.out.println("Formatted Number: " + str);
}

I want the string pattern aabbcc to be displayed as 2a2b2c

I have somehow got the output with the help of some browsing. But I couldn't understand the logic behind the code. Is there any simple way to achieve this?
public class LetterCount {
public static void main(String[] args)
{
String str = "aabbcccddd";
int[] counts = new int[(int) Character.MAX_VALUE];
// If you are certain you will only have ASCII characters, I would use `new int[256]` instead
for (int i = 0; i < str.length(); i++) {
char charAt = str.charAt(i);
counts[(int) charAt]++;
}
for (int i = 0; i < counts.length; i++) {
if (counts[i] > 0)
//System.out.println("Number of " + (char) i + ": " + counts[i]);
System.out.print(""+ counts[i] + (char) i + "");
}
}
}
There are 3 conditions which need to be taken care of:
if (s.charAt(x) != s.charAt(x + 1) && count == 1) ⇒ print the counter and character;
if (s.charAt(x) == s.charAt(x + 1)) ⇒ increase the counter;
if (s.charAt(x) != s.charAt(x + 1) && count >= 2) ⇒ reset to counter 1.
{
int count= 1;
int x;
for (x = 0; x < s.length() - 1; x++) {
if (s.charAt(x) != s.charAt(x + 1) && count == 1) {
System.out.print(s.charAt(x));
System.out.print(count);
}
else if (s.charAt(x)== s.charAt(x + 1)) {
count++;
}
else if (s.charAt(x) != s.charAt(x + 1) && count >= 2) {
System.out.print(s.charAt(x));
System.out.print(count);
count = 1;
}
}
System.out.print(s.charAt(x));
System.out.println(count);
}
The code is really simple.It uses the ASCII value of a character to index into the array that stores the frequency of each character.
The output is simply got by iterating over that array and which character has frequency greater than 1, print it accordingly as you want in the output that is frequency followed by character.
If the input string has same characters consecutive then the solution can be using space of O(1)
For example in your string aabbcc, the same characters are consecutive , so we can take advantage of this fact and count the character frequency and print it at the same time.
for (int i = 0; i < str.length(); i++)
{
int freq = 1;
while((i+1)<str.length()&&str.charAt(i) == str.charAt(i+1))
{++freq;++i}
System.out.print(freq+str.charAt(i));
}
You are trying to keep count of the number of times each character is found. An array is referenced by an index. For example, the ASCII code for the lowercase letter a is the integer 97. Thus the count of the number of times the letter a is seen is in counts[97]. After every element in the counts array has been set, you print out how many have been found.
This should help you understand the basic idea behind how to approach the string compression problem
import java.util.*;
public class LetterCount {
public static void main(String[] args) {
//your input string
String str = "aabbcccddd";
//split your input into characters
String chars[] = str.split("");
//maintain a map to store unique character and its frequency
Map<String, Integer> compressMap = new LinkedHashMap<String, Integer>();
//read every letter in input string
for(String s: chars) {
//java.lang.String.split(String) method includes empty string in your
//split array, so you need to ignore that
if("".equals(s))
continue;
//obtain the previous occurances of the character
Integer count = compressMap.get(s);
//if the character was previously encountered, increment its count
if(count != null)
compressMap.put(s, ++count);
else//otherwise store it as first occurance
compressMap.put(s, 1);
}
//Create a StringBuffer object, to append your input
//StringBuffer is thread safe, so I prefer using it
//you could use StringBuilder if you don't expect your code to run
//in a multithreaded environment
StringBuffer output = new StringBuffer("");
//iterate over every entry in map
for (Map.Entry<String, Integer> entry : compressMap.entrySet()) {
//append the results to output
output.append(entry.getValue()).append(entry.getKey());
}
//print the output on console
System.out.println(output);
}
}
class Solution {
public String toFormat(String input) {
char inChar[] = input.toCharArray();
String output = "";
int i;
for(i=0;i<input.length();i++) {
int count = 1;
while(i+1<input.length() && inChar[i] == inChar[i+1]) {
count+=1;
i+=1;
}
output+=inChar[i]+String.valueOf(count);
}
return output;
}
public static void main(String[] args) {
Solution sol = new Solution();
String input = "aaabbbbcc";
System.out.println("Formatted String is: " + sol.toFormat(input));
}
}
def encode(Test_string):
count = 0
Result = ""
for i in range(len(Test_string)):
if (i+1) < len(Test_string) and (Test_string[i] == Test_string[i+1]):
count += 1
else:
Result += str((count+1))+Test_string[i]
count = 0
return Result
print(encode("ABBBBCCCCCCCCAB"))
If you want to get the correct count considering the string is not in alphabetical order. Sort the string
public class SquareStrings {
public static void main(String[] args) {
SquareStrings squareStrings = new SquareStrings();
String str = "abbccddddbd";
System.out.println(squareStrings.manipulate(str));
}
private String manipulate(String str1) {
//convert to charArray
char[] charArray = str1.toCharArray();
Arrays.sort(charArray);
String str = new String(charArray);
StringBuilder stbuBuilder = new StringBuilder("");
int length = str.length();
String temp = "";
if (length > 1) {
for (int i = 0; i < length; i++) {
int freq = 1;
while (((i + 1) < length) && (str.charAt(i) == str.charAt(i + 1))) {
++freq;
temp = str.charAt(i) + "" + freq;
++i;
}
stbuBuilder.append(temp);
}
} else {
return str + "" + 1;
}
return stbuBuilder.toString();
}
}
Kotlin:
fun compressString(input: String): String {
if (input.isEmpty()){
return ""
}
var result = ""
var count = 1
var char1 = input[0]
for (i in 1 until input.length) {
val char2 = input[i]
if (char1 == char2) {
count++
} else {
if (count != 1) {
result += "$count$char1"
count = 1
} else {
result += "$char1"
}
char1 = char2
}
}
result += if (count != 1) {
"$count$char1"
} else {
"$char1"
}
return result
}

Bulls and Cows game?

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);
}

Finding anagram using array list

I'm trying to find if two string are anagrams of each other:
void anagram(String a, String b) {
List<Character> bcopy = new ArrayList<Character>();
for (int i = 0; i < b.length(); i++)
bcopy.add(b.charAt(i));
if (b.length() == 0 || a.length() == 0) {
System.out.println("Exit");
} else {
for (int i = 0; i < a.length(); i++) {
char temp = a.charAt(i);
if (bcopy.contains(temp)) {
System.out.println("match found" + temp);
bcopy.remove(temp);
}
for (char j : bcopy) {
System.out.println("Values" + j);
}
}
}
}
I keep getting an out of bounds error at the remove() line. Can someone please tell me how I reach the array bounds when I'm searching by the object availability? What am I missing here?
The problem is you're using the int-argument version of remove() since the char temp is being treated as an int. Here's a workaround:
bcopy.remove(Character.valueOf(temp));
By the way a better way to test for anagrams would be something like:
char[] c1 = a.toCharArray();
char[] c2 = b.toCharArray();
Arrays.sort(c1);
Arrays.sort(c2);
return Arrays.equals(c1, c2); // true -> anagram, false -> not anagram
there is another algorithm which might be more suitable to the task. it computes the letter frequencies for strings of equal lengths.
for simplicity i assume that the set of all characters involved can be represented in one of the common 8 bit codepages.
void anagram(String a, String b) {
int freqa[256], freqb[256];
if (b.length() == 0 || a.length() == 0) {
System.out.println("Exit");
return;
}
for (int i = 0; i < b.length(); i++) {
freqa[(int) a.charAt(i)]++;
freqb[(int) b.charAt(i)]++;
}
for (i = 0; i < 256; i++) {
if (freqa[i] <> freqb[i]) {
System.out.println("Exit");
return;
}
}
System.out.println("match found: '" + a + "', '" + b + "'");
}
There is a problem with your code. You are removing the items from a list however the loop is running 'n' times is the string length is 'n'.
So if item is removed from the list and the loop count reaches a number which Index is removed from list it will give the exception. You can keep a count instead of removing the items. I modified your code little bit which is working fine now.
Please check
import java.util.ArrayList;
import java.util.List;
class Anagram{
void anagram(String a, String b) {
List<Character> bcopy = new ArrayList<Character>();
int count=0;
for (int i = 0; i < b.length(); i++)
bcopy.add(b.charAt(i));
if (b.length() == 0 || a.length() != b.length()) {
System.out.println("Two strings are not anagram");
} else {
for (int i = 0; i < a.length(); i++) {
char temp = a.charAt(i);
if (bcopy.contains(temp)) {
//System.out.println("match found" + temp);
//bcopy.remove(temp);-- Here list size was reduced but the loop was constant, because list size is less than a.length() now it was giving error
//System.out.println(c);
count++;
}
}
if(count==a.length()) {
System.out.println("Two strings are anagram");
}
else {
System.out.println("Two strings are not anagram");
}
}
}
}
public class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Anagram a=new Anagram();
a.anagram("abs", "ass");
}
}

java recurrsion with ArrayList not working

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.

Categories

Resources