So I need to write a method which allows me to find the next number without using anything but String and StringBuffer classes (so no parse or whatsoever).
when I run my code everything seems to work fine except for the last input (String k). It causes an outofbounds error in the while loop. But I don't see how It could do that because since my boolean should be false (I think because it only consists out of nine's) it shouldn't enter te loop in the first place.
public class Palindroom {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="347"
+ ""; System.out.println("s "+increment(s));
String p="199919";System.out.println(" p "+increment(p));
String d="199999";System.out.println( " d "+increment(d));
String k ="999999";System.out.println(" k "+increment(k));
}
static String increment (String s) {
StringBuffer sb = new StringBuffer (s);
char laatst = sb.charAt(sb.length()-1);
int laatste = sb.length()-1;
if(laatst == '9') {
int a = 1;
boolean nine = false;
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) != 9 ) {
nine = true;
} else nine = false;
}
if (nine == true) {
while (sb.charAt(sb.length()- a) == '9'){
sb.setCharAt(sb.length()- a, '0' );
a++;
}
sb.setCharAt(sb.length()-a, ((char)((sb.charAt(sb.length()-a)+1))));
} else if( nine == false) {
System.out.println("negen "+nine);
sb.insert(0, '1');
for (int i = 0; i < sb.length(); i++) {
if(sb.charAt(i)=='9') {
sb.setCharAt(i, '0');
}
}
}
} else {
sb.setCharAt(laatste,(char) (laatst+1) );
}
return sb.toString();
}
}
EDIT: based on the comments :) . Correct your logic for true and false.
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) == '9' ) {
nine = true;
}
else
{
nine = false;
break;
}
}
also you are getting an out of bounds exception because
while (sb.charAt(sb.length()- a) == '9'){
sb.setCharAt(sb.length()- a, '0' );
a++;
}
after the fifth iteration sb.length()- a will be negative when the control comes to check the condition.
you are not getting the same error in other cases because the control never goes to the while loop (as nine == false)
OLD OBSERVATION
in
if(laatst == '9') {
int a = 1;
boolean nine = false;
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) != 9 ) {
nine = true;
} else nine = false;
}
you are doing
if (sb.charAt(i) != 9 ) {
nine = true;
} else nine = false;
instead do, if you want to compare against the character 9,
if (sb.charAt(i) != '9' ) {
nine = true;
} else nine = false;
otherwise you are just comparing the ASCII value of the charAt(i) and so your boolean will only be false, if charAt(i) has ascii value 9
The problem is that when your string consists only of 9s, your while loop causes a to exceed sb.length(). Then, you end up trying to access the character at place -1 of sb.
Related
public class AnagramUnoptimized {
public static void main(String[] args) {
String a = "good";
String b = "ogod";
boolean isAnagram = false;
String c = a.toLowerCase();
String d = b.toLowerCase();
if(c.length()==d.length()) {
boolean [] Visited = new boolean[a.length()];
for (int i = 0; i < c.length(); i++) {
isAnagram = false;
for (int j = 0; j < d.length(); j++) {
if (c.charAt(i) == d.charAt(j) && Visited[j]==false) {
isAnagram = true;
Visited[j] = true;
}
}
if (isAnagram == false) {
break;
}
}
}
if(isAnagram==true){
System.out.println("The given Strings are Anagrams");
}
else{
System.out.println("The given Strings are not Anagrams");
}
}
}
I used a Visited boolean array to check for repeated items but its now showing "Not anagram" for all inputs....
Can you tell me why its showing "Not anagram" if the strings have repeating elements??
The problem with your code is you are continuing with the loop even when visited[j] is changed to true whereas you need to break the inner loop at this point. Do it as follows:
for (int j = 0; j < d.length(); j++) {
if (c.charAt(i) == d.charAt(j) && visited[j] == false) {
isAnagram = true;
visited[j] = true;
break;
}
}
The output after this change:
The given Strings are Anagrams
A better way to do it would be as follows:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String a = "good";
String b = "ogod";
char[] first = a.toLowerCase().toCharArray();
char[] second = b.toLowerCase().toCharArray();
Arrays.sort(first);
Arrays.sort(second);
boolean isAnagram = Arrays.equals(first, second);
if (isAnagram == true) {
System.out.println("The given Strings are Anagrams");
} else {
System.out.println("The given Strings are not Anagrams");
}
}
}
Output:
The given Strings are Anagrams
In your code you should break the inner for loop when the
condition "if (c.charAt(i) == d.charAt(j) && Visited[j]==false)"
has been meet. Because it is still looping through the second stiring and if it will meet the same char one angain it will change the value of Visited[] to true two times, leading to an error. It this example this is the case witch char 'o'. Adding " break; " at the end of the if statement should fix the problem.
I'm trying to write a function that takes in a String and returns the greatest number of consecutive equivalent vowels in the String.
Here's my attempt:
public static final String VOWELS = "aeiou";
public static int consecutiveVowelsInLine(String line) {
int longestVowels = 0;
int candidateLength = 0;
for (int i = 0; i < line.length() - 1; i++) {
if (isVowel(line.charAt(i))) {
if (line.charAt(i) == line.charAt(i+1)) {
candidateLength++;
}
} else {
candidateLength = 0;
}
longestVowels = Math.max(longestVowels, candidateLength);
}
return longestVowels;
}
public static boolean isVowel(char c) {
VOWELS.contains(c.toLowerCase());
}
The problem is this doesn't handle the case where the String is a single character that's a vowel. So if the String is just "a", my code gives back 0 instead of 1.
As said before, the vowels have to be the same.
Testcases:
a -> 1
b -> 0
ae -> 1
aeae -> 1
aab -> 2
aba -> 1
abee -> 2
I think you aim to do too much in the loop: instead of looking to the character next, concentrate on the current character and maintain a state that stores the previous vowel:
public static int consecutiveVowelsInLine(String line) {
int longestVowels = 0;
int candidateLength = 0;
char vowel = 'b'; //b is not a vowel
for (int i = 0; i < line.length(); i++) {
char ci = line.charAt(i);
if (isVowel(ci)) {
if (ci == vowel) { //the same as the other one
candidateLength++;
} else {
candidateLength = 1;
}
vowel = ci;
} else {
candidateLength = 0;
vowel = 'b';
}
longestVowels = Math.max(longestVowels, candidateLength);
}
return longestVowels;
}
Here vowel stores the current vowel sequences you are working with. In the beginning we use b, simple because that is not a vowe. In case we encounter a vowel, that vowel is stores in vowel and we update the candidateLength accordingly. In case we encounter a non-vowel, we set vowel back to b (or another non-vowel).
Demo:
There were some problems with your isVowel method as well, a running implementation with a few testcases can be found here.
Here's one problem:
if (line.charAt(i) == line.charAt(i+1)) {
candidateLength++;
}
If the string is only one character, you're checking the character against null. Add a check, something like this:
if (line.length() == 1 && isVowel(line.charAt(0)) {
etc.
}
Simply change it like:
public static int consecutiveVowelsInLine( String line ){
int result = findConsecutiveMaxValue( line );
if( result == 0 ){
result = findSingleVowel( line );
}
return result;
}
private static int findSingleVowel( String line ){
for( int i = 0; i < line.length(); i++ ){
if( isVowel( line.charAt( i ) ) ){ return 1; }
}
return 0;
}
private static int findConsecutiveMaxValue( String line ){
int longestVowels = 0;
int candidateLength = 0;
for( int i = 0; i < line.length() - 1; i++ ){
if( isVowel( line.charAt( i ) ) ){
if( line.charAt( i ) == line.charAt( i + 1 ) ){
candidateLength++;
}
}
else{
candidateLength = 0;
}
longestVowels = Math.max( longestVowels, candidateLength );
}
return longestVowels;
}
Change:
if (line.charAt(i) == line.charAt(i+1)) {
candidateLength++;
}
to:
if (candidateLength == 0 || line.charAt(i) == line.charAt(i-1)) {
candidateLength++;
}
Additionally the condition in for() loop looks suspicious - use getLength() instead of getLength()-1.
I have written this algorithm to determine if a string has all unique characters, but its giving me some errors. Can anyone help me with improving the code.
It is giving me the error that I am duplicating the uniquechar1 method but I am passing it to an if statement.
package nospacesinstrings;
import java.util.Scanner;
public class uniquechar {
public static boolean uniquechar1(String s) {
if (s == null || s.length() > 0 ) {
return false;
}
for (int i = 0 ;i < s.length();i++) {
for (int j = s.length() ;j > 0;j--) {
if (i == j)
return false;
else
return true;
}
}
}
public static void main(String[] args) {
String s ;
System.out.println("Enter the string ");
Scanner in = new Scanner(System.in);
s = in.nextLine();
if (uniquechar1(s) == true) {
System.out.println("String has all the unique characters ");
} else {
System.out.println("String does not have all the unique characters ");
}
}
}
Your check at the top looks backwards. I think you meant to put s.length() < 1 instead of s.length() > 0
You also are returning a value before you have finished iterating over your string. You should only return true if you iteration through the complete string without returning false
Also, your double loop will always end up comparing each character to itself so the method will return false. To do it using a for each loop, you need to stop before you get to the currently checked index.
for (int i = 0 ;i < s.length();i++){
for (int j = s.length() ;j > i;j--){
if (i == j )
{return false ;}
}
return true;
you could also avoid traversing twice down the string by collecting characters as you go. Something like this:
Stack<char> stack = new Stack<char>();
for (int i = 0 ;i < s.length();i++){
if (stack.Contains(s[i]))
{return false ;}
stack.Push(s[i]);
}
return true ;
Lastly, if you should research character comparison. Are you looking to fail if any two any character even if they are different cases (i.e. A == a or A != a)?
This algorithm should work. I'm assuming there are no numbers in the string. (Edited to correct code).
public static boolean uniquechar1(String s)
{
if (s == null || s.length() == 0 )
return true;
// make sure no letter in alphabet occurs twice in the string.
boolean[] letters = new boolean[26];
s = s.toUpperCase();
s = s.replaceAll(" ", "");
for (int i = 0; i < s.length(); i++)
{
char ch = s.charAt(i);
ch = (char) (ch - 'A');
if (letters[ch] == true)
return false;
else
letters[ch] = true;
}
return true;
}
Here is a tester method.
public static void main(String[] args)
{
System.out.println( uniquechar1("Hello World!") );
System.out.println( uniquechar1("A A") );
System.out.println( uniquechar1("ABC") );
}
Outputs:
false
false
true
Hi All I'm using the following function to check the Consecutive digits in java
The issue im facing here is it works for the first Consecutive digits only
For example it work for 123456789123456XXXX
but want this to work Consecutive any where
XXXX123456789123456 or XX123456789123456XX
Update
Now if i found 13 Consecutive digits then i need to pass all Consecutive digits to the mask function
and my result should be
something like this
for input 123456789123456XXXX result should be 123456%%%%%3456XXXX
for input XXXX123456789123456 result should be XX123456%%%%%3456XX
Please help me to solve this
My Code
public void checkPosCardNoAndMask(String cardNo) {
String maskNumber = "";
String starValue = "";
boolean isConsecutive = false;
int checkConsecutive = 0;
for (int i = 0, len = cardNo.length(); i < len; i++) {
if (Character.isDigit(cardNo.charAt(i))) {
maskNumber = maskNumber + cardNo.charAt(i);
} else {
if (checkConsecutive >= 13)
isConsecutive = true;
else
break;
starValue = starValue + cardNo.charAt(i);
}
checkConsecutive++;
}
if (isConsecutive)
{
cardNo = maskCCNumber(maskNumber) + starValue;
System.out.printIn("Consecutive found!!:"+cardNo);
}
else
{
System.out.printIn("Consecutive not found!!:"+cardNo);
}
}
Masking logic
public String maskCCNumber(String ccNo)
{
String maskCCNo = "";
for (int i = 0; i < ccNo.length(); i++)
{
if (i > 5 && i < ccNo.length() - 4)
{
maskCCNo = maskCCNo + '%';
}
else
{
maskCCNo = maskCCNo + ccNo.charAt(i);
}
}
return maskCCNo;
}
With regex you can do this way:
String str = "XX123456789123456XX";
if (str.matches(".*\\d{13}.*")) {
System.out.println(true);
Pattern compile = Pattern.compile("\\d+");
Matcher matcher = compile.matcher(str);
matcher.find();
String masked = maskCCNumber(matcher.group());//send 123456789123456 and returns 123456%%%%%3456
String finalString=str.replaceAll("\\d+", masked);// replace all digits with 123456%%%%%3456
System.out.println(finalString);
}
Output:
true
XX123456%%%%%3456XX
There are few issues:
You're breaking out of else, when first time you find non-digit character. This will skip any consecutive digit coming after that. So, you should not break.
In fact, you should add break out of the loop once you find 13 consecutive digit.
You're not really looking for consecutive digits, but just total number of non-cosnecutive digits. At least the current logic without break would work this way. You should reset the checkConsecutive variable to 0 when you find a non-digit character.
So, changing your for loop to this will work:
for (int i = 0, len = cardNo.length(); i < len; i++)
{
if (Character.isDigit(cardNo.charAt(i))) {
checkConsecutive++;
} else if (checkConsecutive == 13) {
isConsecutive = true;
break;
} else {
checkConsecutive = 0;
}
}
Of course I don't know what is starValue and maskValue, so I've removed it. You can add it appropriately.
BTW, this problem can also be solved with regex:
if (cardNo.matches(".*\\d{13}.*")) {
System.out.println("13 consecutive digits found");
}
try this
public void checkPosCardNoAndMask(String cardNo) {
if (cardNo.matches("[0-9]{13,}")) {
System.out.println("Consecutive found!!");
} else {
System.out.println("Consecutive not found!!");
}
}
If you want to work with your code then make one change
public void checkPosCardNoAndMask(String cardNo) {
String maskNumber = "";
String starValue = "";
boolean isConsecutive = false;
int checkConsecutive = 0;
for (int i = 0, len = cardNo.length(); i < len; i++) {
if (Character.isDigit(cardNo.charAt(i))) {
maskNumber = maskNumber + cardNo.charAt(i);
checkConsecutive++;
} else {
if (checkConsecutive >= 13)
{isConsecutive = true;break;}
else
checkConsecutive=0;
starValue = starValue + cardNo.charAt(i);
}
}
if (isConsecutive) {
System.out.printIn("Consecutive found!!");
} else {
System.out.printIn("Consecutive not found!!");
}
}
try this
public static void checkPosCardNoAndMask(String cardNo) {
int n = 1;
char c1 = cardNo.charAt(0);
for (int i = 1, len = cardNo.length(); i < len && n < 13; i++) {
char c2 = cardNo.charAt(i);
if (c2 >= '1' && c2 <= '9' && (c2 - c1 == 1 || c2 == '1' && c1 == '9')) {
n++;
} else {
n = 0;
}
c1 = c2;
}
if (n == 13) {
System.out.println("Consecutive found!!");
} else {
System.out.println("Consecutive not found!!");
}
}
My understanding is that you want to mask card numbers in a string. There is one external dependency in following code http://commons.apache.org/proper/commons-lang/ for StringUtils
/**
* Returns safe string for cardNumber, will replace any set of 13-16 digit
* numbers in the string with safe card number.
*/
public static String getSafeString(String str) {
Pattern CARDNUMBER_PATTERN = Pattern.compile("\\d{13,16}+");
Matcher matcher = CARDNUMBER_PATTERN.matcher(str);
while (matcher.find()) {
String cardNumber = matcher.group();
if (isValidLuhn(cardNumber)) {
str = StringUtils.replace(str, cardNumber, getSafeCardNumber(cardNumber));
}
}
return str;
}
public static boolean isValidLuhn(String cardNumber) {
if (cardNumber == null || !cardNumber.matches("\\d+")) {
return false;
}
int sum = 0;
boolean alternate = false;
for (int i = cardNumber.length() - 1; i >= 0; i--) {
int n = Integer.parseInt(cardNumber.substring(i, i + 1));
if (alternate) {
n *= 2;
if (n > 9) {
n = (n % 10) + 1;
}
}
sum += n;
alternate = !alternate;
}
return (sum % 10 == 0);
}
/**
* Returns safe string for cardNumber, will keep first six and last four
* digits.
*/
public static String getSafeCardNumber(String cardNumber) {
StringBuilder sb = new StringBuilder();
int cardlen = cardNumber.length();
if (cardNumber != null) {
sb.append(cardNumber.substring(0, 6)).append(StringUtils.repeat("%", cardlen - 10)).append(cardNumber.substring(cardlen - 4));
}
return sb.toString();
}
I'm writing a calculator code that solves the input whats given in string. All is good, except when it gets a negative result in the parentheses it fails badly because two operations get next to each other:
1+2*(10-11) >> 1+2*(-1) >> 1+2*-1
So where *- is, it gets "" (nothing) in the BigDecimal's constructor.
I know what's the problem, but how can I solve it?
import java.math.BigDecimal;
import java.util.ArrayList;
public class DoMath {
public static void main(String[] args) {
// Test equation goes here.
String number = "95.3+43.23*(10-11.1)";
System.out.println(doMath(number));
}
public static BigDecimal doMath(String input) {
StringBuilder builtInput = new StringBuilder(input);
StringBuilder help = new StringBuilder();
// Check if there are parenthesis in the equation.
boolean noParenthesis = true;
for (int i = 0; i < builtInput.length(); i++) {
if (builtInput.charAt(i) == 40) {
noParenthesis = false;
break;
}
}
if (noParenthesis) { // If there are no parenthesis, calculate the equation!
return calculateAndConvert(builtInput);
} else { // If there are parenthesis, breakdown to simple equations!
int parenthesePair = 0;
// Start extracting characters from the builtInput variable.
for (int i = 0; i < builtInput.length(); i++) {
// Start where we find a parentheses opener.
if (builtInput.charAt(i) == 40) {
parenthesePair = 1;
builtInput.deleteCharAt(i);
for (int j = i; j < builtInput.length(); j++) {
// If we find another opener, add one to parenthesePair variable.
if (builtInput.charAt(j) == 40) {
parenthesePair++;
}
// If we find a closer, subtract one from the given variable.
if (builtInput.charAt(j) == 41) {
parenthesePair--;
}
// If we have found the matching pair, delete it and break the for loop.
if (parenthesePair == 0) {
builtInput.deleteCharAt(j);
builtInput.insert(j, doMath(help.toString()));
break;
}
help.append(builtInput.charAt(j));
builtInput.deleteCharAt(j);
j--;
}
break;
}
}
}
System.out.println(builtInput);
return doMath(builtInput.toString());
}
public static BigDecimal calculateAndConvert(StringBuilder input) {
ArrayList<BigDecimal> listOfNumbers = new ArrayList<BigDecimal>();
StringBuilder numBay = new StringBuilder();
StringBuilder operations = new StringBuilder();
// If the first character is -, the first number is negative.
boolean firstIsNegative = false;
if (input.charAt(0) == 45) {
firstIsNegative = true;
input.deleteCharAt(0);
}
// Converting to numbers.
while (input.length() != 0) {
// If the character is a number or a dot, put it in the numBay variable and delete the char.
if (input.charAt(0) >= 48 && input.charAt(0) <= 57 || input.charAt(0) == 46) {
numBay.append(input.charAt(0));
// If the character is not a number, put it in the operations variable
// and save the number in the list (not operator characters are filtered)
} else {
listOfNumbers.add(new BigDecimal(numBay.toString()));
numBay.setLength(0);
operations.append(input.charAt(0));
}
// Delete the character.
input.deleteCharAt(0);
}
listOfNumbers.add(new BigDecimal(numBay.toString()));
// Setting first number to negative if it's needed.
if (firstIsNegative) {
listOfNumbers.set(0, listOfNumbers.get(0).negate());
}
// Calculate the result from the list and operations and return it.
return calculate(listOfNumbers, operations);
}
public static BigDecimal calculate(ArrayList<BigDecimal> list, StringBuilder ops) {
BigDecimal momentaryResult;
// Check for a multiply operation - if there is one, solve it.
for (int i = 0; i < ops.length(); i++) {
if (ops.charAt(i) == 42) {
momentaryResult = list.get(i).multiply(list.get(i + 1));
list.remove(i);
list.set(i, momentaryResult);
ops.deleteCharAt(i);
i--;
}
}
// Check for a divide operation - if there is one, solve it.
for (int i = 0; i < ops.length(); i++) {
if (ops.charAt(i) == 47) {
momentaryResult = list.get(i).divide(list.get(i + 1));
list.remove(i);
list.set(i, momentaryResult);
ops.deleteCharAt(i);
i--;
}
}
// Check for a subtract operation - if there is one, solve it.
for (int i = 0; i < ops.length(); i++) {
if (ops.charAt(i) == 45) {
momentaryResult = list.get(i).subtract(list.get(i + 1));
list.remove(i);
list.set(i, momentaryResult);
ops.deleteCharAt(i);
i--;
}
}
// Check for a plus operation - if there is one, solve it.
for (int i = 0; i < ops.length(); i++) {
if (ops.charAt(i) == 43) {
momentaryResult = list.get(i).add(list.get(i + 1));
list.remove(i);
list.set(i, momentaryResult);
ops.deleteCharAt(i);
i--;
}
}
// Return with the one remaining number that represents the result.
return list.get(0);
}
}
Edit: or would it be easier to write a new code with a different algorithm...?
I would post this as a comment to your question, but I do not have the required reputation to do so.
Anyway, since you have already recognized that the bug is the "operator" *- couldn't you make a method that would fix this problem by replacing the plus operator immediately before by a minus? Like this:
1+2*-1 >>> 1-2*1
If you want I can write you the code. But maybe it will be easier for you to adapt a solution like this in your code that is already working.
Edit - 1:
Obviously, the code should also treat the following cases:
1-2*-1 >>> 1+2*1
2*-1 >>> -2*1
Edit - 2:
Here is the code I managed to make. Let me know if you find any errors.
public int countChar(String str, char chr) {
int count = 0;
for (int k = 0; k < str.length(); k++) {
if (str.charAt(k) == chr)
count++;
}
return count;
}
public String fixBug(String eq) {
boolean hasBug = eq.contains("*-");
if (hasBug) {
String subeq;
int indbug, indp, indm;
eq = eq.replace("*-", "#");
int N = countChar(eq, '#');
for (int k = N; k > 0; k--) {
indbug = eq.indexOf('#');
subeq = eq.substring(0, indbug);
indp = subeq.lastIndexOf('+');
indm = subeq.lastIndexOf('-');
if (indp == -1 && indm == -1) {
eq = "-" + eq;
} else if (indp > indm) {
eq = eq.substring(0, indp) + '-' + eq.substring(indp + 1);
} else {
eq = eq.substring(0, indm) + '+' + eq.substring(indm + 1);
}
}
eq = eq.replace("#", "*");
}
return eq;
}