![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2010
Posts: 2
Rep Power: 0
![]() |
bizarre behaviour with BufferedReader
import java.io.DataInputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
DataInputStream reader = new DataInputStream(System.in);
int numOfCases;
numOfCases = Integer.parseInt(reader.readLine());
int[] number = new int[numOfCases];
int[] result = new int[numOfCases];
number = getInputNumbers(reader, numOfCases, number);
for (int i = 0; i < numOfCases; i++) {
result[i] = findNextPalindrome(number[i]);
System.out.println(result[i]);
}
}
private static int[] getInputNumbers(DataInputStream reader, int numOfCases, int[] number) throws IOException {
for (int i = 0; i < numOfCases; i++) {
//System.out.println("Enter number " + (i + 1)); For some reason that I cant figure //out, My //[program wont work without the above line
String userInp = reader.readLine();
number[i] = Integer.parseInt(userInp);
}
return number;
}
private static int findNextPalindrome(int input) {
int mid;
int returnThis = 0;
if (input <= 10) {
return 11;
} else {
String strInp = "" + input;
char[] charArray = strInp.toCharArray();
int len = strInp.length();
char[] mirroredCharArray = findMirrorImage(charArray);
String strMirrored = new String(mirroredCharArray);
int intInp = Integer.parseInt(strInp);
int intMirrored = Integer.parseInt(strMirrored);
if (intInp < intMirrored) {
return intMirrored;
} else if (intInp >= intMirrored) {
if (len % 2 == 0) {
mirroredCharArray = incrMidDigit(strInp.toCharArray());
char[] data = findMirrorImage(mirroredCharArray);
returnThis=Integer.parseInt(String.valueOf(data));
} else {
mirroredCharArray = incrMidDigit(strInp.toCharArray());
returnThis=Integer.parseInt(String.valueOf(findMirrorImage(mirroredCharArray)));
}
}
}
return returnThis;
}
private static char[] incrMidDigit(char[] charArray) {
int len = charArray.length;
int mid;
if(len%2==1)
mid=len/2;
else
mid=(len/2)-1;
charArray = incrDigit(charArray,mid);
return charArray;
}
private static char[] incrDigit(char[] charArray, int index) {
if (charArray[index] == '9') {
if (index == 0) {
charArray[index] = '0';
charArray = "1".concat(String.valueOf(charArray)).toCharArray();
} else {
charArray[index] = '0';
charArray = incrDigit(charArray, index - 1);
}
} else {
charArray[index] += 1;
}
return charArray;
}
private static char[] findMirrorImage(char[] charArray) {
int len = charArray.length;
int mid = len / 2;
for (int i = 0; i < mid; i++) {
charArray[(len-1) - i]=charArray[i];
}
return charArray;
}
} |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Dec 2005
Posts: 58
Rep Power: 5
![]() |
Re: bizarre behaviour with BufferedReader
Not much to say really, the DataInputStream just acts silly.
http://java.sun.com/j2se/1.4.2/docs/...readLine%28%29 The BufferedReader solution works nicely.
__________________
Science progresses one death at a time - Niels Bohr |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2010
Posts: 2
Rep Power: 0
![]() |
Re: bizarre behaviour with BufferedReader
Tried it without DataInputStream too. tried it with
BufferedReader reader = new BufferedReader(new(InputStreamReader(System.in))); Does'nt give the required behaviour either... |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Dec 2005
Posts: 58
Rep Power: 5
![]() |
Re: bizarre behaviour with BufferedReader
Can you explain the problem in more detail. I tested it with the BufferedReader and it worked nicely. It worked the same with or without the println in the previous line (the DataInputStream however did not work without the println)
__________________
Science progresses one death at a time - Niels Bohr |
|
|
|
![]() |
| Bookmarks |
| Tags |
| bufferedreader, reading in a loop |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Emulating Behaviour Of "mysql_real_escape_string" | Sane | Python | 2 | Nov 28th, 2007 4:54 PM |
| RadioButtonList Strange Behaviour | Iftikhar | ASP.NET | 0 | Oct 27th, 2006 6:40 AM |
| Is this undefined behaviour? | InfoGeek | C++ | 26 | Jun 27th, 2006 9:17 AM |
| BufferedReader | Eric the Red | Java | 8 | Apr 28th, 2006 11:05 PM |
| Bizarre TI-89 matrix algebra error (not language specific) | Generic | Other Scripting Languages | 0 | Sep 7th, 2005 8:16 PM |