Results 1 to 8 of 8
  1. #1
    DSutta22 is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default Help with Program . . .

    First Off . . . Im not looking for anyone to write the code for me. That being said I need some pointers on what I'm doing wrong with my program.

    Program Description: Should have a "while" loop that asks repeatedly for a number until a number divisible by 3 or 7 is given. When that occurs, your program should display the number of numbers read.

    Here is what I have thus far :

    import java.util.Scanner;
    public class DivisibleBy3or7
    {
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    System.out.println("This program reads numbers until one is divisible by 3 or 7");
    System.out.println("Write a Number: ");
    int Number = input.nextInt();
    int i = 1;
    int sum = 0;
    while (( Number % 3 != 0 ) && ( Number % 7 != 0 ))
    {
    sum = sum + 1
    System.out.println("Write a Number; ");
    i++;
    }
    System.out.println("There were " + sum + " numbers read ");
    }
    }
    There is something misplaced or left out. . . Im new to programming and haven't figured out to get what's in my head translated to the computer.

    Thanks,

    Dan

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,562
    Rep Power
    23

    Default

    What does your program do? Does it work? Does it have errors?
    "while" loop that asks repeatedly
    I don't see any asking being done in the while loop.

  3. #3
    DSutta22 is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default

    I'm sorry. . It doesn't work. . .it just prints the first two lines. . then when I enter a number it does a continuous print of "Write a Number".

    Im sure my error is with my while loop. Im not to sure how to get it to ask "Write a Number" one time at a time.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,562
    Rep Power
    23

    Default

    I don't see any asking being done in the while loop.
    Your not asking inside the loop. How will Number ever change?

  5. #5
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    Java Code:
    import java.util.Scanner;
    public class Divisibleby3or7
    {
    	int number=0;
    	public Divisibleby3or7(){
    		Scanner input = new Scanner(System.in);
    		int sum = 0;
    		do
    		{
    			sum = sum + 1;	
    			System.out.println("This program reads numbers until one is divisible by 3 or 7");
    			System.out.println("Write a Number: ");
    			number = input.nextInt();
    			
    		}while(( number % 3 != 0 )||( number % 7 != 0 ));
    		
    		System.out.println("There were " + sum + " numbers read ");
    	}
    	public static void main(String[] args)
    	{
    		new Divisibleby3or7();
    	}
    }

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by DSutta22 View Post
    First Off . . . Im not looking for anyone to write the code for me.
    You are much clever pal. Really nice to see your attempt here that make to learn Java. Keep going.

    Regrading your code, first thing is you misses a semicolon at the end of following line, within the while loop.

    Java Code:
    sum = sum + 1
    Now look at the code from the beginning. Using a scanner object you collect an int number, say 4. Then you entered to the while loop and do the validation. According to your validation while loop return true. So you can entered to the while loop. There you have three lines.

    Java Code:
    [LIST=1][*]while (( Number % 3 != 0 ) && ( Number % 7 != 0 )) {[*]sum = sum + 1;[*]System.out.println("Write a Number; ");[*]i++;[*]}[/LIST]
    Second line of the while loop, increment the value of sum by 1. Third line just display a line of text to the console. On line four you do another increment as well.

    So is there any process taken to exit from the while loop? NO, so you have endless loop. Until you exit the application, it prints a line of text to the console.

    What you have to do is, use a do-while loop. In do-while loop before the condition is check it guaranteed that body of the loop execute at least once.

    But the following line is never executed.

    Java Code:
    }while(( number % 3 != 0 )||( number % 7 != 0 ));
    Think about that, what exactly happen.

  7. #7
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    oh shit i made a little mistake with condition in while,it was too late at night,that i wrote the code,lol,but i think he understood the idea with do-while loop

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by serjant View Post
    oh shit i made a little mistake with condition in while,it was too late at night,that i wrote the code,lol,but i think he understood the idea with do-while loop
    Yes pal, I know that you are not tested this code and write on the forum directly. Don't worry, I hope our thread starter find what happen there. Actually it's really nice point to think for him. :)

Similar Threads

  1. Help with program
    By bri1547 in forum New To Java
    Replies: 16
    Last Post: 07-27-2008, 05:26 AM
  2. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 08:24 AM
  3. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  4. cannot run the program
    By amiey in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 04:13 AM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •