-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyInput.java
201 lines (176 loc) · 5.05 KB
/
myInput.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//package my;
import java.util.ArrayList;
import java.io.IOException;
public class myInput {
//Veriable Diclare
public static int totalLength=0;
//main method
public static void main(String[] args) {
test();
System.exit(0);
}
private static void test(){
/* All String Tyepe input */
// System.out.print("Enter line :\t");
// System.out.println("Its only take world:\n"+getString());
// System.out.println("Lenth of word:\t"+totalLength);
// flush();
// System.out.print("Enter line:\t");
// System.out.println("Entred Line:\n"+getLine());
// System.out.println("Lenth of word:\t"+totalLength);
// System.out.println("Example of python style input class");
// System.out.println(Input("Enter anyting:\t"));
// // /* All Numeric input*/
// System.out.print("Enter Short Number:\t");
// System.out.println("Enterd Short Number:\t"+getShort());
// flush();
// System.out.print("Enter Integer number:\t");
// System.out.println("Entred Number:\t"+getInt());
// System.out.println("Length of word:\t"+totalLength);
// System.out.print("Enter Long Number:\t");
// System.out.println("Entred Number:\t"+getLong());
// System.out.print("Enter floating Number:\t");
// System.out.println("Entred Floting Numver:\t"+getFloat());
// System.out.println("Lenth of word:\t"+totalLength);
// System.out.println("Entre Double Numver:\t");
// System.out.println("Entred Double Number:\t"+getDouble());
System.out.print("Enter Intiger Bunch:\t");
int[] num =getIntBunch();
System.out.println(num.length);
for (int n : num){
System.out.print(n+", ");
}
System.out.print("\nEnter Float Bunch:\t");
float[] fnum =getFloatBunch();
System.out.println(fnum.length);
for (float n : fnum){
System.out.print(n+", ");
}
}
private static ArrayList<Character> rowInput(){
ArrayList <Character> rowIn = new ArrayList<>();
char ch;
try{
while (true){
ch=(char)System.in.read();
if ( ch == '\r' )
ch=(char)System.in.read();
if ( ch == '\n' )
break;
else
rowIn.add(ch);
}
}
catch (IOException e){
System.out.print(e);
}
return rowIn;
}
public static String getString(){
ArrayList<Character> rowIn = new ArrayList<>();
char ch;
try{
while(true){
ch=(char)System.in.read();
if (ch=='\r')
ch=(char)System.in.read();
if ( ch==' ' || ch=='\t' || ch=='\n' )
break;
else
rowIn.add(ch);
}
} catch (IOException e){
System.out.println(e);
}
//copy the the row_input to word size arre
char[] word =new char[rowIn.size()];
for (int i=0; i<rowIn.size(); i++){ word[i]=rowIn.get(i);}
totalLength = word.length; //set length of word
return String.valueOf(word);//String.valueOf(word); //return after convert to String
}
public static int [] getIntBunch(){
ArrayList<Character> rowIn=rowInput();
ArrayList<Integer> rowIntArre=new ArrayList<>();
rowIn.add('\0');
ArrayList<Character> num=new ArrayList<>();
for (char ch: rowIn){
if (ch>='0' && ch<='9')
num.add(ch);
else if (num.size()>0){
rowIntArre.add(Integer.parseInt(String.valueOf(ArrayList2CharArray(num))));
num.clear();
}
}
int[] intArre=new int[rowIntArre.size()];
for(int a=0; a<intArre.length; a++){
intArre[a]=rowIntArre.get(a);
}
return intArre;
}
public static float [] getFloatBunch(){
ArrayList<Character> rowIn = rowInput();
rowIn.add('\0');
ArrayList<Float> rowFloat=new ArrayList<>();
ArrayList<Character> num=new ArrayList<>();
for (int i=0; i<rowIn.size(); i++){
if (rowIn.get(i)>='0' && rowIn.get(i)<='9' || rowIn.get(i)=='.')
num.add(rowIn.get(i));
else if (num.size()>0){
rowFloat.add(Float.parseFloat(String.valueOf(ArrayList2CharArray(num))));
num.clear();
}
}
float[] floatArre = new float[rowFloat.size()];
for (int a=0; a<floatArre.length; a++)
floatArre[a] = rowFloat.get(a);
return floatArre;
}
public static String getLine(){
ArrayList<Character> rowIn=rowInput();
char[] line =new char[ rowIn.size() ];
//line=rowIn.toArray(line);
for (int i = 0 ; i < line.length; i++){ line[i] = rowIn.get(i);}
totalLength = line.length;
//return String.valueOf(line);
return new String(line);
}
public static short getShort(){
//check
return Short.parseShort(getString());
}
public static int getInt(){
return Integer.parseInt(getString());
}
public static long getLong(){
//check
return Long.parseLong(getString());
}
public static float getFloat(){
return Float.parseFloat(getString());
}
public static double getDouble(){
//check
return Double.parseDouble(getString());
}
public static void flush() {
try{
while (System.in.available()>0)
System.in.read();
}
catch (IOException e){
System.out.println(e);
}
}
public static String Input(String arg){
System.out.print(arg);
return getLine();//String.valueOf(rowInput());
}
//=== Add-on Methhod ==========
private static char[] ArrayList2CharArray(ArrayList<Character> list){
char[] ch=new char[list.size()];
for (int i=0; i<ch.length; i++){
ch[i]=list.get(i);
}
return ch;
}
}