public boolean isNumber(String t) {
for (int i = 0, i<= 9, i++) {
if t.equals(i) {
return true;
}
}
return false;
}
Copypastad the wrong method originally -_-
I have this inside a class compiling with this error:
data_structures/ExpressionEvaluator.java:40: illegal start of type for (int i = 0, i< 10, i++) {
Use semi-colons instead of commas.
for(int i = 0; i < 10; i++) {
//do stuff
}
You should use semi-colon and your if should be surrounded with brackets.
public boolean isNumber(String t) {
for (int i = 0; i <= 9; i++) {
if (t.equals(i)) {
return true;
}
}
return false;
}
I would suggest reading Language Basics
Semicolons separate the qualities of a for loop. Also, the condition of your if block must be surrounded by parentheses.
public boolean isNumber(String t) {
for (int i = 0; i <= 9; i++) {
if (t.equals(i)) {
return true;
}
}
return false;
}
public boolean isNumber(String t) {
for (int i = 0; i<= 9; i++) {
if( t.equals(i) ){
return true;
}
}
return false;
}
1 . use ";" replace of ","
2 .
if(boolean) {
//do stuff
}
Your method only verifies if the String that you passed is a digit, not a number (a number can have more than one digit). You could verifiy it only using a char and calling the ,Character.isDigit
char c = '1';
boolean isDigit = Character.isDigit(c);
If you really want to create your own method, passing a String param, I suggest you to modify like this:
public boolean isDigit(String t) {
return t.length() == 1 && Character.isDigit(t.charAt(0));
}
Related
I write this code but I don't know why doesn't work.
static boolean findeText(String text1, String text2) {
char c1[] = text1.toCharArray();
char c2[] = text2.toCharArray();
boolean b = false;
for (int i = 0; i <= c1.length - c2.length; i++) {
for (int j = 0, h = i; j <= c2.length; j++, h++) {
if (i == c2.length) {
return true;
}
if (c1[h] != c2[j]) {
b = false;
break;
}
}
}
return b;
}
I want find text 2 in text 1 and after that return true or false.
if you want to check if some string contains any other string, just use contains() method.
In your case that would be
return text1.contains(text2);
Plus you should always write your code in defensive way. That means you should always make sure there will be no NullPointerException etc. So in your particular case if someone pass either null text1 or null text2, your program will crash.
Above you had NPE in line
if (c1[h] != c2[j])
I have modified your code slightly to get output as requirement.
static boolean findeText(String text1, String text2) {
if ((text1== null) ||(text2==null) || (text1.isEmpty()) || (text2.isEmpty())) {
System.out.println("Invalid input");
return false;
}
char c1[] = text1.toCharArray();
char c2[] = text2.toCharArray();
for (int i = 0; i <= c1.length - c2.length; i++) {
int count = 0;
for (int j = 0; j < c2.length; j++) {
if (c1[i + j] == c2[j]) {
count = count + 1;
}
if (count == c2.length) {
return true;
}
}
}
return false;
}
Using regular foloop, getting correct output, using for each loop throws ArrayOutOfBound exception. Can anyone explain?
public class FindTheDiff {
public static void main(String args[]){
System.out.println(isAnagramRecursion("all","laa"));
}
private static boolean isAnagramRecursion(String param1, String param2) {
int[] arr = new int[122];
char[] param1Lower = param1.toLowerCase().toCharArray();
char[] param2Low2 = param2.toLowerCase().toCharArray();
for (char ch1 : param1Lower) {
arr[ch1] = arr[ch1]+1;
}
for (char ch2 : param2Low2){
arr[ch2] = arr[ch2]-1;
}
for (int i :arr) {
if(arr[i] != 0)
return false;
}
/*for (int i = 0; i < arr.length; i++) {
if(arr[i] != 0)
return false;
}*/
return true;
}
}
This is the wrong loop. When doing a forEach loop, you iterate over the value, not the index. The correct forEach loop in your case should be:
for (int arrValue :arr) {
if(arrValue != 0)
return false;
}
Here i is not the index of the array, it's the value of the array. But in the regular loop i is the index.
for (int i :arr) {
if(arr[i] != 0)
return false;
}
Use the value of the array means i in if condition
for (int i :arr) {
if(i != 0)
return false;
}
I don't think this question is very complicated to an experienced programmer, but I'm pretty new to it so I'm struggling.
I have a list of delimiters declared before a java class as such:
public static final String DELIMITERS = ",<.>/?;:'\"[{]}\\|=+-_)(*&^%$##!`~ \t\n";
I'd like to create a method that takes two parameters (a starting index and a string). The goal is to read through the string and return the next index that corresponds to a character NOT in the list of delimiters given above. If the starting index is a negative number or greater than the length of the text, the method should simply return -1. Otherwise it just returns the index of the next character NOT in the delimiter list.
This is what I have so far:
public static boolean isDelimiter(char c) {
String letter = "" + c;
if(DELIMITERS.contains(letter)){
return true;
}
else{
return false;
}
}
public static int posNextWord(int startPosition, String text) {
boolean isWord = false;
int nextWordPosition = 0;
if(startPosition < 0 || startPosition > (text.length()-1)){
return -1;
}
else{
while(isWord = false) {
for (int i = startPosition; i < text.length(); i++) {
if(!isDelimiter(text.charAt(i))){
nextWordPosition = nextWordPosition + i + startPosition;
isWord = true;
}
else{
isWord = false;
}
}
}
return nextWordPosition;
}
}
}
When I run this program with a sample text and index, however, the method just returns the number 0. Any help at all would be much appreciated. Also, the method isDelimiter is required for use in the posNextWord() method.
This entire block of code is the problem:
while(isWord = false) {
for (int i = startPosition; i < text.length(); i++) {
if(!isDelimiter(text.charAt(i))){
nextWordPosition = nextWordPosition + i + startPosition;
isWord = true;
}
else{
isWord = false;
}
}
}
return nextWordPosition;
First of all, you only need one loop to check each character of your loop. You don't need a while and a for. Second, if you want to find the first non-matching char, you can just return when you find it.
Like this:
for (int i = startPosition; i < text.length(); i++) {
if(!isDelimiter(text.charAt(i))){
return i + startPosition;
}
}
return -1;
See documented comments. Don't hesitate to ask if it is not clear :
public static boolean isDelimiter(char c) {
String letter = "" + c;
if(DELIMITERS.contains(letter)){
return true;
}
//else{ this else is not needed
System.out.println(letter +" is not a delimiter");
return false;
//}
}
public static int posNextWord(int startPosition, String text) {
//boolean isWord = false; not used
int nextWordPosition = 0;
if((startPosition < 0) || (startPosition > (text.length()-1))){
return -1;
}
//else{ this is not needed
//while(isWord = false) {
for (int i = startPosition; i < text.length(); i++) {
if(!isDelimiter(text.charAt(i))){
nextWordPosition = nextWordPosition + i + startPosition;
//isWord = true;
return nextWordPosition; //as Scary Wombat commented
}
//else{
// isWord = false;
//}
}
//isWord = false;
//}
return nextWordPosition;
// }
}
BTW: I don't think it is a good idea to return 0 as "not found" result. 0 may be returned also as valid "found" position.
Array Processing
Hi! I need to create a boolean method that processes two strings and returns if one is a subset of another. Example
if AAC is a subset of AAABBCK return true/
I currently have
for (int i = 0; i < shorterArray.length; i++) {
for (int j = 0; j < longerArray.length; j++) {
if longerArray[j] == shorterArray[i] {
count++
}
}
if (count == shorterArray.length) {
return true
) else {
return fasle;
}
}
However this doesn't take into account the repetitions
return longerString.indexOf(shorterString) > -1;
Never mind, thanks to Joachim for correcting me on the definition of subset. Now I have to provide the correct answer.
public boolean isSubset(String subset, String superset)
{
boolean[] used = new boolean[superset.length()];
iLoop:
for (int i = 0; i < subset.length(); i++) {
for (int j = 0; j < superset.length(); j++) {
if (!used[j] && subset.charAt(i) == superset.charAt(j)) {
used[j] = true;
continue iLoop;
}
}
return false;
}
return true;
}
If you can use the built in methods within the String class :
String input="your input string with substring";
CharSequence findMe="substring";
boolean retval = input.contains(findMe);
I'm trying to do this:
User inputs a number for example 2013 or 2012 and it checks if the number has any reoccurring digits like in 2012 there is 2 but I don't know where to go from here.
public static boolean hasDuplicates(String text){
for(int i = 0; i < text.length() - 1; i++){
for(int j = i + 1; j < text.length(); j ++){
if(text.charAt(i) == text.charAt(j)){
return true;
}
}
}
return false;
}
You can make it this way:
public static boolean containsDuplicate(int number) {
String strNum = number.toString();
for(int i=0; i < strNum.length() -1 ; i++){
if( containsDuplicatedValue(strNum, strNum.charAt(i) )
return true;
}
return false;
}
private static boolean containsDuplicatedValue(String str, char searchFor) {
return str.indexOf(searchFor) != str.lastIndexOf(searchFor);
}
Here is a way to check for duplicates. If the first occurrence of number has a different index than the last occurrence, then number must occur more than once.
public static boolean containsDuplicate(String str, int number) {
return str.indexOf(number) != str.lastIndexOf(number);
}