Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
public static void main(String[] args) {
String a = "Hi I AM ";
String a1 = "Hi I AM";
if (a1.equalsIgnoreCase(a)) {
System.out.println("equal");
} else {
System.out.println("not equal");
}
}
Why above code is displaying not equal in console?
The first String has an additional space at the end, so it's not equal to the second String.
it will display "not equal" because you have added an extra space in a(which is not equal to a1).
String a="Hi I AM "; // has extra space in the end(8 characters)
String a1="Hi I AM"; //this string has 7 characters
Edit:
it actually works fine after your edit to the question.
Better to use trim() method for removing the unwanted white spaces in leading and tailing positions of the string.
public static void main(String[] args) {
String a="Hi I AM";
String a1="Hi I AM";
if(a1.equalsIgnoreCase(a.trim()))
{
System.out.println("equal");
}
else
{
System.out.println("not equal");
}
See the result:
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
public class Main {
public static void main(String[] args) {
String satz = "Heute ist wunderbares Wetter, fahren wir doch zum Strand";
System.out.println(satz);
System.out.print(satz.substring(22));
}
}
I tried different things, but i have always errors. i am a beginner with Java, and i try to understand strings. I tried to understand them in ANSI C, but i have no clue :D
String is an Object which has build in methods such as the one you're using .substring(). Another which might be handy for you in this case is charAt() in combination with a loop.
You can read more about which methods String object has at https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
//Print All Charchters
for (int i=0; i<satz.length(); i++)
{
System.out.println(satz.charAt(i));
}
//Get char In a specific Location (Index)
System.out.println(satz.charAt(22));
If you want to print out the 23rd letter you can use substring or charAt
String satz = "Heute ist wunderbares Wetter, fahren wir doch zum Strand";
System.out.println(satz);
System.out.println(satz.charAt(22));
System.out.println(satz.substring(22,23))
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to iterate through each character in a string that is inputted and check if any letter is a.
Here is my Java code:
import java.util.Scanner;
public class Input
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String input;
input = in.nextLine();
for (int i=0; i<input.length(); i++)
{
char chararcter = input.charAt(i);
String s = Character.toString(character); //the error is here
if (s.equals("a"))
{
System.out.println("You typed an A.");
}
}
}
}
For reference, here is a Python analog.
input=raw_input()
for i in range (0,len(input)):
if input[i] == "a":
print "You typed an A."
I apologize for the simplistic nature of this question; I'm very new to Java. Thank you for helping.
You've change the spelling in your declaration.
char character = input.charAt(i); // <-- not chararcter (extra rc).
You don't have to convert character to String. just simply do character comparison.
if (chararcter == 'a')
{
System.out.println("You typed an A.");
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Looking this code
String input="I use this method";
String word=input.replaceAll(" ","/");
char buf[]=word.toCharArray();
but i want to another method to doing this?
This is the easiest way I have found to convert a string including white spaces to a char array in java.
String input = "I use this method";
char[] buf = input.toCharArray();
It looks like you are doing it right, but taking out all of the white space with the replaceAll(" ", "/")
From your question I'm assuming you want to save the string without truncating the whitespace in a char array.
If your remove the line " String word=input.replaceAll(" ","/"); " from your code.Then your code will work perfectly fine.PFB a sample code which might help you to understand this better
public static void main(String[] args)
{
String input="I use this method";
System.out.println(input.length()); //length of string before converting to char array
//String word=input.replaceAll(" ","/");
char buf[]=input.toCharArray();
System.out.println(buf.length);//length of string after converting to char array
for(int i=0;i<buf.length;i++){
System.out.print(buf[i]); /// Print the values in char array
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
{
public static int WordCount (String cString)
{
String currentWord;
int index;
int spacePos;
int validWordCount=0;
boolean validWord;
char upClowC;
cString=cString.trim()+" ";
spacePos=cString.indexOf(" ");
validWord=true;
for(index=0;index<cString.length();index++)
{
currentWord=cString.substring(0,spacePos);
upClowC=Character.toUpperCase(currentWord.charAt(index));
if(upClowC<'A'||upClowC>'Z')
{
validWord=false;
}
}
if(validWord==true)
{
validWordCount++;
}
return validWordCount;
}
public static void main(String[] args)
{
String sentence;
System.out.println("enter a sentence:");
sentence=EasyIn.getString();
WordCount(sentence);
}
}
I'm trying to create a method which takes a sentence and picks out the valid words (i.e. no numbers or symbols), but I keep getting an out of bounds error.
I can't use an array.
Your problem is here:
currentWord = cString.substring(0, spacePos);
upClowC = Character.toUpperCase(currentWord.charAt(index));
currentWord gets shorter, but index is still running from 0 to the length of the string.
General notes:
Follow Java naming conventions and change the name of your method to begin with small letter
if(validWord) is enough when you want to compare something to true, otherwise it's like asking "is it true that the value is true" instead of simply "is the value true"
Next time post your stack trace to get better and sooner help
In your code, you are doing
spacePos = cString.indexOf(" ");
And then inside the loop:
currentWord = cString.substring(0,spacePos);
upClowC = Character.toUpperCase(currentWord.charAt(index));
Now, because of the loop, the index will take values from 0 to your string length minus 1. If your substring (currentWord) is smaller than your string - which probably is -, then currentWord.charAt(index) will try to index out of the bounds of the substring, which is why you get the error.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
It is asked in an interview to write the code in Java to display the string which doesn't have consecutive repeated characters.
E.g.: Google, Apple, Amazon;
It should display "Amazon"
I wrote code to find continues repeating char. Is there any algorithm or efficient way to find it?
class replace
{
public static void main(String args[])
{
String arr[]=new String[3];
arr[0]="Google";
arr[1]="Apple";
arr[2]="Amazon";
for(int i=0;i<arr.length;i++)
{
int j;
for(j=1;j<arr[i].length();j++)
{
if(arr[i].charAt(j) == arr[i].charAt(j-1))
{
break;
}
}
if(j==arr[i].length())
System.out.println(arr[i]);
}
}
}
Logic : Match the characters in a String with the previous character.
If you find string[i]==string[i-1]. Break the loop. Choose the next string.
If you have reached till the end of the string with no match having continuous repeated character, then print the string.