Comparing Strings using Split , toCharArray and Equals methods
up vote
-2
down vote
favorite
I am trying to compare 2 Strings . I used split method and then toCharArray
methods.
After all I used equals to, but at the end I get:
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException"
import java.util.Scanner;
public class LoopsWiederholung {
public static void main (String args){
System.out.print("Enter the first String : ");
Scanner scan1 = new Scanner(System.in);
String s1 = scan1.next();
s1.toUpperCase();
System.out.print("Enter the second String : ");
String s2 = scan1.next();
s2.toUpperCase();
String s3 = new String[100];
s3 = s1.split("\ ");
String s4 = new String[100];
s4 = s2.split("\ ");
for (int i = 0 ; i< 100 ; i++){
if( s3[i].toCharArray().equals(s4[i].toCharArray())){
System.out.print(s3[i]);
}
}
}
}
java arrays exception indexoutofboundsexception
|
show 1 more comment
up vote
-2
down vote
favorite
I am trying to compare 2 Strings . I used split method and then toCharArray
methods.
After all I used equals to, but at the end I get:
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException"
import java.util.Scanner;
public class LoopsWiederholung {
public static void main (String args){
System.out.print("Enter the first String : ");
Scanner scan1 = new Scanner(System.in);
String s1 = scan1.next();
s1.toUpperCase();
System.out.print("Enter the second String : ");
String s2 = scan1.next();
s2.toUpperCase();
String s3 = new String[100];
s3 = s1.split("\ ");
String s4 = new String[100];
s4 = s2.split("\ ");
for (int i = 0 ; i< 100 ; i++){
if( s3[i].toCharArray().equals(s4[i].toCharArray())){
System.out.print(s3[i]);
}
}
}
}
java arrays exception indexoutofboundsexception
Any special reason you're comparing your strings withs3[i].toCharArray().equals(s4[i].toCharArray())
? You can just compare them directly withs3[i].equals(s4[i])
.
– Kevin Anderson
Nov 15 at 0:33
@KevinAnderson Homework I guess
– Scary Wombat
Nov 15 at 0:33
It may be helpful to instead compare if the two strings are equal using string1.equals(string2)
– AbsoluteSpace
Nov 15 at 0:39
1
Arrays don't consider themselves equal to other arrays with the same content.
– khelwood
Nov 15 at 0:43
I thought @Omar would be a ghost
– Scary Wombat
Nov 15 at 2:50
|
show 1 more comment
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I am trying to compare 2 Strings . I used split method and then toCharArray
methods.
After all I used equals to, but at the end I get:
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException"
import java.util.Scanner;
public class LoopsWiederholung {
public static void main (String args){
System.out.print("Enter the first String : ");
Scanner scan1 = new Scanner(System.in);
String s1 = scan1.next();
s1.toUpperCase();
System.out.print("Enter the second String : ");
String s2 = scan1.next();
s2.toUpperCase();
String s3 = new String[100];
s3 = s1.split("\ ");
String s4 = new String[100];
s4 = s2.split("\ ");
for (int i = 0 ; i< 100 ; i++){
if( s3[i].toCharArray().equals(s4[i].toCharArray())){
System.out.print(s3[i]);
}
}
}
}
java arrays exception indexoutofboundsexception
I am trying to compare 2 Strings . I used split method and then toCharArray
methods.
After all I used equals to, but at the end I get:
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException"
import java.util.Scanner;
public class LoopsWiederholung {
public static void main (String args){
System.out.print("Enter the first String : ");
Scanner scan1 = new Scanner(System.in);
String s1 = scan1.next();
s1.toUpperCase();
System.out.print("Enter the second String : ");
String s2 = scan1.next();
s2.toUpperCase();
String s3 = new String[100];
s3 = s1.split("\ ");
String s4 = new String[100];
s4 = s2.split("\ ");
for (int i = 0 ; i< 100 ; i++){
if( s3[i].toCharArray().equals(s4[i].toCharArray())){
System.out.print(s3[i]);
}
}
}
}
java arrays exception indexoutofboundsexception
java arrays exception indexoutofboundsexception
edited Nov 15 at 2:30
Book Of Zeus
45.5k10158161
45.5k10158161
asked Nov 15 at 0:14
Omar Faig
1
1
Any special reason you're comparing your strings withs3[i].toCharArray().equals(s4[i].toCharArray())
? You can just compare them directly withs3[i].equals(s4[i])
.
– Kevin Anderson
Nov 15 at 0:33
@KevinAnderson Homework I guess
– Scary Wombat
Nov 15 at 0:33
It may be helpful to instead compare if the two strings are equal using string1.equals(string2)
– AbsoluteSpace
Nov 15 at 0:39
1
Arrays don't consider themselves equal to other arrays with the same content.
– khelwood
Nov 15 at 0:43
I thought @Omar would be a ghost
– Scary Wombat
Nov 15 at 2:50
|
show 1 more comment
Any special reason you're comparing your strings withs3[i].toCharArray().equals(s4[i].toCharArray())
? You can just compare them directly withs3[i].equals(s4[i])
.
– Kevin Anderson
Nov 15 at 0:33
@KevinAnderson Homework I guess
– Scary Wombat
Nov 15 at 0:33
It may be helpful to instead compare if the two strings are equal using string1.equals(string2)
– AbsoluteSpace
Nov 15 at 0:39
1
Arrays don't consider themselves equal to other arrays with the same content.
– khelwood
Nov 15 at 0:43
I thought @Omar would be a ghost
– Scary Wombat
Nov 15 at 2:50
Any special reason you're comparing your strings with
s3[i].toCharArray().equals(s4[i].toCharArray())
? You can just compare them directly with s3[i].equals(s4[i])
.– Kevin Anderson
Nov 15 at 0:33
Any special reason you're comparing your strings with
s3[i].toCharArray().equals(s4[i].toCharArray())
? You can just compare them directly with s3[i].equals(s4[i])
.– Kevin Anderson
Nov 15 at 0:33
@KevinAnderson Homework I guess
– Scary Wombat
Nov 15 at 0:33
@KevinAnderson Homework I guess
– Scary Wombat
Nov 15 at 0:33
It may be helpful to instead compare if the two strings are equal using string1.equals(string2)
– AbsoluteSpace
Nov 15 at 0:39
It may be helpful to instead compare if the two strings are equal using string1.equals(string2)
– AbsoluteSpace
Nov 15 at 0:39
1
1
Arrays don't consider themselves equal to other arrays with the same content.
– khelwood
Nov 15 at 0:43
Arrays don't consider themselves equal to other arrays with the same content.
– khelwood
Nov 15 at 0:43
I thought @Omar would be a ghost
– Scary Wombat
Nov 15 at 2:50
I thought @Omar would be a ghost
– Scary Wombat
Nov 15 at 2:50
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
0
down vote
So many mistakes, so find my code with comments
System.out.print("Enter the first String : ");
Scanner scan1 = new Scanner(System.in);
String s1 = scan1.next();
s1 = s1.toUpperCase(); // Strings are immutable
System.out.print("Enter the second String : ");
String s2 = scan1.next();
s2 = s2.toUpperCase();
// first check the lengths
if (s1.length() != s2.length()) {
System.out.println("not the same");
return;
}
String s3 = s1.split(""); // use this pattern
String s4 = s2.split("");
for (int i = 0 ; i< s3.length ; i++){
if (s3[i].equals(s4[i]))
System.out.print(s3[i]);
}
}
I am think that you either split
to get an array of Strings
, or use toCharArray()
to compare chars
What's the point of keeping thesplit
at all?
– khelwood
Nov 15 at 0:41
@khelwood No idea. Homework I guess. Of course all of this code can easily be replaced withif (s1.equals(s2)) System.out.println ("equal"));
As I mention in my answer, there is also the use of toCharArray() to compare chars if that is what the homework should do
– Scary Wombat
Nov 15 at 0:44
add a comment |
up vote
0
down vote
The for loop iterates from 0 to 99, but it assumes that there are 99 elements in that array, so if there isn't you see an ArrayIndexOutOfBoundsException.
One fix may be to change:
String s3 = s1.split("\ ");
and
String s4 = s2.split("\ ");
So that the for loop can then be changed to:
for (int i = 0 ; i< s3.length(); i++){
if(s3[i].equals(s4[i])){
System.out.print(s3[i]);
}
}
As @Scary Wombat mentioned, it is easier to compare two strings using string1.equals(string2)
instead of checking character arrays.
1
I am trying to compare 2 Strings - not sure why the OP is splitting as he does.
– Scary Wombat
Nov 15 at 0:34
I did what you have mentioned here . But it gives as output only one word . I mean I write 2 same sentences but it checks only first word and prints it
– Omar Faig
Nov 16 at 0:07
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
So many mistakes, so find my code with comments
System.out.print("Enter the first String : ");
Scanner scan1 = new Scanner(System.in);
String s1 = scan1.next();
s1 = s1.toUpperCase(); // Strings are immutable
System.out.print("Enter the second String : ");
String s2 = scan1.next();
s2 = s2.toUpperCase();
// first check the lengths
if (s1.length() != s2.length()) {
System.out.println("not the same");
return;
}
String s3 = s1.split(""); // use this pattern
String s4 = s2.split("");
for (int i = 0 ; i< s3.length ; i++){
if (s3[i].equals(s4[i]))
System.out.print(s3[i]);
}
}
I am think that you either split
to get an array of Strings
, or use toCharArray()
to compare chars
What's the point of keeping thesplit
at all?
– khelwood
Nov 15 at 0:41
@khelwood No idea. Homework I guess. Of course all of this code can easily be replaced withif (s1.equals(s2)) System.out.println ("equal"));
As I mention in my answer, there is also the use of toCharArray() to compare chars if that is what the homework should do
– Scary Wombat
Nov 15 at 0:44
add a comment |
up vote
0
down vote
So many mistakes, so find my code with comments
System.out.print("Enter the first String : ");
Scanner scan1 = new Scanner(System.in);
String s1 = scan1.next();
s1 = s1.toUpperCase(); // Strings are immutable
System.out.print("Enter the second String : ");
String s2 = scan1.next();
s2 = s2.toUpperCase();
// first check the lengths
if (s1.length() != s2.length()) {
System.out.println("not the same");
return;
}
String s3 = s1.split(""); // use this pattern
String s4 = s2.split("");
for (int i = 0 ; i< s3.length ; i++){
if (s3[i].equals(s4[i]))
System.out.print(s3[i]);
}
}
I am think that you either split
to get an array of Strings
, or use toCharArray()
to compare chars
What's the point of keeping thesplit
at all?
– khelwood
Nov 15 at 0:41
@khelwood No idea. Homework I guess. Of course all of this code can easily be replaced withif (s1.equals(s2)) System.out.println ("equal"));
As I mention in my answer, there is also the use of toCharArray() to compare chars if that is what the homework should do
– Scary Wombat
Nov 15 at 0:44
add a comment |
up vote
0
down vote
up vote
0
down vote
So many mistakes, so find my code with comments
System.out.print("Enter the first String : ");
Scanner scan1 = new Scanner(System.in);
String s1 = scan1.next();
s1 = s1.toUpperCase(); // Strings are immutable
System.out.print("Enter the second String : ");
String s2 = scan1.next();
s2 = s2.toUpperCase();
// first check the lengths
if (s1.length() != s2.length()) {
System.out.println("not the same");
return;
}
String s3 = s1.split(""); // use this pattern
String s4 = s2.split("");
for (int i = 0 ; i< s3.length ; i++){
if (s3[i].equals(s4[i]))
System.out.print(s3[i]);
}
}
I am think that you either split
to get an array of Strings
, or use toCharArray()
to compare chars
So many mistakes, so find my code with comments
System.out.print("Enter the first String : ");
Scanner scan1 = new Scanner(System.in);
String s1 = scan1.next();
s1 = s1.toUpperCase(); // Strings are immutable
System.out.print("Enter the second String : ");
String s2 = scan1.next();
s2 = s2.toUpperCase();
// first check the lengths
if (s1.length() != s2.length()) {
System.out.println("not the same");
return;
}
String s3 = s1.split(""); // use this pattern
String s4 = s2.split("");
for (int i = 0 ; i< s3.length ; i++){
if (s3[i].equals(s4[i]))
System.out.print(s3[i]);
}
}
I am think that you either split
to get an array of Strings
, or use toCharArray()
to compare chars
answered Nov 15 at 0:20
Scary Wombat
34.8k32252
34.8k32252
What's the point of keeping thesplit
at all?
– khelwood
Nov 15 at 0:41
@khelwood No idea. Homework I guess. Of course all of this code can easily be replaced withif (s1.equals(s2)) System.out.println ("equal"));
As I mention in my answer, there is also the use of toCharArray() to compare chars if that is what the homework should do
– Scary Wombat
Nov 15 at 0:44
add a comment |
What's the point of keeping thesplit
at all?
– khelwood
Nov 15 at 0:41
@khelwood No idea. Homework I guess. Of course all of this code can easily be replaced withif (s1.equals(s2)) System.out.println ("equal"));
As I mention in my answer, there is also the use of toCharArray() to compare chars if that is what the homework should do
– Scary Wombat
Nov 15 at 0:44
What's the point of keeping the
split
at all?– khelwood
Nov 15 at 0:41
What's the point of keeping the
split
at all?– khelwood
Nov 15 at 0:41
@khelwood No idea. Homework I guess. Of course all of this code can easily be replaced with
if (s1.equals(s2)) System.out.println ("equal"));
As I mention in my answer, there is also the use of toCharArray() to compare chars if that is what the homework should do– Scary Wombat
Nov 15 at 0:44
@khelwood No idea. Homework I guess. Of course all of this code can easily be replaced with
if (s1.equals(s2)) System.out.println ("equal"));
As I mention in my answer, there is also the use of toCharArray() to compare chars if that is what the homework should do– Scary Wombat
Nov 15 at 0:44
add a comment |
up vote
0
down vote
The for loop iterates from 0 to 99, but it assumes that there are 99 elements in that array, so if there isn't you see an ArrayIndexOutOfBoundsException.
One fix may be to change:
String s3 = s1.split("\ ");
and
String s4 = s2.split("\ ");
So that the for loop can then be changed to:
for (int i = 0 ; i< s3.length(); i++){
if(s3[i].equals(s4[i])){
System.out.print(s3[i]);
}
}
As @Scary Wombat mentioned, it is easier to compare two strings using string1.equals(string2)
instead of checking character arrays.
1
I am trying to compare 2 Strings - not sure why the OP is splitting as he does.
– Scary Wombat
Nov 15 at 0:34
I did what you have mentioned here . But it gives as output only one word . I mean I write 2 same sentences but it checks only first word and prints it
– Omar Faig
Nov 16 at 0:07
add a comment |
up vote
0
down vote
The for loop iterates from 0 to 99, but it assumes that there are 99 elements in that array, so if there isn't you see an ArrayIndexOutOfBoundsException.
One fix may be to change:
String s3 = s1.split("\ ");
and
String s4 = s2.split("\ ");
So that the for loop can then be changed to:
for (int i = 0 ; i< s3.length(); i++){
if(s3[i].equals(s4[i])){
System.out.print(s3[i]);
}
}
As @Scary Wombat mentioned, it is easier to compare two strings using string1.equals(string2)
instead of checking character arrays.
1
I am trying to compare 2 Strings - not sure why the OP is splitting as he does.
– Scary Wombat
Nov 15 at 0:34
I did what you have mentioned here . But it gives as output only one word . I mean I write 2 same sentences but it checks only first word and prints it
– Omar Faig
Nov 16 at 0:07
add a comment |
up vote
0
down vote
up vote
0
down vote
The for loop iterates from 0 to 99, but it assumes that there are 99 elements in that array, so if there isn't you see an ArrayIndexOutOfBoundsException.
One fix may be to change:
String s3 = s1.split("\ ");
and
String s4 = s2.split("\ ");
So that the for loop can then be changed to:
for (int i = 0 ; i< s3.length(); i++){
if(s3[i].equals(s4[i])){
System.out.print(s3[i]);
}
}
As @Scary Wombat mentioned, it is easier to compare two strings using string1.equals(string2)
instead of checking character arrays.
The for loop iterates from 0 to 99, but it assumes that there are 99 elements in that array, so if there isn't you see an ArrayIndexOutOfBoundsException.
One fix may be to change:
String s3 = s1.split("\ ");
and
String s4 = s2.split("\ ");
So that the for loop can then be changed to:
for (int i = 0 ; i< s3.length(); i++){
if(s3[i].equals(s4[i])){
System.out.print(s3[i]);
}
}
As @Scary Wombat mentioned, it is easier to compare two strings using string1.equals(string2)
instead of checking character arrays.
edited Nov 15 at 0:41
answered Nov 15 at 0:25
AbsoluteSpace
14517
14517
1
I am trying to compare 2 Strings - not sure why the OP is splitting as he does.
– Scary Wombat
Nov 15 at 0:34
I did what you have mentioned here . But it gives as output only one word . I mean I write 2 same sentences but it checks only first word and prints it
– Omar Faig
Nov 16 at 0:07
add a comment |
1
I am trying to compare 2 Strings - not sure why the OP is splitting as he does.
– Scary Wombat
Nov 15 at 0:34
I did what you have mentioned here . But it gives as output only one word . I mean I write 2 same sentences but it checks only first word and prints it
– Omar Faig
Nov 16 at 0:07
1
1
I am trying to compare 2 Strings - not sure why the OP is splitting as he does.
– Scary Wombat
Nov 15 at 0:34
I am trying to compare 2 Strings - not sure why the OP is splitting as he does.
– Scary Wombat
Nov 15 at 0:34
I did what you have mentioned here . But it gives as output only one word . I mean I write 2 same sentences but it checks only first word and prints it
– Omar Faig
Nov 16 at 0:07
I did what you have mentioned here . But it gives as output only one word . I mean I write 2 same sentences but it checks only first word and prints it
– Omar Faig
Nov 16 at 0:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310674%2fcomparing-strings-using-split-tochararray-and-equals-methods%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Any special reason you're comparing your strings with
s3[i].toCharArray().equals(s4[i].toCharArray())
? You can just compare them directly withs3[i].equals(s4[i])
.– Kevin Anderson
Nov 15 at 0:33
@KevinAnderson Homework I guess
– Scary Wombat
Nov 15 at 0:33
It may be helpful to instead compare if the two strings are equal using string1.equals(string2)
– AbsoluteSpace
Nov 15 at 0:39
1
Arrays don't consider themselves equal to other arrays with the same content.
– khelwood
Nov 15 at 0:43
I thought @Omar would be a ghost
– Scary Wombat
Nov 15 at 2:50