8 Replies - 4265 Views - Last Post: 14 June 2012 - 02:47 PM Rate Topic: -----

#1 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

New to Java (Help w/ perfect numbers & palindromes)

Posted 13 June 2012 - 12:08 PM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: New to Java (Help w/ perfect numbers & palindromes)

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,794
  • Joined: 27-December 08

Re: New to Java (Help w/ perfect numbers & palindromes)

Posted 13 June 2012 - 12:18 PM

Quote

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.

For your perfect numbers section, are you trying to determine if the numbers are perfect or not? What specific problems or errors are you encountering with it?

Quote

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.

The String class has a toLowerCase() and a replace() method. You can replace() whitespace. You can also search for a regular expression to find punctuation, and again replace() punctuation with an empty String: "". Remember that Strings are immutable, so when you replace() on a String, a new String is returned with the changes. So you will have to:
String s = "123";
s = s.replace("2", ""); //remove 2



If you don't assign s.replace() back to s or another String variable, the changes will not be saved.
Was This Post Helpful? 0
  • +
  • -

#3 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

Re: New to Java (Help w/ perfect numbers & palindromes)

Posted 13 June 2012 - 12:28 PM

I got the perfect numbers part to work, but I am having trouble having the first line be only prime numbers and the second line only perfect numbers. My output combines them altogether and is one long line of numbers.

And for the palindrome part, in which of my lines do I have to look more closely into? I kind of understand what you are talking about from what you said so far.

View Postmacosxnerd101, on 13 June 2012 - 12:18 PM, said:

Quote

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.

For your perfect numbers section, are you trying to determine if the numbers are perfect or not? What specific problems or errors are you encountering with it?

Quote

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.

The String class has a toLowerCase() and a replace() method. You can replace() whitespace. You can also search for a regular expression to find punctuation, and again replace() punctuation with an empty String: "". Remember that Strings are immutable, so when you replace() on a String, a new String is returned with the changes. So you will have to:
String s = "123";
s = s.replace("2", ""); //remove 2



If you don't assign s.replace() back to s or another String variable, the changes will not be saved.

Was This Post Helpful? 0
  • +
  • -

#4 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

Re: New to Java (Help w/ perfect numbers & palindromes)

Posted 13 June 2012 - 01:13 PM

I am still having trouble with my program. This is my partially updated code
import java.io.*;
import java.util.*;

public class Program3
{
	public static void main(String args[] ) throws Exception
	{
		if (args.length < 3)
		{
			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 + " ");
				break;
			}
			}
		}
		
		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 still can't figure out how to have my palindrome program work while ignoring white spaces and ALL punctuation. I want it to print out only the palindromes back onto my screen from a list of words from a .txt file.

Also, can someone tell me how to make it so my prime numbers are all on one line and my perfect numbers are all outputted on a separate line. When I run my program as I do now, they are all concatenated as one but I wish to separate them.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,794
  • Joined: 27-December 08

Re: New to Java (Help w/ perfect numbers & palindromes)

Posted 13 June 2012 - 03:34 PM

Quote

Also, can someone tell me how to make it so my prime numbers are all on one line and my perfect numbers are all outputted on a separate line.

A System.out.print() call will append to the current line. Use System.out.println() instead. Also, to check if a number is prime, you only have to go from i = 2 up to the sqrt(n).
while(infile.hasNextInt()) {
    int num = infile.nextInt();

    if(num == 2){
       //print that it is a prime
       continue; //and continue to the next number
    }

    else if(num % 2 == 0 && num != 2){
        //number isn't prime
        //so continue
        continue;
    }
    else{
        double sqrt = Math.sqrt(num);
        boolean isPrime = true; //assume prime until we disprove
         
        //skip evens because no evens 
        //other than 2 are prime
        for(int i = 3; i < sqrt; i += 2){
            if(num % i == 0){
                isPrime = false;
                break; //num isn't a prime
            }
        }
    }

    //now finally, if isPrime is true, print num 
}




Quote

I still can't figure out how to have my palindrome program work while ignoring white spaces and ALL punctuation. I want it to print out only the palindromes back onto my screen from a list of words from a .txt file.

Try using the following regular expression in your replace() method: \\p{P}+.
String test = "This is a test.";
test = test.replace("\\p{P}+", ""); //replace punctuation
test = test.replace(" ", ""); //replace spaces



I'm not a regular expressions expert and found this online. It should replace punctuation. If not, you may have to search for a regular expression that replaces punctuation.
Was This Post Helpful? 0
  • +
  • -

#6 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

Re: New to Java (Help w/ perfect numbers & palindromes)

Posted 13 June 2012 - 03:55 PM

Is this how I enter it? Sorry again, I'm really new to java. But this isn't working either cause I am getting only the single word palindromes repeated to me on cmd prompt. The ones with spaces and punctuation arn't being recognized. And the single worded palindromes for some reason are repeated multiple times in my output.

while( infile.hasNext())
		{
			String word = infile.next();
			word = word.replace("\\p{P}+", "");
			word = word.replace(" ", "");
			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 + " ");
				}
			}
		}


This post has been edited by macosxnerd101: 13 June 2012 - 07:18 PM
Reason for edit:: Removed quote

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,794
  • Joined: 27-December 08

Re: New to Java (Help w/ perfect numbers & palindromes)

Posted 13 June 2012 - 07:19 PM

To read in an entire line, use the Scanner nextLine() method. The next() method will read in tokens separated by spaces. Also, use the hasNextLine() method to check instead of the hasNext() method.
Was This Post Helpful? 0
  • +
  • -

#8 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

Re: New to Java (Help w/ perfect numbers & palindromes)

Posted 13 June 2012 - 09:27 PM

This is the newer and slightly improved code.
My output is as follows:
C:\Users\David>java Program3 prime.txt perfect.txt palindrome.txt
2 3 5 7 11 13 17 19 23 29 31 53 61 67
6 28 496 8128
dad radar racer racecar bob mom stanleyyelnats

The prime and perfect numbers block works fine, just as I want. The palindrome block works for only the single words with no spaces or punctuation.

My palindrome.txt file includes the following words, each on their on line: dog, dad, radar, bucket, racer, racecar, bob, toy, cat, mom, rock, help, stanleyyelnats, work, (Madam, I'm Adam), (A man, a plan, a canal, panama!). I put the ones in parenthesis that are not working in my program. Yes those would be two palindromes.

Not sure where to head from here. Whether I need to make them all the same case, which I am pretty sure I did, or how to ignore all the spaces and punctuation.

import java.io.*;
import java.util.*;

public class Program3
{
	public static void main(String args[] ) throws Exception
	{
		if (args.length < 3)
		{
			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 + " ");
			}
		}
		System.out.print("\n");
		
		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 + " ");
				break;
			}
			}
		}
		System.out.print("\n");
		
		infile = new Scanner(new File(args[2]));
		while( infile.hasNextLine())
		{
			String word = infile.nextLine();
			word = word.replace("\\p{P}+", "");
			word = word.replace(" ", "");
			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 + " ");
					break;
				}
			}
		}
	}
}



View Postmacosxnerd101, on 13 June 2012 - 07:19 PM, said:

To read in an entire line, use the Scanner nextLine() method. The next() method will read in tokens separated by spaces. Also, use the hasNextLine() method to check instead of the hasNext() method.


Almost forgot, the output I wish to get is:
C:\Users\David>java Program3 prime.txt perfect.txt palindrome.txt
2 3 5 7 11 13 17 19 23 29 31 53 61 67
6 28 496 8128
dad
radar
racer
racecar
bob
mom
stanleyyelnats
Madam, I'm Adam
A man, a plan, a canal, panama!

So I do want punctuation, case, and spaces in my output, remebering that each is on their own line.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,794
  • Joined: 27-December 08

Re: New to Java (Help w/ perfect numbers & palindromes)

Posted 14 June 2012 - 02:47 PM

You will want to make your String lower cased before testing if it is a palindrome. Also, if you want the original phrase to print out in tact, assign the updates of replace() and toLowerCase() to a different String variable and test that different variable to see if it is a palindrome.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1