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


}
}









share|improve this question
























  • 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










  • 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















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


}
}









share|improve this question
























  • 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










  • 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













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


}
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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










  • 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










  • @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












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






share|improve this answer





















  • 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


















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.






share|improve this answer



















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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






share|improve this answer





















  • 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















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






share|improve this answer





















  • 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













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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 at 0:20









Scary Wombat

34.8k32252




34.8k32252












  • 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


















  • 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
















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












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.






share|improve this answer



















  • 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















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.






share|improve this answer



















  • 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













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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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














  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?