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

Thread: Not sure how to go about this...

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    6
    My Mood
    Amused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Not sure how to go about this...

    So I'm writing a code to ask the user how many weeks they go till they get paid, how many hours they worked each day, how much their pay rate is, and then compile it all. Only problem is that I can't get the numbers to add up right. I'm sure I haven't written the code properly since I'm new to this and self taught but maybe someone can help? When the program runs a second time (if they work 2 weeks) it over wrights the data stored in the variables.

     
     
     
    	import java.util.Scanner;
     
     
    public class paycheck1 {
     
     
    		public static void main(String[] args) {
    			double sunday = 0;
    			double monday = 0;
    			double tuesday = 0;
    			double wednesday = 0;
    			double thursday = 0;
    			double friday = 0;
    			double saturday = 0;
     
    			System.out.println("How many weeks per paycheck?");
    		Scanner week = new Scanner (System.in);
    		int weekc = week.nextInt();
    		int weekv = weekc;
    		weekv++;
    		int count = 1;
    		while (count != weekv)
    		 {
    			// check System.out.println ("count: " + count + "weekv: " + weekv);
    			System.out.println ("How many hours did you work Sunday of week " + count + " ?");
    			Scanner scan = new Scanner (System.in);
    			sunday = scan.nextDouble();
     
    			System.out.println ("How many hours did you work Monday of week " + count + " ?");
    			Scanner scan1 = new Scanner (System.in);
    			monday = scan1.nextDouble();
     
    			System.out.println ("How many hours did you work Tuesday of week " + count + " ?");
    			Scanner scan2 = new Scanner (System.in);
    			tuesday = scan2.nextDouble();
     
    			System.out.println ("How many hours did you work Wednesday of week " + count + " ?");
    			Scanner scan3 = new Scanner (System.in);
    			wednesday = scan3.nextDouble();
     
    			System.out.println ("How many hours did you work Thursday of week " + count + " ?");
    			Scanner scan4 = new Scanner (System.in);
    			thursday = scan4.nextDouble();
     
    			System.out.println ("How many hours did you work Friday of week " + count + " ?");
    			Scanner scan5 = new Scanner (System.in);
    			friday = scan5.nextDouble();
     
    			System.out.println ("How many hours did you work Saturday of week " + count + " ?");
    			Scanner scan6 = new Scanner (System.in);
    			saturday = scan6.nextDouble();
     
     
    			count ++;
     
     
     
    		 }
     
     
    			double total = sunday + monday + tuesday + wednesday + thursday + friday + saturday;
    			System.out.println ("In total, you worked " + total + " hours." );
     
    			System.out.println ("What is your hourly pay?");
    			Scanner scan7 = new Scanner (System.in);
    			double pay = scan7.nextDouble();
     
    			double paytotal = pay*total;
     
    			System.out.println ("Your paycheck for this period of " + weekc + " weeks will be $" + paytotal +".");
     
     
     
     
     
     
    		}
     
    		}

  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Not sure how to go about this...

    A very simple solution seems to be if you keep a sum.
    sunday += scan.nextDouble();

    In fact you could keep only a single variable total and instead of the days of the week use
    total += scan.nextDouble();

  3. The Following User Says Thank You to ilan For This Useful Post:

    happyxcrab (October 20th, 2011)

  4. #3
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not sure how to go about this...

    How do I print out the Java code in the box like the OP has?
    Last edited by djl1990; October 6th, 2011 at 03:19 PM.

  5. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Not sure how to go about this...

    [highlight=java] Code goes here [/highlight]

  6. #5
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not sure how to go about this...

    Quote Originally Posted by javapenguin View Post
    [highlight=java] Code goes here [/highlight]
    Thanks for that

  7. #6
    Junior Member
    Join Date
    May 2011
    Posts
    6
    My Mood
    Amused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Not sure how to go about this...

    thanks so much ilan :3
    I'll try it out asap

  8. #7
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Not sure how to go about this...

    You don't have to create a new Scanner object every time, just reuse one.

    Here's an example
    Scanner in = new Scanner(System.in);
     
    System.out.println("Say something");
    in.readLine();
    System.out.println("Say another thing");
    in.readLine();
    System.out.println("Say a number");
    in.readInt();

Tags for this Thread