Need help with my project

Need help with my project

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.

Uses a for loop to perform the following steps:

-Continue writing to the same file as before. -Write a 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 yyyy-mm-dd hh:mm:ss.

View Answers

June 23, 2012 at 3:41 PM

The given code prompt the user to input two integers and validate them. Then. we have calculated the odd numbers between two input values,sum of values and output all numbers from second input number to first one.

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

class Examples{

    public static void main(String[] args) throws Exception{
        Scanner input=new Scanner(System.in);
        System.out.print("Enter Num1: ");
        int firstNum =input.nextInt();

        System.out.print("Enter Num2: ");
        int secondNum =input.nextInt();
        while(firstNum<0){
            System.out.print("Number should be positive.!Re-enter: ");
            firstNum =input.nextInt();
        }
        while(secondNum<0){
            System.out.print("Number should be positive.!Re-enter: ");
            secondNum =input.nextInt();
        }
        while(secondNum>1000){
            System.out.print("Second Number should be less than 1000!Re-enter: ");
            secondNum =input.nextInt();
        }
        while(secondNum<firstNum){
            System.out.print("Second Number should be at least 10 greater than firstNum.Re-enter: ");
            secondNum =input.nextInt();
        }
        int sum=0;
        File f=new File("c:/numbers.txt");
        BufferedWriter bw=new BufferedWriter(new FileWriter(f,true));
        bw.write("Odd numbers from "+Integer.toString(firstNum)+" to "+Integer.toString(secondNum));
        bw.newLine();

            for(int i=firstNum;i<=secondNum;i++){
                sum+=i;
                if((i%2)!=0){
                    bw.write(Integer.toString(i));
                    bw.newLine();
                }
            }

            bw.write("Sum of Numbers: "+sum);
            bw.newLine();
            bw.write("Output all numbers from secondNum to firstNum:");
            bw.newLine();
            for(int i=secondNum;i>=firstNum;i--){
                bw.write(Integer.toString(i)+",");
            }
            bw.newLine();
            Date d=new Date();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
            bw.write(sdf.format(d));
            bw.close();
    }
}









Related Tutorials/Questions & Answers:
Need help with my project
Need help with my project  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
pls i need help with my assignment
pls i need help with my assignment  how to write a code that ask the user for the height of the triangle and prints the triangle using * eg if height is 3 it prints * and also using import java.util.Scanner
Advertisements
Help With My Java Project?
Help With My Java Project?  Write a console program that repeatedly prompts the user to enter data until they type done (any case, Upper, Lower... to put it all together to make it work. please help import
please help.. this my importtant project..!!!!!
please help.. this my importtant project..!!!!!  Consider the Algebra: Solving quadratic equations. The two roots of a quadratic equation ax2 + bx + c = 0 can be obtained using the formula: ???????????1= ?????+?????2
I need help on my Java code.... please please help me out!?
I need help on my Java code.... please please help me out!?  Well my... in the file. Also it should be displayed on the screen. However my code doesn't display... need a part of the code thats fixed if possible! Heres is the code: import
need help....how to connect and disconnect multiple databases(databases created in mysql) using java and my sql
need help....how to connect and disconnect multiple databases(databases created in mysql) using java and my sql  i am working on a project on deadlock in distributed transactions , and in that i am using my sql in java i need
Need help
Need help  Hello... I need some help I have a method which contains 1 string value and i wnat when this method get called den that string value..." shud b replaced by string value"s" so my new arrayname will b the string
Need help
Need help  Hello... I need some help I have a method which contains 1 string value and i wnat when this method get called den that string value..." shud b replaced by string value"s" so my new arrayname will b the string
Need help
Need help  Dear sir, I have a problem. How to write JSP coding, if a user select a value from drop down list for example department, the another drop.... This name list should get from the database. Please help me. By the way, I'm
Need help with this!
Need help with this!  Can anyone please help me... to a file at all at this time. Any help would be greatly appreciated, thank you... to effectivly end the loop with out the need to break it. for(i=(0); i <
hello. can anyone help me with my java project, using jcreator?
hello. can anyone help me with my java project, using jcreator?  this is how it goes.. we are to develop a GUI of a certain form, in my case this one... just couldn't solve it.. alot of errors.. pls do help... thanks
Need Help on JMS - JMS
Need Help on JMS   Hi, In my application i need to create my own ques and my own QueueConnectionFactory..... Plz tell me how to create the same... Any help is appriciated,.. Thanks in advance
need help please
need help please  Dear sir, my name is logeswaran. I have a big problem that I can't find the solution for a program. How can I block a user from enter a page second time. Please help me. By the way I'm using Access database
Friends need a help on ruby..
Friends need a help on ruby..  Friends i need a ruby script and the requirement is When i am running a ruby script in cmd, after executing my script, a html report will be generated having my console output details. For ex
Need help with nested queries
Need help with nested queries  Hello, Table1 has "id", "votes" columns. Table2 has "id", "Name" column. I need to retrieve Name and its corresponging Votes. I tried with nested queries but its confusing. Can anyone please help
Need Help on the script
Need Help on the script  hello there, My name is Femi and am a web developer using php. am working on a project. and am having some difficulties. i dont know if you can help me out with it. The platform am building will receive
i need project for shopping cart in struts 1 with the oracle database and give clear explanation for how to execute it in my eclipse
i need project for shopping cart in struts 1 with the oracle database and give clear explanation for how to execute it in my eclipse  i need a project for shopping cart in struts1 with the oracle database and give clear
Need Help in Java programming
Need Help in Java programming  Hello. Can someone please help me with writing the following program Java program that gives assignment details such as:assignment number,assignment name,due date,submission date,percentage marks
need help with program
need help with program  To write a program to read a set of words from a file and return the following 1)Each word in the file against its frequency 2) the frequency should be in the descending order of the frequencies
need help to create applet
need help to create applet  Can u help me..?? I get a task ...and i dont know how to make it... So I hope that u can help me... Here is the task... e) CardLayout If u can help me, then please
need help with program
need help with program  To write a program where we can take two integer array as input,Find the missing number from array"B" has all the numbers from array"A" except one,and find the fastest way in doing
in my project, i have to add scrollbars to a panel,. i need some example on it,how to doit.
in my project, i have to add scrollbars to a panel,. i need some example on it,how to doit.  in my project, i have to add scrollbars to a panel,. i need some example on it,how to doit. Thanks In Advance   Please
Need help on JAVA JSP
Need help on JAVA JSP  Hi, I have never worked on java and I have been given an assignment where I have to fix existing issues in the tool(created...(need to know how I can adjust it based on the cureen system's screen resolution
PLEASE HELP WITH MY JAVA
PLEASE HELP WITH MY JAVA  Hey my name is Gavin and im a student at school that takes IT. my teacher has gave me a problem and i can't figure it out please help!!!!!!!! it is a for-loop question: Display the first 5 multiples
Need some help urgently
Need some help urgently  Can someone please help me with this below question. I need to write a class for this. If you roll Y standard six-sided dice... need to write a class for this where X and Y are passed as parameter. If someone
Need Help - Java Beginners
Need Help  Hello Sir, Am a beginner of Java. Also i did course on J2EE... but dont have any ideas about real time projects. But i want to do some projects in Java as well as J2EE... Can u help me and guide to do a project
Need Project
Need Project  How to develop School management project by using Struts Framework? Please give me suggestion and sample examples for my project
hello there i need help
hello there i need help  : i need to do a program like this: Automatic Teller Machine [B] Balance [D] Deposit [W] Withdrawal [Q] Quit select you... i am a beginner, and aside from that i am really eager to learn java please help
Need help with console program?
Need help with console program?  Write a console program that repeatedly prompts the user to enter data until they type done (any case, Upper, Lower, or Mixed). thanks in advance.   Here is an example that repeatedly
Need Help With This Scenario
Need Help With This Scenario  The bonnet of a car is opened to determine if there is anything wrong with the engine. Firstly, the radiator water is checked. If the radiator water is below the minimum indicator level
Need Help With This Scenario
Need Help With This Scenario  The bonnet of a car is opened to determine if there is anything wrong with the engine. Firstly, the radiator water is checked. If the radiator water is below the minimum indicator level
i need a help in this please
i need a help in this please  The factorial of a nonnegative integer n is written n! (pronounced ?? n factorial??) and is defined as follows: n!=n . (n-1) . (n-2) . .... . 1 (for values of n greater than or equal to 1) and n!=1
need help with a program - Java Beginners
Java algorithm - need help with a program  Java algorithm - need help with a program
need help...................please
need help...................please  I have a problem in my JSP Coding. How to retrive value from database in to text field when user select one value... on this area. Please help me. By the way, I'm using access Database and JSP coding
need help - Java Beginners
need help  Need help in programming in Java  Simple java program that will show the output within quotes using system.out.println();DISPLAYING OUPUT " * " ," *** " ........I used System.out.print
Need help - Java Beginners
Need help   To Write a Java program that asks the users to enter a m * n matrix of integers, m and n being the number of rows and columns...; Hi Friend, Please try the following code. We hope that this code will help
seriously need help....
seriously need help....  Write a program that will prompt the user to select an appliance, lookup the energy the appliance uses and for each time of use rate band: calculate the cost to run the appliance and display a bar graph
Need help in java programming
Need help in java programming  Hello. Can someone please help me with writig the following programm. Assignment 20% Presentation 10% Mini Test 10% Exam 60% Java program that accepts the following details: student
I have need to help
I have need to help  Write a program that, for four points A, B, C and P, draws a triangle formed by ABC and a small cross showing the position of P; and displays a line of text indicating which of the following three cases
need project
need project  hi im new on this site so dnt knw actual procedure . but i need a project on banking system in java with sql database. which should... is good plz add it i need project till Saturday i.e 10th march 2012 plzzzz
need help - Swing AWT
need help  Create a program to correct and grade a set of multiple choice test result. this is a console program that uses JOptionPane dialog boxes as well. Must read a set of 10 correct answers, using a JOptionPane dislog box
Need urgent help with C++ errors!
Need urgent help with C++ errors!  hi, i'm new to C++ programming. this is my code... i'm using Turbo C++. It's showing so many errors!.. I don't know what to do. Please help!! #include<iostream.h> void main
help on project - JSP-Servlet
help on project  Need help on Java Project
my project
my project   how to creat a e learnig site with student information , faculty details , student queries , student feedback, course information
need help with a program - Java Beginners
need help with a program   Part I An algorithm describes how... by the user. The output of the program should be the length and width (entered.... First you would need to make up the "test data". For this algorithm, the test data
Please Help To Solve My Problem
Please Help To Solve My Problem  PHP I Have 6 Textbox and 1 ok button. 6 Textbox to Add the 6 Team Name. Each Team Name In String(Like- A,B,C,D,E,F... Are There Then It's Large Codding So I Need To Help To Another Way To Do
in my project, i have to add only vertical scrollbar to a panel,but not horizontal scrollbar. i need some example on it,how to doit.
in my project, i have to add only vertical scrollbar to a panel,but not horizontal scrollbar. i need some example on it,how to doit.  in my project.... i need some example on it,how to doit. Thanks in advance
need someone to do/help with code
need someone to do/help with code  i need someone to do a code for me. It is a restaurant menu that displays a list of 10 food items. A customer..., along with the number of items sold..please help did a code 500 times cant
i need help - Development process
i need help  hello, i need help regarding this program. public..., guide me and how my program can take ping for every 5Minutes and store the result in to the files,if i want to do this i must make my program running
i need help plz .... Quickly
i need help plz .... Quickly   how can i count how many numbers enterd by the user so the output would be like this Total number of Scores = .... this is my code :- (adsbygoogle = window.adsbygoogle || []).push

Ads