codesnippets [Nebula Cyber Wiki]

Site Tools


codesnippets

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

codesnippets [2019/12/19 23:43]
54.240.196.168 created
codesnippets [2019/12/20 02:32] (current)
54.240.196.184
Line 7: Line 7:
 { {
   
-    public static int[] reader(String input)throws IOException +    public static int[] reader(String input)throws IOException{ 
-    {+
     int rangeArray[] = new int[2];     int rangeArray[] = new int[2];
-    try{ +    BufferedReader reader = new BufferedReader(new FileReader(input)); 
-     BufferedReader reader = new BufferedReader(new FileReader(input)); +    while (true) { 
-     while (true) { + String line = reader.readLine(); 
- String line = reader.readLine(); + if (line == null){ 
- if (line == null){ + break; 
- break; +
- + String[] parts = line.split("-"); 
- String[] parts = line.split("-"); + int lowRange = Integer.parseInt(parts[0]); 
- int lowRange = Integer.parseInt(parts[0]); + int highRange = Integer.parseInt(parts[1]); 
- int highRange = Integer.parseInt(parts[1]); + rangeArray[0] = lowRange; 
- rangeArray[0] = lowRange; + rangeArray[1] = highRange;
- rangeArray[1] = highRange+
-     } +
-     reader.close(); +
-    } +
-    catch(IOException e){ +
- e.printStackTrace();+
     }     }
-    return rangeArray;+   return rangeArray;
     }     }
  
-    public static int[] checkAdjacent() +    public static int checkAdjacent()throws IOException{
-    { +
-      +
- try{+
  int[] rangeArray = reader("input.txt");  int[] rangeArray = reader("input.txt");
  int[] duplicateArray = new int[rangeArray[1] - rangeArray[0]];  int[] duplicateArray = new int[rangeArray[1] - rangeArray[0]];
  int counter = 0;  int counter = 0;
  for(int i=rangeArray[0]; i < rangeArray[1]; i++){  for(int i=rangeArray[0]; i < rangeArray[1]; i++){
- //System.out.println("Current Element is: " + i)+ int num = i; 
- String[] str1 Integer.toString(i).split("")+ boolean flag false
- //System.out.println(str1[1])+ int duplicateCounter = 0
- for(int 0j<str1.length;j++){ + int currentDigit num % 10; 
- if( j != 5 && (Integer.parseInt(str1[j]) == Integer.parseInt(str1[j+1]))){ + num /= 10; 
- //System.out.println(j); + while(num > 0){ 
- System.out.println(i); + if(currentDigit < num %10){ 
- duplicateArray[counter] i+ flag true
- counter++;+ break;
  }  }
 + if(currentDigit == num %10){
 + duplicateCounter++;
 + }
 + currentDigit = num % 10;
 + num = num/10;
 + }
 + if(flag == false && duplicateCounter >= 1){
 + counter++;
  }  }
  }  }
- return duplicateArray; + return counter;
  }  }
- catch(IOException e){+    public static void main(String args[]){ 
 +     try{ 
 + System.out.println(checkAdjacent()); 
 +     } 
 +     catch(IOException e){
  e.printStackTrace();  e.printStackTrace();
- } +     }
- int[] placeholder = new int[1]; +
- return placeholder; +
-    } +
-    public static void main(String args[]){ +
- checkAdjacent();+
     }     }
 } }
 +
 </code> </code>
codesnippets.txt · Last modified: 2019/12/20 02:32 by 54.240.196.184