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: Different approach to this code

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

    Lightbulb Different approach to this code

    Instructor informed me today that my code is similar to another students and I must redo it. I am not quite sure what to change to give her what she is asking for. Any suggestions? new to JAVA.

    <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,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Different approach to this code

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    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: Different approach to this code

    thanks! Done!

Similar Threads

  1. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  2. Best way to approach this problem
    By Myshkin in forum Collections and Generics
    Replies: 1
    Last Post: September 30th, 2012, 09:19 AM
  3. How would I approach this?
    By thatguywhoprograms in forum Java Theory & Questions
    Replies: 5
    Last Post: December 20th, 2011, 10:08 PM
  4. How would you approach this?
    By Staticity in forum Java Theory & Questions
    Replies: 3
    Last Post: October 10th, 2011, 12:09 AM
  5. How best to approach this project?
    By eb_dev in forum Java Theory & Questions
    Replies: 0
    Last Post: March 22nd, 2011, 11:43 AM