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 3 of 3

Thread: Can I make this code better?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can I make this code better?

    Like to see if there is a different approach to this code I might be missing. Ideas, thoughts, suggestions will be greatly appreciated.
    <import java.util.*;
    import java.io.*;
    import java.text.SimpleDateFormat;
     
    public class A2RE5161843 {
     
        public static void main(String[] args) throws IOException {
            Scanner input = new Scanner(System.in);
            int firstNum = 0, secondNum = 0;
            boolean valid = false;
            while (!valid) {
                System.out.print("Enter first number :");
                firstNum = input.nextInt();
                if (firstNum < 0) {
                    System.out.println("Enter positive number ");
                } else if (firstNum > 1000) {
                    System.out.println("Enter number less than 1000");
                } else {
                    valid = true;
                }
            }
            valid = false;
            while (!valid) {
                System.out.print("Enter second number :");
                secondNum = input.nextInt();
                if (secondNum < 0) {
                    System.out.println("Enter positive number ");
                } else if (secondNum > 1000) {
                    System.out.println("Enter number less than 1000");
                } else if ((secondNum - firstNum) < 10) {
                    System.out.println("Second number should be at least 10 greater than first number");
                } else {
                    valid = true;
                }
            }
            FileWriter fw = new FileWriter(new File("A2RE5161843.txt"));
            //Use a for loop to perform the following steps:
            //Continue writing to the same file as before.
            //Write ad label as before.
            //Output all odd numbers between firstNum and secondNum inclusive, one number per line.
            fw.write("1. All odd numbers between firstNum and secondNum inclusive");
            fw.write(System.getProperty("line.separator"));
            for (int i = firstNum; i <= secondNum; i++) {
                if (i % 2 != 0) {
                    fw.write(i + "");
                    fw.write(System.getProperty("line.separator"));
                }
            }
            //Output the sum of all numbers between firstNum and secondNum exclusive.
            fw.write("2. The sum of all numbers between firstNum and secondNum exclusive");
            fw.write(System.getProperty("line.separator"));
            int sum = 0;
            for (int i = firstNum + 1; i < secondNum; i++) {
                sum += i;
            }
            fw.write(sum + "");
            fw.write(System.getProperty("line.separator"));
            //Output all numbers from secondNum to firstNum in a single line with commas separating the numbers.
            fw.write("3. All numbers from secondNum to firstNum in a single line with commas separating the numbers");
            fw.write(System.getProperty("line.separator"));
            for (int i = firstNum; i <= secondNum; i++) {
                fw.write(i + ",");
            }
            fw.write(System.getProperty("line.separator"));
            //Write the date and time as the last line in the file in the format yyy-mm-dd hh:mm:ss.
            fw.write("4. The date and time as the last line in the file in the format yyy-mm-dd hh:mm:ss");
            fw.write(System.getProperty("line.separator"));
            fw.write((new SimpleDateFormat("yyyy-mm-dd hh:mm:ss")).format(new Date()));
            System.out.println("File Assignment2.txt written");
            fw.close();
        }
    }>


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

    Default Re: Can I make this code better?

    Can you explain what the code is supposed to do?
    There doesn't appear to be any excessive logic that controls how it will execute.

    One improvement would be to get the line separator into a local variable and use that instead of continually calling the getProperty method.
    Also the line separator value could be appended at the end of the String that is written instead of making a separate call to the write() method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can I make this code better?

    I am new to JAVA programming, and take classes online. This is one of my assignments, and trying to basically self teach myself how to do this. Here is the requirement:

    Write a console program that uses a while loop to perform the following steps:
    Prompt the user to input two integers: firstNum and secondNum where secondNum is at
    least 10 greater than firstNum, both numbers are positive integers, and secondNum is less than 1000.

    Verify that the user entered acceptable numbers, and if not, provide error feedback and prompt them again.

    Output all results to a file in the same directory as the program, placing
    an appropriate label between each section of output.
    Note that your program must be able to run repeatedly overwriting the file from the previous run.

    Output all odd numbers between firstNum and secondNum inclusive, one number per line.
    Output the sum of all numbers between firstNum and secondNum exclusive.
    Use a for loop to perform the following steps:
    Continue writing to the same file as before.
    Write ad label as before.
    Output all numbers from secondNum to firstNum in a single line with commas separating the numbers.
    Write the date and time as the last line in the file in the format yyy-mm-dd hh:mm:ss.
    Notes:
    Inclusive means that the firstNum and secondNum are output if appropriate
    Exclusive means that the firstNum and secondNum are not output

Similar Threads

  1. can someone please make this code
    By poldz123 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 19th, 2012, 05:50 PM
  2. I NEED HELP, HOW TO MAKE THIS CODE!!!
    By tahsim in forum Member Introductions
    Replies: 1
    Last Post: March 20th, 2012, 09:05 AM
  3. How to make this code even simpler
    By blakmaze in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2011, 03:00 PM
  4. How to make code more efficient?
    By Apocalypse in forum Java Theory & Questions
    Replies: 2
    Last Post: October 21st, 2011, 09:07 AM
  5. need help to make my code flexible.
    By sagar474 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 29th, 2011, 12:11 PM