6
\$\begingroup\$

Input

The input for this problem is a String of sorted numbers and a special number. The syntax of the input would be 1,2,3,4;5

Output

The program will output all of the possible sums that equate to the special number. The output for the example would be 1,4;2,3

I solved the problem by using some simple subtraction. The program will begin at the greatest value and work its way to the lowest value. For each value the program will subtract it from the pre-set value and search the array for that value. I want to know what I could do better for this.

import java.util.*;
public class NumPairs {
 static String line = "1,2,3,4;5";
 public static void main(String[] args) {
 String fina = "";
 if(!line.equals("")){
 String a = line.substring(line.indexOf(";")+1,line.length());
 String[] valu = line.substring(0,line.indexOf(";")).split(",");
 for(int h = valu.length-1; h >0; h--){
 int need = Integer.parseInt(a) - Integer.parseInt(valu[h]);;
 if(Arrays.asList(valu).indexOf(need + "") != -1 && fina.indexOf(need +"") == -1 && need != Integer.parseInt(valu[h])){
 if(need > Integer.parseInt(valu[h]))
 fina += valu[h] +","+need +";";
 else
 fina += need +","+ valu[h] + ";";
 }
 }
 }
 if(fina.equals(""))
 System.out.println("NULL");
 else
 System.out.println(fina.substring(0,fina.length()-1));
 } 
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Feb 26, 2016 at 19:38
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to Code Review! I hope you get some great answers. \$\endgroup\$ Commented Feb 26, 2016 at 20:41
  • \$\begingroup\$ Related: codereview.stackexchange.com/questions/121041/… \$\endgroup\$ Commented Feb 27, 2016 at 2:35

1 Answer 1

3
\$\begingroup\$

Lack of separation of concerns

You have a single main function that:

  1. Reads a String and parses it to an internal format
  2. Applies logic to it (finds numbers that sum to the target)
  3. Pretty prints the result

You should have at least 3 more functions:

public class NumPairs {
 // Pseudocode, to return two objects at once read 
 // http://stackoverflow.com/questions/457629/how-to-return-multiple-objects-from-a-java-method 
 public static (int[], int) parseInput(String input) {
 }
 public static int[][] sumsTo(int[] list, int result) {
 }
 public static String prettify(int[][] result) {
 }
 public static void main(String[] args) {
 // Simply use the other functions
 }
}

You may still need to subdivide sumsTo surther.

answered Feb 26, 2016 at 23:34
\$\endgroup\$
1
  • \$\begingroup\$ Surely you mean "prettify" \$\endgroup\$ Commented Mar 1, 2016 at 22:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.