• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Comparing numbers stored in string arrays

 
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to compare two String arrays with integer inputs separated by "#".
Eg:
Array 1: 1#4#5#15#2#17  for elements 1,4,5,15,2,17
Array 2: 5#4#1#17#2#15  for elements 5,4,1,17,2,15
and print "yes" if the two arrays have same integer values irrespective of their position in the array.

Eg:  For the above arrays, output is  "yes".


I am confused from where to start.  Is `compareTo()` and charAt() useful here?
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean you are having a string array where each element of an array is much alike the one you had marked Array1 and Array2 in your post(As Array1 and Array2 doen't seems array actually).

Kind Regards,
Praveen.
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Array 1: 1#4#5#15#2#17  for elements 1,4,5,15,2,17


If this means you have a String that you need to turn into an array, look into String#split().
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am confused from where to start


Start at the beginning.  Don't begin with trying to figure out if you can use <something>. begin with figuring out in English (or whatever) what you need to do.  write out the series of steps of what you need to do:

1)  get the two strings
2) break the strings apart into individual integers
3) compare the two sets of integers
4) print out if they are equal or not

Now that you have this, break each down into smaller chunks

1a) prompt the user for first input
1b) get the first string
1c) prompt the user for second input
1d) get the second string

Alternately, you may do it this way:

1a) open the file
1b) read first line of file and save as input1
1c) read second line of file and save as input2
1d) close file

but regardless of how you want to do it, you need to break it down into smaller, simpler steps. Eventually, you get to the point where you have a good idea how to code it..

then you start coding it 2-3 lines at a time, recompiling and testing about 10 time more often than you think you should.


 
Marshal
Posts: 80665
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this an assignment you have been given? I suggest you can find ways of dividing that String if you look through its methods. I shall let you work out the remainder of the process for yourself.
 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This problem is a mini-part of a bigger assignment. The actual problem is an array of strings. Eg: 1#2#3, 4#5#7, 6#5#9 and there are two such arrays which later needs to be checked for transformation using legal sequence of row and column moves. I wasn't sure how to extract the integer from the string array in the above format. That's the reason I put it up this way. `String#split()`works well here.
But there are other issues I am facing. I am using `Integer.parseInt()` to convert the String array into integer array using a loop and then appending it to a StringBuilder object. I then  use `Arrays.sort` to sort this StringBuilder object  and then compare it with another StringBuilder object formed from another String array of the same format. I use Arrays.equals() for this comparison. I am getting errors in usage of`Arrays.sort()` and Arrays.equals() for StringBuilder objects.
I really don't know how to sort and compare two StringBuilder objects or an alternative approach.

I am sorry. My code is in a real bad shape for now.

 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I removed Integer.ParseInt() and the double if statements (Lines 49 and 54) but the same errors exist i.e. the usage of Arrays.sort() and Arrays.equals() for the two StringBuilder objects which store the strings of integers.

 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A StringBuilder is not an array, so Arrays.sort isn't going to work.

In the future, please give us the line number and the full error message.
 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. I will keep that in mind.
I think I need a lot of practice on Java Strings.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One problem you are having is this statement inside the for loops:

Here you parse the integer from the string, but you immediately throw the result away without storing it anywhere
 
Saloon Keeper
Posts: 11067
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Khusbu Sinha wrote:I have to compare two String arrays with integer inputs separated by "#".
Eg:
Array 1: 1#4#5#15#2#17  for elements 1,4,5,15,2,17
Array 2: 5#4#1#17#2#15  for elements 5,4,1,17,2,15
and print "yes" if the two arrays have same integer values irrespective of their position in the array.

Eg:  For the above arrays, output is  "yes".


I am confused from where to start.  Is `compareTo()` and charAt() useful here?


I am confused. It appears that you start with a String "1#4#5#15#2#17", then you want to convert that to an array of int's, then get another similar String and convert that into an array of int's, and finally sort and compare the two int arrays. I don't see anywhere in this that calls for an array of Strings, which is what you have all over your code.

Edit: On second thought you'd temporarily need an array of Strings to hold the result of split() before calling parseInt(), but you certainly wouldn't be needing to pass this temporary array around. It might be useful to have a helper method:

Also, the name of your method, "Mathematician" does not reflect clearly the intent and purpose of the method. Method names should always begin with a lower case letter.
 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The actual problem is this. There are two string arrays input by the user  as follows:
Input 1: Size of the array = 3
Input 2: (Array 1)  
              1#4#5
              3#6#7
              5#8#9
Input 2: (Array 2)  
              1#6#5
              4#7#8
              3#5#9
They contain the same no. of integer strings per line as specified by the user input array size. For eg: the 1st line of Array 1 = 1#4#5 = 3 integer strings.
In case, the array inputs are blank in any line, the output should be "invalid".

I have modified my code but it passes very few test cases and mostly gives the correct output only when the two arrays are transpose of each other (when seen from the input format). It does not give the correct output when all the integer strings in both the arrays are same irrespective of their positions in the array.

Test case 1 (passed):
Enter the array size:
3
Enter the 1st array elements:
1#4#5
3#6#7
5#8#8
Enter the 2nd array elements:
1#6#5
4#7#8
3#5#8

Output:
yes

Test case 2(failed):

Enter the array size:
2
Enter the 1st array elements:
1#6
3#4
Enter the 2nd array elements:
6#3
4#1

Output:
no

Test case 3 (passed)
Enter the array size:
2
Enter the 1st array elements:
1#1
2#2
Enter the 2nd array elements:
1#1

Output:
invalid


Below is my code:

 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A bit of advise:refactor your methods(in all the methods even the main,you are doing plenty of things in there).in the equivalent method,look at where you did the check on parameter.i mean you have created a plenty of objects before validating the parameter which is totally redundant(in the case if user inputs the invalid argument).and the other thing is you are catching ArrayIndexOutOfBoundsException and simply printing a invalid message.by such an activity you are actually making it hard for you or a caller to find the cause of the problem(line number where the exception occurs).
you can consider these points:
→the first thing should be when user inputs the string you should immediately check if it's valid.for yes you can continue else throw him the error message(or better as IllegalArgumentException with a invalid argument message so that he can see the stack trace for the cause).for this part you can write a method isValid or something else which will return a boolean.it will help you to avoid redundant work.
→second thing is.now you have 2 string arrays and you are required to convert the string array to a int array.write a method "int[] parseIntArray(String[] arr) {...}" to convert a string array to a int array.first make use of split() and then you can make use of parseInt during iterating inside string array(the array you have got after applying split) to convert it to int[].alternatively,i have not a good knowledge of java8 but streams concept can also be used for attaining this part.somebody please do a favor for this point.at this point you are sure that user has input a valid thing so you should not be worried for something like what if the parseInt throws an exception.
→next part is we have now 2 int arrays.you are required to check if they contains the same element.do it by yourself.

these things should not be taken as exhaustive.you can do a further refactoring where ever you feel suitable.

Praveen
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can also refactor the method in my second point as in there splitting and conversion to int are two different task.

Praveen
 
Sheriff
Posts: 9012
655
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Khusbu Sinha wrote:System.out.println(equivalent (size, s1, s2));

Why do you pass in 'size' while s1 and s2 sizes are same (s1 and s2 creation relies on size value)?

Khusbu Sinha wrote:

Please explain what do you want to achieve here. Look at your comment - looks like you had a 'start of commend in your head...' cut... 'then added some other bit'. Comments most likely going to be read by others, so, such comment is almost useless.

In general your code is very hard to follow. Lots of operations without clear intent. Try to use more methods wrapping different operations in it and giving a clear name.

Lines 49-58. Isn't same routine repeated twice?



I'd consider throwing this code away - for your good. Writing down clear instructions and rewriting the code, but this time by small piece with lots of methods.

 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Khusbu,
Reason for my advise: flow is the very important thing when you are coding with the object oriented style.a very good example originally given by @junilu is → here.flow helps to read the program just like we read a english sentence(written with valid semantics).for your code what we need is:
→ object who will compare the 2 strings.
→ the 2 strings.
→ a check so that the 2 strings are valid for doing a comparison.program should stop here if the validity does not hold.
→ splitting the string in a string arrays(input) to the arrays which contains only the int as a string.
→ converting the string to the int array.
→ comparison of the int arrays.
→ printing the result.
→ end.

if you would see in your code where you use the for loops-they are identical in pairs.what do you think if you put a code for the identical pairs in a method and simply call that code during needed,how much lines will be saved(and how much does the readability will be improved).
 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Praveen. I admit my code was highly annoying and confusing. It needs a lot of refactoring.
Junilu Lacar has advised me for 'Clean Code' by Robert C. Martin.
I am looking forward to it.
 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, the link posted by you was quite useful.
 
Carey Brown
Saloon Keeper
Posts: 11067
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Khusbu Sinha wrote:I have to compare two String arrays with integer inputs separated by "#".
Eg:
Array 1: 1#4#5#15#2#17  for elements 1,4,5,15,2,17
Array 2: 5#4#1#17#2#15  for elements 5,4,1,17,2,15
and print "yes" if the two arrays have same integer values irrespective of their position in the array.

Eg:  For the above arrays, output is  "yes".


You are showing an example here where "17" is an integer. However your latest code parses individual digits, not integers, and appends them into a long string of digits.

 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah..Yes. That's the error.
 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The whole idea of appending to a StringBuilder object and then copying it in a char array is a mess. I am using ArrayList now.
 
Carey Brown
Saloon Keeper
Posts: 11067
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As others have mentioned, it's a good idea to identify redundant code and refactor that into its own method. For an example, something like this:
 
Saloon Keeper
Posts: 5617
214
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given what OP writes:

Khusbu Sinha wrote:(...)
This problem is a mini-part of a bigger assignment. The actual problem is an array of strings. Eg: 1#2#3, 4#5#7, 6#5#9 and there are two such arrays which later needs to be checked for transformation using legal sequence of row and column moves.(...)


it might be wiser to return a List<List<Integer>>, or even simpler, an int[][] or String[][].

@OP
has this assignment anything to do with solving a series of linear equations?
 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Carey
Thanks for this method. It makes the conversion from string array to integer array much simpler.
@ Piet
You are right that returning an int[][] would be a wiser choice in this case, though I am not sure how to implement it correctly here. I tried for it and my code returns wrong values of the integer array returned. Please guide me in its correct implementation. (By the way, this integer array is to be used further for legal sequence of row and column moves to check whether it exactly matches the second integer array after the moves).

 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry for changing the variable names in the method. I am updating them.
 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you need to decide whether you want the String array to be parsed into a List or an int[][].  From what I understand, you want two Lists that you can sort and compare.  Why copy the List into an int[][]?
 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's because I have to use this int[][] later in the program for some  calculations. That's why I decided to return an int[][] from this method to perform both the comparison as well as the calculation.

My code for copying the list into int[][] is wrong.
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Khusbu Sinha wrote:It's because I have to use this int[][] later in the program for some  calculations. That's why I decided to return an int[][] from this method to perform both the comparison as well as the calculation.


Then I don't see any reason to parse the String array into a List first.  Parse it directly into an int[][].

My code for copying the list into int[][] is wrong.


I noticed that!  But I wasn't going to say anything if you didn't need it.
 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:  Parse it directly into an int[][].





I know I am doing something horribly wrong here. Please tell me what it is.
 
Carey Brown
Saloon Keeper
Posts: 11067
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have too many nested loops. You only need 2.

 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You have too many nested loops. You only need 2.


This is unnecessarily constrained to handling symmetrical arrays. It doesn't have to be.

You can do this instead:


 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding the following at Line 10 does not help.


Where do I go wrong here?
 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was referring to Carey Brown's post.
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Khusbu Sinha wrote:Adding the following at Line 10 does not help.


Where do I go wrong here?



Saying "it does not help" or ItDoesntWorkIsUseless -- please TellTheDetails (← click on those links)
 
Carey Brown
Saloon Keeper
Posts: 11067
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Khusbu Sinha wrote:Adding the following at Line 10 does not help.


Where do I go wrong here?




When you say "does not help" you're not giving us enough info to go on.
 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean I get the following output when I try to print out this int[][] :

[[I@7cc8e0 [[I@7cc8e0
[[I@7cc8e0 [[I@7cc8e0

 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 1 was outside the code.
 
Carey Brown
Saloon Keeper
Posts: 11067
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for bothering you all. I corrected line 11.



and the program runs correctly now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic