Welcome to Dream.In.Code
Become a Java Expert!

Join 149,510 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,348 people online right now. Registration is fast and FREE... Join Now!




Urgent! Displaying Palindrome Numbers Using Indexes from .txt file

 
Reply to this topicStart new topic

Urgent! Displaying Palindrome Numbers Using Indexes from .txt file

futura91
16 Aug, 2007 - 07:00 PM
Post #1

New D.I.C Head
*

Joined: 27 Jul, 2007
Posts: 12


My Contributions
i saw someone here who was also dealing with palindromes. but in my case, i need to create a .txt file containing a series of integer i which is >=1 and <=100. this should be placed one integer per line. integer i serves as the index of the palindrome numbers. then, another .txt file should be created which contains lines of numbers which serves as the input. these numbers correspond to the palindrome number at the i-th index. the line is terminated by a zero.

sample input:
1
12
24
0

sample output:
1
33
151

i'm sort of ashamed to place my code here because i don't really know what to do. i sort of get the logic but i can't put it into code...

CODE
import javax.swing.*;
import java.io.*;
import java.util.*;

public class MP2{
    public static void main(String[] args)
    throws IOException{
        String fileName="/*This is where you will place the location of the .txt file*/";
        FileReader fr=new FileReader(fileName);
        BufferedReader br=new BufferedReader(fr);
        String inLine;
        boolean repeat=true;
        while(repeat){            /*i really don't know what to do here...*/
            inLine=br.readLine();
            if(inLine==null){
                repeat=false;
                break;
            }
            else{
                String inStream;
                StringTokenizer st=new StringTokenizer(inLine);
                while(st.hasMoreTokens()){
                    inStream=st.nextToken();
                    int num=Integer.parseInt(inStream);
                    if(num==0){
                    break;
                    }
                    else{
                        System.out.println(num);
                        int[] palArr=new int[num+1];
                        for(int i=0; i<inStream.length(); i++){
                            palArr[i]=num;
                            
                        }
                    }
                }
            }
        }
        fr.close();
    }
}


i can only print the input from the .txt file. i don't know how i'm going to go about using the indexes to find the i-th palindrome number. please help me.


and i'm sorry if my code is terrible...
User is offlineProfile CardPM
+Quote Post

alpha02
RE: Urgent! Displaying Palindrome Numbers Using Indexes From .txt File
16 Aug, 2007 - 09:45 PM
Post #2

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 687


Dream Kudos: 850
My Contributions
Here's something that should work:

CODE
try{
    //Read the text file
    DataInputStream in = new DataInputStream(new FileInputStream("input.txt"));
    String contents = "";
    while (in.available() != 0){
        contents = contents.concat(in.readLine()+"\n");
    }
    in.close();

    //Get the indexes listed in the file
    String[] inx = contents.split("\n");
    int[] indexes = new int[inx.length];
    for (int i=0; i<inx.length; i++){
        indexes[i] = Integer.parseInt(inx[i]);
    }

    //Open the output file
    PrintStream p = new PrintStream(new FileOutputStream("output.txt"));

    //Get the palindromes and write to file
    for (int i=0; i<indexes.length; i++){
        int num = 0;
        int pid = 0;
        int rid = indexes[i];
        while (pid < rid){
            if (new StringBuffer(String.valueOf(num)).reverse().toString().equals(String.valueOf(num)) == true){
                pid += 1;
            }
            num += 1;
        }
        p.println(String.valueOf(num));
    }
    p.close();
}
catch (IOException err){}


I haven't tested it yet, up to you now:) Hope this helps.
User is offlineProfile CardPM
+Quote Post

futura91
RE: Urgent! Displaying Palindrome Numbers Using Indexes From .txt File
16 Aug, 2007 - 10:21 PM
Post #3

New D.I.C Head
*

Joined: 27 Jul, 2007
Posts: 12


My Contributions
thank you very much for your immediate reply.

i really appreciate it.

User is offlineProfile CardPM
+Quote Post

futura91
RE: Urgent! Displaying Palindrome Numbers Using Indexes From .txt File
18 Aug, 2007 - 08:59 PM
Post #4

New D.I.C Head
*

Joined: 27 Jul, 2007
Posts: 12


My Contributions
thanks... icon_up.gif
it works but the output is like this:

INPUT:
1
12
24
0

OUTPUT:
1
23 (should be 33)
142 (should be 151)

would you mind explaining more about this part to me???

CODE
    for (int i=0; i<inx.length; i++){
                    indexes[i] = Integer.parseInt(inx[i]);
                }

                   PrintStream p = new PrintStream(new FileOutputStream("C:\\output.txt"));
                   for (int i=0; i<indexes.length; i++){
                    int num = 0;
                    int pid = 0;
                    int rid = indexes[i];
                    while (pid < rid){
                        if (new StringBuffer(String.valueOf(num)).reverse().toString().equals(String.valueOf(num)) == true){
                            pid += 1;
                        }
                        num += 1;
                    }


i studied the code you gave me. maybe i can make some revisions if i could understand it more... hope you don't mind. thanks again. it made my work easier.
User is offlineProfile CardPM
+Quote Post

alpha02
RE: Urgent! Displaying Palindrome Numbers Using Indexes From .txt File
18 Aug, 2007 - 09:23 PM
Post #5

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 687


Dream Kudos: 850
My Contributions
CODE
for (int i=0; i<inx.length; i++){
                    indexes[i] = Integer.parseInt(inx[i]);
                }


We take all lines in the text file and stores them in a int array.

CODE
PrintStream p = new PrintStream(new FileOutputStream("C:\\output.txt"));


We open a connection to the file to allow us to write our output.

CODE
for (int i=0; i<indexes.length; i++){
                    int num = 0;
                    int pid = 0;
                    int rid = indexes[i];
                    while (pid < rid){
                        if (new StringBuffer(String.valueOf(num)).reverse().toString().equals(String.valueOf(num)) == true){
                            pid += 1;
                        }
                        num += 1;
                    }


We loop through all input numbers to get the corresponding palindromes. The variables:
num = The number we are looking if it is a palindrome.
pid = The index of palindrome we are at.
rid = The WANTED palindrome index.

The if line checks if the number is a palindrome. If yes, the palindrome index increments.

User is offlineProfile CardPM
+Quote Post

futura91
RE: Urgent! Displaying Palindrome Numbers Using Indexes From .txt File
18 Aug, 2007 - 11:06 PM
Post #6

New D.I.C Head
*

Joined: 27 Jul, 2007
Posts: 12


My Contributions
oh... okay. thanks again. now i understand. i'll try to work this out.

thanks so much for your help!!! biggrin.gif
User is offlineProfile CardPM
+Quote Post

alpha02
RE: Urgent! Displaying Palindrome Numbers Using Indexes From .txt File
18 Aug, 2007 - 11:06 PM
Post #7

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 687


Dream Kudos: 850
My Contributions
QUOTE(futura91 @ 19 Aug, 2007 - 03:06 AM) *

oh... okay. thanks again. now i understand. i'll try to work this out.

thanks so much for your help!!! biggrin.gif


You're welcome
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 07:29PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month