Skip to content

Instantly share code, notes, and snippets.

@Viacheslav77
Last active March 3, 2016 18:17
Show Gist options
  • Select an option

  • Save Viacheslav77/acdd9577f1770aaa9915 to your computer and use it in GitHub Desktop.

Select an option

Save Viacheslav77/acdd9577f1770aaa9915 to your computer and use it in GitHub Desktop.
Пользователь вводит набор чисел в виде одной строки (“1, 2, 3, 4, 4, 5”). Избавиться от повторяющихся элементов в строке и вывести результат на экран.
package DuplicateNumber2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Scanner;
import MyExceptions.Myclass.NumeralExceptions;
public class getNamber {
public static class NumeralExceptions extends Exception {
public NumeralExceptions (String message){
super(message);
}
@Override
public String getMessage(){
return "NumeralExceptions" + super.getMessage();
}
}
public static class InpNum {
static Scanner bf = new Scanner(System.in);
private static String [] tmpText;
public String[] getText (){
return tmpText;
}
public String[] getNum (){
tmpText =null;
while (tmpText == null){
try {
setNumber();
} catch (NumeralExceptions e) {
System.out.println (e.getMessage());
tmpText = null;
}
}
return tmpText;
}
public static void setNumber() throws NumeralExceptions {
String text = bf.next();
if(text.equals("n"))
text = "0000";
tmpText = text.split(" ");
for(String s:tmpText)
for(int i = 0; i < s.length(); i++)
if (s.charAt(i)>=97&&s.charAt(i)<=122)
throw new NumeralExceptions ("-------------------------\nEnter the numbers in the correct format :)\n------------------------------------------");
}
}
}
----------------------------------------------------------------------------------------------------
Enter the numbers separated by spaces or 'Enter' (Enter the letter 'n' to finish entering numbers):
----------------------------------------------------------------------------------------------------
123
Added. Total ..1
1234 343 0 9 7 5 3 5 78 4 3 68 8 0 9 1 2 53
Added. Total ..2
Added. Total ..3
Added. Total ..4
Added. Total ..5
Added. Total ..6
Added. Total ..7
Added. Total ..8
Added. Total ..8
Added. Total ..9
Added. Total ..10
Added. Total ..10
Added. Total ..11
Added. Total ..12
Added. Total ..12
Added. Total ..12
Added. Total ..13
Added. Total ..14
Added. Total ..15
34
Added. Total ..16
fdwef
NumeralExceptions-------------------------
Enter the numbers in the correct format :)
------------------------------------------
0 0 1 1 2 2 3 3 4 4 5 6
Added. Total ..16
Added. Total ..16
Added. Total ..16
Added. Total ..16
Added. Total ..16
Added. Total ..16
Added. Total ..16
Added. Total ..16
Added. Total ..16
Added. Total ..16
Added. Total ..16
Added. Total ..17
n
----------------------------------------------------------------------------------------
Print the sorted .... 0 1 2 3 4 5 6 7 8 9 34 53 68 78 123 343 1234
----------------------------------------------------------------------------------------
package DuplicateNumber2;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import DuplicateNumber2.getNamber.InpNum;
import DuplicateNumber2.getNamber.NumeralExceptions;
public class MyClass {
/* Пользователь вводит набор чисел в виде одной строки (“1, 2, 3, 4, 4, 5”).
Избавиться от повторяющихся элементов в строке и вывести результат на экран.*/
public static void main (String[] args) {
Set <Integer> mapSet = new TreeSet <Integer>();
InpNum inpNum = new InpNum();
System.out.println("----------------------------------------------------------------------------------------------------");
System.out.println("Enter the numbers separated by spaces or 'Enter' (Enter the letter 'n' to finish entering numbers): ");
System.out.println("----------------------------------------------------------------------------------------------------");
int end = 0;
while (end==0){
String[] tmp2 = inpNum.getNum();
for(String s: tmp2 ){
if(s.equals("0000")){
end=1;
break;
}
mapSet.add(Integer.parseInt(s));
System.out.println("Added. Total .." + mapSet.size());
}
}
System.out.println("----------------------------------------------------------------------------------------");
System.out.print("Print the sorted .... ");
for(Integer in: mapSet)
System.out.print(in.toString() + " ");
System.out.println("\n----------------------------------------------------------------------------------------");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment