Results 1 to 18 of 18

Thread: Help w/ loop

  1. #1
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default Help w/ loop

    Hi i've been using dr. java for about a month now and we were introduced to loops some time last week. i'm not really too clear on it yet and i was just wondering for some help on this program we're supposed to do..

    it asks the user for a model car number and there are many model numbers that are defective (determined by if statements)
    i have the actual code working, but i would like to know how to keep asking the question over and over again? also, if the user inputs '0' it should be able to 'quit' the program.. how would i do that?
    and where would i place it in the code..? i'm confused on that part too.

    any help would be great..
    ask if you're unclear on anything :)

    -iWonder

  2. #2
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    Can you post the code you have so far?

  3. #3
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    
    public class CarRecall2 {
      public static void main (String[] args) {
        //declare vars
        int car;
        Scanner input = new Scanner(System.in);
        
        //Show 'enter the car's model'
        System.out.println("Enter the car's model number or press 0 to quit: ");
         
    
                  //Get value from user
        car = input.nextInt();
        //closes input
        input.close();
        
          
        
        if (car ==119){
          System.out.println("Your car is defective. It must be repaired.");
      } else if (car ==179){
          System.out.println("Your car is defective. It must be repaired");
      } else if (car ==189){
          System.out.println("Your car is defective. It must be repaired");
      } else if (car ==190){
          System.out.println("Your car is defective. It must be repaired");
      } else if (car ==191){
          System.out.println("Your car is defective. It must be repaired");
      } else if (car ==192){
          System.out.println("Your car is defective. It must be repaired");
      } else if (car ==193){
          System.out.println("Your car is defective. It must be repaired");
      } else if (car ==194){
          System.out.println("Your car is defective. It must be repaired");
      } else if (car ==195){
          System.out.println("Your car is defective. It must be repaired");
      } else if (car ==221){
          System.out.println("Your car is defective. It must be repaired");
      } else if (car ==780){
          System.out.println("Your car is defective. It must be repaired");
      } else {
          System.out.println("Your car is not defective.");
      }
        }
          
    }

  4. #4
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    First of all, you can seriously shorten the "your car is defective" part:

    | means "OR"

    Here is the final code for your thing. I changed the OR part and added a loop:

    Java Code:
    import java.util.Scanner;
    
    public class CarRecall2 {
      public static void main (String[] args) {
        //declare vars
        int car;
        Scanner input = new Scanner(System.in);
        
        while(true) {
        //Show 'enter the car's model'
        System.out.println("Enter the car's model number or press 0 to quit: ");
    
                  //Get value from user
        car = input.nextInt();
        //closes input
        input.close();
    
        if(car==0)
            break;
    
        if (car==119|car==179|car==189|car==190|car==191|car==192|car==193|car==194|car==195|car==221|car==780){
        System.out.println("Your car is defective. It must be repaired.");
       }
       else {
          System.out.println("Your car is not defective.");
       }
        }
    }

  5. #5
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    oh, thanks so much! :)
    i really appreciate it

  6. #6
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by iWonder View Post
    oh, thanks so much! :)
    i really appreciate it
    You're welcome :)

    Do you understand what I did (so you can do it in future projects)?

  7. #7
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    mmhmm
    i just needed a real example.our textbook only explains, and i was lost for a while -_-
    Actually i have another program, but i wont burden you with another one..because I havent even started yet

  8. #8
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    Hi i experienced an error when I ran this..
    I would show you a picture but i have to post at least 20 replies to post a picture..

    i35.tinypic.com/ipo26b.jpg

  9. #9
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    Scanner closed error? I can't scroll up to see the code but I didn't know it was possible to close the scanner. Is this error located in a different class? You have to send the scanner as a parameter or just re-define if so.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    It's not only possible, it's recommended to close it when you're done using it. Else you end up wasting resources. The OP is probably best off here posting the latest incarnation of his code.

  11. #11
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    
    public class CarRecall2 {
      public static void main (String[] args) {
        //declare vars
        int car;
        Scanner input = new Scanner(System.in);
        
        while(true) {
        //Show 'enter the car's model'
        System.out.println("Enter the car's model number or press 0 to quit: ");
    
                  //Get value from user
        car = input.nextInt();
        //closes input
        input.close();
    
        if(car==0)
            break;
    
        if (car==119|car==179|car==189|car==190|car==191|car==192|car==193|car==194|car==195|car==221|car==780){
        System.out.println("Your car is defective. It must be repaired.");
       }
       else {
          System.out.println("Your car is not defective.");
       }
        }
      }
    }
    let me know how to fix this please

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    let me know how to fix this please
    Please post all error messages and indicate with a comment the line that trips the errors.

    nevermind, I see. You create a Scanner object, then you have a loop. within this loop you close the scanner object and thus it is not usable any more. So every time you loop through this while loop after the first time around the Scanner is no longer available and an error will be thrown.

    Don't do this. Call close on the Scanner object only after you have exited the while loop.
    Last edited by Fubarable; 11-19-2008 at 09:53 PM.

  13. #13
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    so i shouldnt close the scanner until after the loop?

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Better than asking this this question: why not try it out yourself first? You won't blow up your computer, trust me.

  15. #15
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    i would try it
    but im in the library xD

    and i only do java at school..
    how do i get these programs (for free), and what's the best in your opinion?

  16. #16
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    what programs are you talking about? If your talking about compiling your source code you can carry it around on a flash drive and execute it through the command prompt(though if i remember my library it will be blocked because "naughty kids are hacking") but that doesnt stop you from creating a text file with 1 line that says command and saving it as a .bat file and running it.

    What do you use at school to write code? try using that it's probably free

  17. #17
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    i use Dr.Java
    but i tried installing it and it didnt work for some reason :S

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    in the library? You really need to go where you can do some coding and compiling if you're really going to get the max out of this forum.

    As for compilers, you can run Java right from the command line and enter code with your favorite editor. I like Notepad++.

Similar Threads

  1. Need help with While Loop
    By mrdestroy in forum New To Java
    Replies: 14
    Last Post: 10-20-2008, 03:29 PM
  2. For Loop
    By kian_hong2000 in forum New To Java
    Replies: 1
    Last Post: 08-07-2008, 03:01 PM
  3. do...while loop
    By eva in forum New To Java
    Replies: 16
    Last Post: 01-31-2008, 07:44 AM
  4. while loop
    By michcio in forum New To Java
    Replies: 5
    Last Post: 01-27-2008, 01:56 AM
  5. While loop
    By leebee in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 04:11 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
  •