Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help With Three Part Program

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help With Three Part Program

    Ok, so I have to write a program that takes a low and high number and an input filename from the command line and has to do the following three things:

    1) Print all primes numbers in the range from low to high on a single line, separated by a space.
    2) Print all perfect numbers in the range from low to high on a single line, separated by a space.
    3) Echo the words from the input file and indicate if they are palindromes or not.

    My code is as follows:



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

    public class Program3
    {

    public static void main (String[] args)
    {
    try
    {

    int lo=0,hi=0; // range of #s to look for primes and perfects
    String infileName; // file of Strings, one per line
    BufferedReader infile = null;

    if (args.length < 3)
    {
    System.out.println("CMD LINE INPUT ERROR: Must enter 2 numbers followed by name of input file on cmd line.");
    System.exit(0);
    }

    lo=Integer.parseInt(args[0]);
    hi=Integer.parseInt(args[1]);
    infileName=(args[2]);

    for(int n=lo; n<=hi; n++){
    boolean isPrime = (n%2 != 0);
    int divisor=3;

    while(isPrime && divisor<n/2);
    {
    if(n%divisor==0)
    isPrime=false;
    else
    divisor++;

    if (isPrime)
    System.out.print(n + " ");
    System.out.println();

    for (int perfectNum=lo; perfectNum<=hi; perfectNum++)
    {
    int total=0;
    for (int perfectDiv=1 ; perfectDiv<perfectNum;perfectDiv++)
    {
    if (perfectNum % perfectDiv==0) total=total+perfectDiv;
    }
    if (perfectNum==total) System.out.print(perfectNum + " ");
    }

    }
    String line=null;
    BufferedReader bufferedReader = new BufferedReader(new FileReader(infileName));
    while (infile.ready())
    {
    line = infile.readLine();
    }
    boolean isPalindrome=true;

    int lineLength = line.length();
    int middleOfline = lineLength/2;

    for (int beginningLetter = 0; beginningLetter<middleOfline; beginningLetter++ )
    {
    if (line.charAt(beginningLetter) != line.charAt(lineLength-beginningLetter-1))
    {
    isPalindrome=false;
    }
    if (isPalindrome)
    System.out.println( line + " IS a palindrome." );
    else
    System.out.println( line + " NOT a palindrome." );


    }

    }


    }
    catch ( Exception e)
    {
    StringWriter sw = new StringWriter();
    e.printStackTrace(new PrintWriter(sw));
    System.out.println("EXCEPTION CAUGHT: " + sw.toString() );
    System.exit( 0 );
    }
    }
    }

    I'm getting a few problems. First of all, whenever I input any number greater than 7, my terminal only displays 1, 3, 5, and 7, but no other primes. My perfect number section is printing 6 and 28 multiple times instead of one on the same line, separated by a space. Finally, I'm not sure that my BufferedReader syntax is correct, and each time I attempt to execute the problem, it says that the file I am trying to read does not exist.

    Any help or suggestions would be greatly appreciated!

    Thanks,
    Jay


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help With Three Part Program

    Can you copy and paste here the full contents of the console window from when you execute the program that shows the input to the program and its output?

    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    Last edited by Norm; September 27th, 2012 at 04:36 PM.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. What does this part of the code mean?
    By jean28 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 23rd, 2012, 11:47 PM
  2. Draw and arc under part of a string
    By lammybun in forum Java Theory & Questions
    Replies: 3
    Last Post: February 26th, 2012, 12:26 PM
  3. Replies: 1
    Last Post: April 26th, 2011, 08:47 AM
  4. A Part Question
    By Aksand in forum Java Theory & Questions
    Replies: 5
    Last Post: April 13th, 2011, 11:12 AM
  5. [SOLVED] Help Again Last Part of my Problem :(
    By metalx66 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2011, 09:48 PM