Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Need help with my project
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


philippines
Need help with my project
1 Answer(s)      2 years and 6 months ago
Posted in : Java Beginners


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.


Advertisement
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
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
Advertisements
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
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
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 Project
Need Project  How to develop School management project by using Struts Framework? Please give me suggestion and sample examples for my project
my project
my project   how to creat a e learnig site with student information , faculty details , student queries , student feedback, course information
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
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....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 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
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
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 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
help on project - JSP-Servlet
help on project  Need help on Java Project
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
my question is related to my project viz
my question is related to my project viz   Sir, my project is based... in Net Beans the whole project is working except a jsp page named dayplan.jsp.... plz help me
i need project
i need project  can u send online shoppin project 2 my mailid.   Please visit the following links: Struts2 Shopping cart JSP-Servlet Shopping cart
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 <
My name, java project
My name, java project  Write a class that displays your first name... displayLetterA() { code goes in here }, ...and my names hayley ... anyone? help
My name, java project
My name, java project  Write a class that displays your first name... displayLetterA() { code goes in here }, ...and my names hayley ... anyone? help
i want to find the byte code of a image file ... for my project..plz if anybody help me for java coding i will grateful..
i want to find the byte code of a image file ... for my project..plz if anybody help me for java coding i will grateful..   i want to convert a image file to its byte code format that help me for the pattern matching in my
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
I GOT ERROR FOR MY PROJECT
I GOT ERROR FOR MY PROJECT  Dear Friend/ RoseIndia Team, Firstly I would to thank u Guys help me so many time.. I got error on my little project when running at server. please correctly my error Guys... ERROR CODE: "LOGIN
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
hi .. need help ASAP ..i have a project buit in eclipse , i have installed jasper. i want the steps to work on it ..
hi .. need help ASAP ..i have a project buit in eclipse , i have installed jasper. i want the steps to work on it ..   hi .. need help ASAP ..i have a project buit in eclipse , i have installed jasper. i want the steps to work
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 have problem in my Project
I have problem in my Project  Dear Sir, i have problem in my project about Jtable i have EDIT JButton whenevery i was click on edit he is display all data from database but i want to select any row
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
Advertisements
 

 

 

DMCA.com