java - How to decompose my program into multiple methods? -


how decompose java program multiple methods? first stores string input, loops through extract of numbers array list. prints of these numbers along sum , product.

public class assignment1 { public static void main(string[] args){      // creates scanner storing input string     scanner numscanner = new scanner(system.in);     string input;     system.out.println("enter string of numbers compute sum , product:");     system.out.println("(enter '.' terminate program)");     input = numscanner.nextline();       // terminates program if there no input     if (input.length() == 0){                 system.out.println("invalid input: not enough characters");                 system.exit(-1);     }       // terminates program if first character '.'     if (input.charat(0) == '.'){         system.out.println("thank using numberscanner!");         system.exit(-1);     }       // defines of variables used in loops     int index = 0;     int sum = 0;     int product = 1;      integer start = null;     int end = 0;     arraylist<integer> numbers = new arraylist<>();       // loop extracts numbers string , computes sum , product             while (index < input.length()){                 if (input.charat(index) >= 'a' && input.charat(index) <= 'z' && start == null){                     index++;                 }else if (input.charat(index) >= '1' && input.charat(index) <= '9' && start == null){                     start = index;                     index++;                 }else if (character.isdigit(input.charat(index))){                     index++;                 }else{                         end = index;                         numbers.add(integer.parseint(input.substring(start,end)));                         sum += integer.parseint(input.substring(start,end));                         product *= integer.parseint(input.substring(start,end));                         index++;                         start = null;                 }                }               // last number, end greater length of string             // prints last number without using end             if (index == input.length()){             numbers.add(integer.parseint(input.substring(start)));             sum += integer.parseint(input.substring(start));             product *= integer.parseint(input.substring(start));             index++;             }           // prints numbers, sum , product beside each other         system.out.print("numbers: ");         (object : numbers) {             system.out.print(a.tostring() + " ");             }         system.out.println("sum: " + sum + " product: " + product); } 

}

i don't know how split single method multiple methods

i think having i/o in main method ok here. move out construction of numbers list. can have method getnumbers accepts string , returns list of digits in string. instance:

private static list<integer> getnumbers(string str) {     list<integer> numbers = new arraylist<>();      (int = 0; < str.length(); i++) {         char c = str.charat(i);          if (character.isdigit(c))             numbers.add(c - '0');     }      return numbers; } 

now should trivial loop on list returned getnumbers() , compute sum/product of elements:

list<integer> numbers = getnumbers(input);  system.out.println(numbers);  int sum = 0; int product = 1;  (int : numbers) {     sum += i;     product *= i; }  system.out.printf("sum: %d product: %d%n", sum, product); 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -