CAn anyone help me wit the?? scanner stdin?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shadachi
    New Member
    • May 2007
    • 15

    CAn anyone help me wit the?? scanner stdin?

    This is the question?.. can anyone help me?? kinda confused..using JcreatorLe

    A wholesale book dealer needs a program to write invoices for book orders that he takes for a customer over the phone. Each order usually consists of multiple copies of several book titles. The program should ask the user if there is an order to process. If the user responses yes, then the program should ask for the price of the first book in the order and the number of such books. The program should then display the cost of these books, including a 7.5% sales tax. Then the program should ask if there is another order. If there is one, the program should process it as just described. If there are no more orders, the program should display the total number of orders processed, the total number of books sold, and the total receipts.

    SAmple Output :

    Is there an order to process (Y/N)? y

    Enter the price for book 1: RM 20.00
    Enter the quantity for book 1: 3
    Cost for book 1: RM64.50

    Is there an order to process (Y/N)? y

    Enter the price for book 2: RM 50.00
    Enter the quantity for book 2: 2
    Cost for book 2: RM 107.50

    Is there an order to process (Y/N)? y

    Enter the price for book 3: RM 40.00
    Enter the quantity for book 3: 2
    Cost for book 3: RM 86.00

    Is there an order to process (Y/N)? y

    Enter the price for book 4: RM 10.00
    Enter the quantity for book 4: 3
    Cost for book 4: RM 32.25

    Is there an order to process (Y/N)? n

    Total number of orders processed: 4
    Total number of books sold: 10
    Total price: RM 290.25

    I kinda messed up alot..
    here is my codes..


    import java.util.*;
    import java.text.Decim alFormat;
    import java.lang.Doubl e;


    public class Book {



    public static void main(String args[]){

    int process;
    double price;
    int quantity;
    int quantity1=0;
    double sum=0.00;
    double sum1=0.00;
    double cost=0.00;
    String Result="";


    DecimalFormat twoDigits = new DecimalFormat(" 0.00");
    Scanner stdin= new Scanner(System. in);


    System.out.prin t("Is there an order to process? (Y?N)");

    Result= stdin.next();


    do

    {
    System.out.prin t("Enter the price for book %d: RM ");

    price = stdin.nextDoubl e();

    System.out.prin t("Enter the quantity for book %d: ");

    quantity = stdin.nextInt() ;


    quantity1 = quantity1 + quantity;

    cost = (double)((price * quantity) * 1.075);

    System.out.prin tln("Cost for book %d: %.2lf \n");

    sum1 = sum + cost;
    count++;

    System.out.prin t("Is there an order to process? (Y?N)");


    }while ((Result =='y'));

    System.out.prin tln("\nTotal number of orders processed:%d "+count);
    System.out.prin tln("\nTotal number of books sold:%d "+quantity1 );
    System.out.prin tln("\nTotal price:%.2lf \n"+sum1);





    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    You were very close but the following things let you down.
    1.) Choice of variable names
    2.) Compare strings using .equals method
    3.) println is different from printf
    I twicked those things here:

    [CODE=java]import java.util.*;
    import java.text.Decim alFormat;
    import java.lang.Doubl e;
    public class Book {
    public static void main(String args[]) {
    double totalPrice = 0.0;
    int totalQuantity = 0;
    final double tax = 1.075;
    int orders = 0;
    DecimalFormat twoDigits = new DecimalFormat(" 0.00");
    Scanner stdin= new Scanner(System. in);

    System.out.prin t("Is there an order to process? (Y?N) :");
    String response = stdin.next();

    do {
    System.out.prin t("Enter the price for book :");
    double price = stdin.nextDoubl e();
    System.out.prin t("Enter the quantity for book :");
    int quantity = stdin.nextInt() ;
    totalQuantity = totalQuantity + quantity;
    double cost = (price * quantity) * tax;
    totalPrice = totalPrice + cost;
    System.out.prin tln("Cost for book is " + cost);

    orders++;
    System.out.prin t("Is there an order to process? (Y?N) :");
    response = stdin.next();
    }
    while (response.equal sIgnoreCase("y" ));
    System.out.prin tln("\nTotal number of orders processed: " + totalQuantity);
    System.out.prin tln("\nTotal number of books sold: " + orders);
    System.out.prin tln("\nTotal price:" + totalPrice);
    }
    }[/CODE]

    Comment

    • shadachi
      New Member
      • May 2007
      • 15

      #3
      Thx dude.. it works.. btw i can't seem to have a start at this ..seems confusing..

      Write a program that asks the user to enter the number of a month of the year and the year. For example, if the user wants to enter June, 1992, he or she would enter 6 and then enter 1992. The program should then display the number of days in that month. Be sure to take leap years into account.

      Sample output:

      Enter month: 8
      Enter year: 2006

      The number of days in August, 2006 is 31

      Sample output2:

      Enter month: 2
      Enter year: 2000

      The number of days in February, 2000 is 29
      Sample output3:

      Enter month: 2
      Enter year: 2005

      The number of days in February, 2005 is 28
      I kinda made a mess again.. =\

      import java.util.Scann er;

      class Switch2
      {

      public static void main(String[]args)
      throws java.io.IOExcep tion
      {
      int choice;
      System.out.prin tln("January of year year 2006.....1");
      System.out.prin tln("February of year year 2006.....2");
      System.out.prin tln("March of year year 2006.....3");
      System.out.prin tln("April of year year 2006.....4");
      System.out.prin tln("May of year year 2006.....5");
      System.out.prin tln("June of year year 2006.....6");
      System.out.prin tln("July of year year 2006.....7");
      System.out.prin tln("August of year year 2006.....8");
      System.out.prin tln("September of year year 2006.....9");
      System.out.prin tln("October of year year 2006.....10");
      System.out.prin tln("November of year year 2006.....11");
      System.out.prin tln("Disember of year year 2006.....12");

      do
      {

      Scanner stdin = new Scanner(System. in);

      System.out.prin t("Type your choice[1-12] and press [Enter]: ");
      choice = stdin.nextInt() ;

      switch(choice)
      {
      case 0 : System.out.prin tln("INVALID") break;
      case 1 : System.out.prin tln("31"); break;
      case 2 : System.out.prin tln("28"); break;
      case 3 : System.out.prin tln("31"); break;
      case 4 : System.out.prin tln("30"); break;
      case 5 : System.out.prin tln("31"); break;
      case 6 : System.out.prin tln("30"); break;
      case 7 : System.out.prin tln("31"); break;
      case 8 : System.out.prin tln("31"); break;
      case 9 : System.out.prin tln("30"); break;
      case 10 : System.out.prin tln("31"); break;
      case 11 : System.out.prin tln("30"); break;
      case 12 : System.out.prin tln("31"); break;

      default: System.out.prin tln("Invalid Option");
      }

      } while(choice!=1 3);

      }
      }

      Comment

      • nomad
        Recognized Expert Contributor
        • Mar 2007
        • 664

        #4
        Originally posted by shadachi
        Thx dude.. it works.. btw i can't seem to have a start at this ..seems confusing..




        Sample output:




        Sample output2:



        Sample output3:



        I kinda made a mess again.. =\
        You have a small mistake here
        case 0 : System.out.prin tln("INVALID") break;
        make sure you place a ; in front of ("INVALID")

        anyway there are many ways you can get your output to look like this
        Enter month: 2
        Enter year: 2005
        The number of days in February, 2005 is 28
        I would make a array or an array list which would hold your months and days and then use an if statement using a scanner for the input .
        Someone might suggest using the Gregorian calendar

        nomad

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by nomad
          ....
          Someone might suggest using the Gregorian calendar

          nomad
          I suggest java.util.Grego rianCalendar.

          Comment

          • shadachi
            New Member
            • May 2007
            • 15

            #6
            Can someone giv me a sample ..i am kinda lost d ><

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by shadachi
              Can someone giv me a sample ..i am kinda lost d ><
              Just read the methods defined in the docs for it.
              Like Jos always says, refuse to write any code unless you have access to the Java docs.

              Comment

              • shadachi
                New Member
                • May 2007
                • 15

                #8
                GregorianCalend er is quite hard to understand now.. but i will later.. for now..i hope that someone can make a sample for me..using arrays and if statment.. i tried buy failed.. quite hard.. pls teach..

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by shadachi
                  GregorianCalend er is quite hard to understand now.. but i will later.. for now..i hope that someone can make a sample for me..using arrays and if statment.. i tried buy failed.. quite hard.. pls teach..
                  If you refuse to use the Gregorian Calendar I suggest you google for 'Zeller congruence".
                  It's simple formula that translates a year (including century), month and day to the
                  Julian day number. The fun part is that the difference of two Julian day numbers
                  equals the number of days between the two dates (converted to Julian day numbers).

                  All you need to implement is:

                  [code=java]
                  int ZC(int year, int month, int day) {
                  return ...
                  }[/code]

                  btw, that is the way the Gregorian Calendar does it as well.

                  kind regards,

                  Jos

                  Comment

                  • shadachi
                    New Member
                    • May 2007
                    • 15

                    #10
                    lolx... i think i'm stupid.. i couldn't understand anything.. haiz..

                    i tried this ..but i dono how to continue d..can help?

                    Code:
                    import java.util.*;
                    import java.lang.String;
                    
                    
                    public class Switch {
                    	
                    	
                        public static void main(String args[]) {
                           
                           
                           int theYear;
                           int theMonth;
                           int theDays;
                           
                            String Months[] =  {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                            "July", "Aug", "Sep", "Oct", "Nov", "Dec"};
                          
                                  
                            Scanner stdin= new Scanner(System.in);
                     
                            System.out.print("Please Enter the year : ");
                    
                    		theYear = stdin.nextInt();
                    	   
                              
                           
                            System.out.print("Please Enter the month :");
                           
                            theMonth = stdin.nextInt();
                                       
                            
                            
                          
                           
                           System.out.println("the Number of Days in " +theYear+"," +theMonth+ " is "+theDays);
                           
                            
                          
                         }
                    }

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by shadachi
                      lolx... i think i'm stupid.. i couldn't understand anything.. haiz..

                      i tried this ..but i dono how to continue d..can help?
                      Have you tried to compile what you wrote? Programming is not about guessing
                      what might be right more or less. Computers are stupid, i.e. you have to tell them
                      exactly what you have in mind.

                      kind regards,

                      Jos

                      Comment

                      • shadachi
                        New Member
                        • May 2007
                        • 15

                        #12
                        Originally posted by JosAH
                        Have you tried to compile what you wrote? Programming is not about guessing
                        what might be right more or less. Computers are stupid, i.e. you have to tell them
                        exactly what you have in mind.

                        kind regards,

                        Jos
                        Tried d.. but it does come out then u type the year and month it comes out but i dono how to continue from this part d..i need help in the arrays.. i din learn arrays yet .. i tried to learn thru google but it is not quite clear.. can assist abit? plz? ??

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by shadachi
                          Tried d.. but it does come out then u type the year and month it comes out but i dono how to continue from this part d..i need help in the arrays.. i din learn arrays yet .. i tried to learn thru google but it is not quite clear.. can assist abit? plz? ??
                          Do you have an algorithm for this? Write down your algorithm first. How would you do it manually on a piece of paper? After that transform those steps into a program, the compiler is not going to solve that part for you.

                          Comment

                          • shadachi
                            New Member
                            • May 2007
                            • 15

                            #14
                            algorithm... i dono bout that but i only got an idea... but i dono how to use java to implement it..

                            Prompt user to input month,
                            Prompt user to input Year,

                            use the scanner to read the inputs.

                            direct them to years,
                            using the if statement (year/4) leap
                            then to months,

                            then to days ,
                            if (leap year) feb would be 29
                            using arrays.


                            print out the results .year ,month,days,

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by shadachi
                              algorithm... i dono bout that but i only got an idea... but i dono how to use java to implement it..

                              Prompt user to input month,
                              Prompt user to input Year,

                              use the scanner to read the inputs.

                              direct them to years,
                              using the if statement (year/4) leap
                              then to months,

                              then to days ,
                              if (leap year) feb would be 29
                              using arrays.


                              print out the results .year ,month,days,
                              You haven't explained how you are going to get the other days except for February.
                              Are you planning on using lots of if-else statements like?

                              [CODE=java]if(month.equals ("Jan")) {
                              days = 31;
                              }
                              else ....[/CODE]
                              That would solve the problem but wouldn't be neat.
                              An obvious replacement for bunch of if-else is the switch ...

                              Comment

                              Working...