Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 2nd, 2010, 11:38 AM   #1
nagu89
Newbie
 
Join Date: Mar 2010
Posts: 2
Rep Power: 0 nagu89 is on a distinguished road
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;
}
}
In the above code, the program does not work as expected if the commented lines in the getInputNumbers are uncommented. My arguement is that the code should work just fine even without the println() statement. Any cues anyone?
nagu89 is offline   Reply With Quote
Old Mar 3rd, 2010, 8:07 AM   #2
LENIN
Programmer
 
LENIN's Avatar
 
Join Date: Dec 2005
Posts: 58
Rep Power: 5 LENIN is on a distinguished road
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
LENIN is offline   Reply With Quote
Old Mar 3rd, 2010, 11:22 AM   #3
nagu89
Newbie
 
Join Date: Mar 2010
Posts: 2
Rep Power: 0 nagu89 is on a distinguished road
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...
nagu89 is offline   Reply With Quote
Old Mar 3rd, 2010, 3:03 PM   #4
LENIN
Programmer
 
LENIN's Avatar
 
Join Date: Dec 2005
Posts: 58
Rep Power: 5 LENIN is on a distinguished road
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
LENIN is offline   Reply With Quote
Reply

Bookmarks

Tags
bufferedreader, reading in a loop

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC