import java.io.*;
import java.util.*;
public class Program3
{
public static void main(String args[] ) throws Exception
{
if (args.length < 2)
{
System.out.println("Must put filename on cmd line\n");
System.exit(0);
}
Scanner infile = new Scanner ( new File(args[0]));
while( infile.hasNext())
{
int num = infile.nextInt();
int i;
for (i = 2; i < num; i++)
{
int n = num%i;
if (n==0)
{
break;
}
}
if (i==num)
{
System.out.print(num + " ");
}
}
infile = new Scanner(new File(args[1]));
while(infile.hasNext())
{
int num = infile.nextInt();
int sum=0;
for(int i=1; i<num; i++)
{
if(num%i==0)
{
sum+=i;
}
if(sum==num)
{
System.out.print(num + " ");
}
}
}
infile = new Scanner(new File(args[2]));
while( infile.hasNext())
{
String word = infile.next();
int left = 0;
int right = word.length()-1;
while(left < right)
{
if (word.charAt(left) != word.charAt(right))
{
break;
}
left++;
right--;
}
for (int i=0; i<left; i++)
{
word = word.toLowerCase();
word.charAt(i);
if (Character.isLetterOrDigit(word.charAt(i)))
{
System.out.print(word + " ");
}
}
}
}
}
I basically got as far as I could and now I'm stuck. What I have to do is input 3 files (like .txt files). Once is a prime.txt which includes prime numbers and some not, a perfect.txt, contains perfect numbers and non perfect numbers, and a palindrome.txt. The prime block of code works fine. The perfect and palindrome parts are the parts I'm having trouble with. I am getting a HUGE amount of numbers and words fed back onto the screen. That is my first problem.
Secondly, I am not too sure if my coding for perfect is working at all cause there are so many numbers being spit back onto my command prompt.
Lastly, I want to make my palindrome block of code ignore spaces, punctuation, and letter case. I have tried looking it up on google but I keep on failing to correct my problem.
Sorry for the long amount of help needed, I am new to Java and trying to learn.
As always, thank you to anyone that helps me.

New Topic/Question
Reply



MultiQuote









|