Advertisement
Guest User

I Messed Up The Code

a guest
Mar 19th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class RohittDataSubScribeToOurMemePage
  4. {
  5.     public static ArrayList<String> numbers = new ArrayList<String>();
  6.     public static char[] phrase;
  7.     public static char[] letters = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  8.  
  9.  
  10.     //Converts character to number String
  11.     public static String charToNum(char x)
  12.     {
  13.         for(int i = 0;i < letters.length;i++)
  14.         {
  15.             if(x == letters[i])
  16.                 return Integer.toString(i);
  17.         }
  18.  
  19.         return "error";
  20.     }
  21.  
  22.     //Adds an element to list if requirements are met
  23.     public static void addToList(String x)
  24.     {
  25.         //Makes sure there's a previous element
  26.         int previousElement = numbers.size() - 1;
  27.  
  28.         //If there's a previous element
  29.         if(previousElement >= 0)
  30.         {
  31.             //If previous element isn't a pair
  32.             if(numbers.get(previousElement).length() == 1)
  33.             {
  34.                 //If current String is a single letter
  35.                 if(x.length() == 1)
  36.                     numbers.set(previousElement,numbers.get(previousElement) + x);
  37.  
  38.                 //If current String is a pair
  39.                 else
  40.                 {
  41.                     String first = x.substring(0,1);
  42.                     String second = x.substring(1);
  43.                     numbers.set(previousElement,numbers.get(previousElement) + first);
  44.                     numbers.add(second);
  45.                 }
  46.             }
  47.  
  48.  
  49.             //If previous element is a pair
  50.             else
  51.                 numbers.add(x);
  52.         }
  53.         else
  54.         {
  55.             numbers.add(x);
  56.         }
  57.     }
  58.  
  59.     public static void main(String[] args) {
  60.         //Takes phrase and converts to char array
  61.         Scanner input = new Scanner(System.in);
  62.         System.out.print("Input your phrase: ");
  63.         String temp = input.nextLine().toLowerCase();
  64.         phrase = new char[temp.length()];
  65.         for(int x = 0;x < temp.length();x++)
  66.         {
  67.             phrase[x] = temp.charAt(x);
  68.         }  
  69.  
  70.  
  71.  
  72.         //Converts character to number array
  73.         for(char element : phrase)
  74.         {
  75.             addToList(charToNum(element));
  76.         }
  77.  
  78.         //Prints out the number pairs
  79.         for(String element : numbers)
  80.         {
  81.             if(element.length() == 2)
  82.                 System.out.println(element);
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement