Advertisement
Guest User

FromLeftToTheRight

a guest
Aug 22nd, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1. package dataTypesAndVariables;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class sumFromLefToRight {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int counter = Integer.parseInt(scanner.nextLine());
  9.  
  10.         while(counter > 0){
  11.             String input = scanner.nextLine();
  12.             String numberOne = "";
  13.             String numberTwo = "";
  14.  
  15.             boolean one = true;
  16.  
  17.  
  18.             for (int i = 0; i < input.length() ; i++) {
  19.                 if(input.charAt(i) == ' '){
  20.                     one = false;
  21.                 }
  22.  
  23.                 if(!one){
  24.                     if(input.charAt(i) != ' ') {
  25.                         numberTwo += input.charAt(i);
  26.                     }
  27.                 }else{
  28.                     numberOne += input.charAt(i);
  29.                 }
  30.             }
  31.  
  32.             int sumOne = 0;
  33.             int sumTwo = 0;
  34.             boolean negativeOne = false;
  35.             boolean negativeTwo = false;
  36.  
  37.             if(numberOne.charAt(0) == '-') {
  38.                 negativeOne = true;
  39.                 for (int f = 1; f < numberOne.length(); f++) {
  40.                     sumOne += numberOne.charAt(f) - '0';
  41.                 }
  42.             }else{
  43.                 for (int f = 0; f < numberOne.length(); f++) {
  44.                     sumOne += numberOne.charAt(f) - '0';
  45.                 }
  46.             }
  47.  
  48.             if(numberTwo.charAt(0) == '-') {
  49.                 negativeTwo = true;
  50.                 for (int s = 1;  s < numberTwo.length(); s++) {
  51.                     sumTwo += numberTwo.charAt(s) - '0';
  52.                 }
  53.             }else{
  54.                 for (int s = 0; s < numberTwo.length(); s++) {
  55.                     sumTwo += numberTwo.charAt(s) - '0';
  56.                 }
  57.             }
  58.  
  59.             if(negativeOne){
  60.                 sumOne *= -1;
  61.             }
  62.  
  63.             if(negativeTwo){
  64.                 sumTwo *= -1;
  65.             }
  66.  
  67.             if(sumOne > sumTwo){
  68.                 System.out.println(sumOne);
  69.             }else{
  70.                 System.out.println(sumTwo);
  71.             }
  72.  
  73.             counter--;
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement