-5

Use a Loop to print out each monthly rental from the array that falls below the user entered threshold

This was a question I received from my lecturer, I require help because I simply have no idea where to even start. Here is my complete code for the Program, it is the last case.

import java.util.Scanner ;
public class jakeGrim {

  public static void  main(String[] args) {
                // Local variable

                int option;
                String squareFootage="";

                int noBed = 0;

                double totalSum =0;

                String propertyCode="";

                String propertyType="";

                String threshold="";


                Scanner input = new Scanner( System.in );
                Scanner user_input = new Scanner( System.in );
                double[] array = new double[12]; 

  do{
            // Display menu graphics
            System.out.println(" ");
            System.out.println("|  *****Rental Menu*******   |");
            System.out.println("|        1. Enter rental property Details     ");
            System.out.println("|        2. Enter monthly rent ( 12 Months )       ");
            System.out.println("|        3. Display Annual Rent");
            System.out.println("|        4. Display rental report       ");
            System.out.println("|        5. Display Monthly rents falling below a certain threshold       ");
            System.out.println(" ");
            System.out.println(" Please Select an option: ");
            option = input.nextInt();


  {

    switch (option) {


   case 1:


            System.out.println("Enter Rental Details: ");
            System.out.println("Property Code:            ");
            propertyCode = user_input.next();
            System.out.println("Property Type:            ");
            propertyType = user_input.next();
            System.out.println("Square Footage:           ");
            squareFootage = user_input.next();
            System.out.println("Number Of bedrooms        ");
            noBed = input.nextInt();
            break;


      case 2:

     {
         Scanner keyboardScanner = new Scanner(System.in); 

         for (int i = 0; i < 12; i++) {
          System.out.println("Enter Rental for month[" +( i +1)+ "]");
          array[i] = keyboardScanner.nextDouble();
}




        //So now, we need to do something with that array and sum up all the values in that array. 
        for (int i = 0; i < array.length; i++){
          System.out.println(array[i]);

         totalSum += array[i];
}
}




 break;


     case 3:
         System.out.println("The annual rent for propery code "+propertyCode+" is:  " +(totalSum));


break;

    case 4:

        System.out.println(" Property Code:      "+propertyCode);
        System.out.println(" Property Type:      "+propertyType);
        System.out.println(" Square Footage:     "+squareFootage);
        System.out.println(" Number of Bedrooms: "+noBed);
        System.out.println("");
        System.out.println("");
        for(int i = 0; i<12; i++)
            System.out.println("Rental for month " + (i+1) + " : " + array[i]);


    case 5:

         Scanner user_input = new Scanner( System.in );

        System.out.println("Enter the Rental Threshold: ");
        threshold = user_input.next();

        System.out.println("


    break;
    default:
        System.out.println("Invalid selection");
      break; 
     }
  }
}while (option!=0);
}
}
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
Jake Grim
  • 41
  • 7
  • Explain your question clearly... – Devavrata Jun 29 '14 at 21:11
  • If you "simply have no idea where to even start", then your question is not appropriate for Stack Overflow. – Blorgbeard Jun 29 '14 at 21:13
  • 1
    @BlorgBeard Is this not a Website to help those that need it when programming? New to coding or not? – Jake Grim Jun 29 '14 at 21:15
  • @Devavrat The question Is Up at the top In bold, I'm required to do that And not sure where to start. – Jake Grim Jun 29 '14 at 21:15
  • 2
    @JakeGrim You should try to better explain why your program is not working at the moment (what output do you expect? what do you get?) and [narrow down the code to the section that does not work as expected instead of posting 150 lines of code](http://stackoverflow.com/help/mcve). – assylias Jun 29 '14 at 21:16
  • 2
    No Jake, it's not. It's a website for "professional and enthusiast programmers" to ask and answer *specific* programming-related questions. Please look at the [tour](http://stackoverflow.com/tour). Specifially, "Don't ask about... Questions you haven't tried to find an answer for (show your work!)" – Blorgbeard Jun 29 '14 at 21:17
  • That is, people who are new to coding can ask questions, but they shouldn't expect free tutoring. You need to have a specific question, not "Please do my homework". – Blorgbeard Jun 29 '14 at 21:19
  • @assylias My apologies, I was given out to last time I asked a question due to the fact that they were unaware that the code was in a switch statement and I needed to " post all of the code " So I thought to skip the middle man this time and Just post all, It is the last Case I require help with, Case 5. – Jake Grim Jun 29 '14 at 21:20
  • @JakeGrim this clearly shows lack in efforts. – Devavrata Jun 29 '14 at 21:20
  • @Blorgbeard I'm sorry! I genuinely thought this was a website set up to help people who were starting out programming, I'm just stumped and Can't seem to find anything that can help me, I've looked everywhere, I didnt ask for you to do it FOR me, I asked for ANY help. – Jake Grim Jun 29 '14 at 21:21
  • @JakeGrim OK, here's a hint. You've used a loop in case 4 to print out every value. You need to do the same thing again, but put an `if` inside the loop to check if each value is below the threshold before you print it. – Blorgbeard Jun 29 '14 at 21:30
  • @JakeGrim Just a tip -- If you have looked everywhere and made efforts to solve your problem, **show it**. By just saying "Here's my problem, here's my code, I'm stuck, help me" you're not likely to receive a warm welcome at SO. As pointed out before, questions are generally expected to be *specific*, and a general "help me" question is generally considered too broad. In addition, by not showing any research effort in the body of your question you give the impression that you're expecting readers to solve your problem for you, which people tend not to like. (continued) – awksp Jun 29 '14 at 23:10
  • @JakeGrim By showing *what* you tried and by explaining *exactly* how it didn't work, as well as steps you took to try to figure out what was going wrong, you show that you did *your* job of giving a real effort to solve your problem. In addition, by taking those additional steps, if you haven't solved your problem already you'll usually have narrowed down your problem enough that either a Google search would reveal the answer or readers wouldn't mind helping you with your *specific* problem. (continued). – awksp Jun 29 '14 at 23:12
  • @JakeGrim This is a website set up to help *programmers*. There's no bias against newbies, nor favoritism towards veterans -- just preference as to the *quality* of questions. Imagine you're trying to get a professor or TA to help you -- you don't want them to keep on bothering you for additional information, and you *definitely* don't want to give the impression that you want them to do your thinking for you. Think of Stack Overflow as a last resort -- only ask a question after you've exhausted *everything*, and when you do ask that question, make sure you include *everything* you have done. – awksp Jun 29 '14 at 23:14

1 Answers1

0

Well,your program has several flaws which should be improved for better performance.

  1. There is no need of braces before switch.So,delete the braces{ before the switch statement!

  2. Again,there is no need of braces { in case 2 and } before break statement of case-2. You better leave it as it is.

  3. Add these lines of code in your case 5: after deleting previous data into that.

    case 5:
       //  Scanner user_input = new Scanner( System.in );
    System.out.println("Enter the Rental Threshold: ");
    threshold = user_input.next();
    for(int i=0;i<array.length;i++){
    if(Integer.valueOf(threshold)>array[i])
    System.out.println("Month "+(i+1)+" has the rent falling below the threshold range as the rent is "+array[i]);
    }
    System.out.println("");
    break;
    
  4. Lastly,you simply need to edit do-while condition,set it as do{...}while(option>=1 && option<=5);

  5. Also, as a matter of fact,try to indent your code properly to give it a better feel! It will make others go through your code and sincerely help you!

Correct working code :-

       import java.util.Scanner;
       public class jakeGrim {
       public static void main(String[] args) {
       int option;
       String squareFootage="";
       int noBed = 0;
       double totalSum =0;
       String propertyCode="";
       String propertyType="";
       String threshold="";
       Scanner input = new Scanner( System.in );
       Scanner user_input = new Scanner( System.in );
       double[] array = new double[12]; 
       do{
        // Display menu graphics
        System.out.println(" ");
        System.out.println("|  *****Rental Menu*******   |");
        System.out.println("|        1. Enter rental property Details     ");
        System.out.println("|        2. Enter monthly rent ( 12 Months )       ");
        System.out.println("|        3. Display Annual Rent");
        System.out.println("|        4. Display rental report       ");
        System.out.println("|        5. Display Monthly rents falling below a certain threshold       ");
        System.out.println(" ");
        System.out.println(" Please Select an option: ");
        option = input.nextInt();

          switch (option){
        case 1:
        System.out.println("Enter Rental Details: ");
        System.out.println("Property Code:            ");
        propertyCode = user_input.next();
        System.out.println("Property Type:            ");
        propertyType = user_input.next();
        System.out.println("Square Footage:           ");
        squareFootage = user_input.next();
        System.out.println("Number Of bedrooms        ");
        noBed = input.nextInt();
        break;

        case 2:

        Scanner keyboardScanner = new Scanner(System.in); 
        for (int i = 0; i < 12; i++) {
        System.out.println("Enter Rental for month[" +( i +1)+ "]");
        array[i] = keyboardScanner.nextDouble();
         }
       //So now, we need to do something with that array and sum up all the values in that array. 
        for (int i = 0; i < array.length; i++){
         System.out.println(array[i]);
        totalSum += array[i];
        }
        break;

        case 3:
        System.out.println("The annual rent for propery code "+propertyCode+" is:  " +totalSum);
        break;

        case 4:
        System.out.println(" Property Code:      "+propertyCode);
        System.out.println(" Property Type:      "+propertyType);
        System.out.println(" Square Footage:     "+squareFootage);
        System.out.println(" Number of Bedrooms: "+noBed);
        System.out.println("");
        System.out.println("");
        for(int i = 0; i<12; i++)
        System.out.println("Rental for month " + (i+1) + " : " + array[i]);
        break;

        case 5:
        //    Scanner user_input = new Scanner( System.in );
        System.out.println("Enter the Rental Threshold: ");
        threshold = user_input.next();
        for(int i=0;i<array.length;i++){
        if(Integer.valueOf(threshold)>array[i])
        System.out.println("Month "+(i+1)+" has the rent falling below the threshold range as the rent is "+array[i]);
        }
        System.out.println("");
        break;

        default:
        System.out.println("Invalid selection");
        break; 

       }
       } while (option>=1 && option<=5);
     }
    }
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • @JakeGrim-Now as I have given you the answer,please upvote the answer and kindly accept it too after verifying its correctness,else no help from next time! – Am_I_Helpful Jun 29 '14 at 22:17
  • 2
    "Now as I have given you the answer" --> Now really, do you think that's acceptable in an academic environment? Even more, do you think you're doing OP any favors by fixing his code *for* him? In addition, you could do a much better job of *explaining* your changes, instead of saying "Do this, this, and this". It's hard to learn when you're just told to make changes without being told *why*. But seriously. **Don't do anyone's work for them**. It's not helping them, and it gives the wrong idea to people about what Stack Overflow can be used for. – awksp Jun 29 '14 at 23:06
  • @user3580294-Sorry and THANKS for reminding.This was the last answer which I gave because OP was really concerned with the question.See also his comments and his question also involves code.But,no genuine help from the next time! – Am_I_Helpful Jun 30 '14 at 03:29
  • "Really concerned with the question" is **not** a reason to do someone's work for them. It's their responsibility to do their work, regardless of how concerned they are. Yes, I read his comments, and yes, his question has code. It does not mean you should provide code for him to copy/paste. It's a nice intention you have, though. – awksp Jun 30 '14 at 03:32
  • Fine @user3580294,I'll take care of these things from the next time! BTW,this was hardly second or third help given by me to people falling in this category.But,from the next time,I'll take care of reputation of SO Professionals.THANKS. – Am_I_Helpful Jun 30 '14 at 03:37
  • Not sure if I detect sarcasm... But in any case, helping is OK. Writing copy/paste-able code shouldn't be for homework questions. – awksp Jun 30 '14 at 03:38
  • @user3580294-This was really no sarcasm from my side.Whatever I replied it was internally from my heart.Again,you are mis-understanding me here! – Am_I_Helpful Jun 30 '14 at 03:41
  • Also, This doesnt work, I'm getting issues like it calling the months, Month 81 and month 41. – Jake Grim Jun 30 '14 at 11:15
  • What is month 81 and month 41?What are you talking about,can you be more specific and explain properly??? – Am_I_Helpful Jun 30 '14 at 11:16
  • Sorry, Just woke up and In need of coffee, It starts at month 01 correctly as I would like, But all months after become 21 - 31 - 41 -51 and so on. the Months are being named weirdly, Basically going up In intervals of 10 – Jake Grim Jun 30 '14 at 11:27
  • @JakeGrim-Why do you need month 21,31,41,51??? – Am_I_Helpful Jun 30 '14 at 11:29
  • I dont. It Is naming the months Under the threshold that when they should be month 2,3,4,5 ? – Jake Grim Jun 30 '14 at 11:30
  • Now,it won't.Check it again.It was because in `System.out.println("Month "+(i+1)+" has the rent falling below the threshold range as the rent is "+array[i]); ` earlier I left `()` near i+1. – Am_I_Helpful Jun 30 '14 at 11:34
  • @JakeGrim-Also,please upvote and accept the answer if satisifed!PLEASE,if output is correct! – Am_I_Helpful Jun 30 '14 at 11:34