LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-03-2004, 12:04 PM   #1
Lifesaver
LQ Newbie
 
Registered: Mar 2004
Posts: 4

Rep: Reputation: 0
Unhappy java, reversing digit


does anyone have a java file that'll read in digits from another file and reverse the digits?
or any ideas on how i might go about doing that?
 
Old 03-03-2004, 12:12 PM   #2
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
A LIFO, either simulated ( eg, using two arrays ) or real ( eg, a stack ). This sounds too much like a homework exercise to offer you more than a hint.
 
Old 03-03-2004, 12:14 PM   #3
Lifesaver
LQ Newbie
 
Registered: Mar 2004
Posts: 4

Original Poster
Rep: Reputation: 0
i dont think our class has covered what u just talked about. so it's not really a hint to me heh
 
Old 03-03-2004, 12:34 PM   #4
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
If you read the digits from the file as strings, then you can use the StringBuffer class to reverse them. Check the Java API for info on StringBuffer.
 
Old 03-03-2004, 03:01 PM   #5
Lifesaver
LQ Newbie
 
Registered: Mar 2004
Posts: 4

Original Poster
Rep: Reputation: 0
can anyone help me with this... i'm currently doing this myself... (some code was given to me from the instructor to use in this program)
now, im stuck on the last loop, because the last loop makes num = null (because there is no more numbers), the program doesn't go on like it's supposed to. i've tried to make this into a pretested while loop, but i need to give a different value to "String oneLine = null", so it won't just exit right out of the loop. im not sure what value i can give to oneLine at the beginning for it to compile properly.

any ideas would be appriecated. thanks

String oneLine=null; /* the contents of a line of the user input file */
int numCount1=0;
do {
try {
oneLine = fileIn.readLine();
}
catch (IOException e) {
System.err.println("Sorry, unable to read a line from the file: " + filename);
System.exit(0);
}
int num = Integer.parseInt(oneLine); /* converts string to integer */
int revNum; /* holds the reversed digit */
int rightDigit=1; /* holds rightmost digit */
int numCount =-1; /* holds count of the while loop */
revNum = 0;
while (rightDigit !=0 ) {
rightDigit = num % 10;
revNum = revNum * 10 + rightDigit;
num = num / 10;
numCount = numCount + 1;
}
System.out.println("Read the line: " + oneLine);
System.out.println("Reversed is " + revNum / 10);
numCount1 = numCount1 + numCount;
System.out.println(numCount1);
} while (oneLine != null);
 
Old 03-03-2004, 04:09 PM   #6
Lifesaver
LQ Newbie
 
Registered: Mar 2004
Posts: 4

Original Poster
Rep: Reputation: 0
problem solved, program done )
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Storing four digit time in oracle genderbender Programming 1 03-11-2005 11:33 PM
reversing program greenthing Programming 3 02-27-2005 08:08 PM
54 digit numbers Scratchit General 3 06-23-2004 09:24 AM
reversing a number narendra_i Linux - Software 1 11-15-2003 09:25 AM
reversing videocard lackluster Linux - Hardware 1 11-16-2002 02:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration