By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,199 Members | 1,551 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,199 IT Pros & Developers. It's quick & easy.

CAn anyone help me wit the?? scanner stdin?

P: 15
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.DecimalFormat;
import java.lang.Double;


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.print("Is there an order to process? (Y?N)");

Result= stdin.next();


do

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

price = stdin.nextDouble();

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

quantity = stdin.nextInt();


quantity1 = quantity1 + quantity;

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

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

sum1 = sum + cost;
count++;

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


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

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





}
}
Jul 3 '07 #1
Share this Question
Share on Google+
29 Replies


10K+
P: 13,264
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:

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.text.DecimalFormat;
  3. import java.lang.Double;
  4. public class Book {
  5.     public static void main(String args[]) {
  6.         double totalPrice = 0.0;
  7.         int totalQuantity = 0;
  8.         final double tax = 1.075;
  9.         int orders = 0;
  10.         DecimalFormat twoDigits = new DecimalFormat("0.00");
  11.         Scanner stdin= new Scanner(System.in);
  12.  
  13.         System.out.print("Is there an order to process? (Y?N) :");
  14.         String response = stdin.next();
  15.  
  16.         do {
  17.             System.out.print("Enter the price for book :");
  18.             double price = stdin.nextDouble();
  19.             System.out.print("Enter the quantity for book :");
  20.             int quantity = stdin.nextInt();
  21.             totalQuantity = totalQuantity + quantity;
  22.             double cost = (price * quantity) * tax;
  23.             totalPrice = totalPrice + cost;
  24.             System.out.println("Cost for book is " + cost);
  25.  
  26.             orders++;
  27.             System.out.print("Is there an order to process? (Y?N) :");
  28.             response = stdin.next();
  29.         }
  30.         while (response.equalsIgnoreCase("y"));
  31.         System.out.println("\nTotal number of orders processed: " + totalQuantity);
  32.         System.out.println("\nTotal number of books sold: " + orders);
  33.         System.out.println("\nTotal price:" + totalPrice);
  34.      }
  35. }
Jul 3 '07 #2

P: 15
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.Scanner;

class Switch2
{

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

do
{

Scanner stdin = new Scanner(System.in);

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

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

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

} while(choice!=13);

}
}
Jul 3 '07 #3

nomad
Expert 100+
P: 664
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.println("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
Jul 3 '07 #4

10K+
P: 13,264
....
Someone might suggest using the Gregorian calendar

nomad
I suggest java.util.GregorianCalendar.
Jul 3 '07 #5

P: 15
Can someone giv me a sample ..i am kinda lost d ><
Jul 3 '07 #6

10K+
P: 13,264
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.
Jul 3 '07 #7

P: 15
GregorianCalender 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..
Jul 3 '07 #8

Expert 10K+
P: 11,448
GregorianCalender 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:

Expand|Select|Wrap|Line Numbers
  1. int ZC(int year, int month, int day) {
  2.    return ...
  3. }
btw, that is the way the Gregorian Calendar does it as well.

kind regards,

Jos
Jul 3 '07 #9

P: 15
lolx... i think i'm stupid.. i couldn't understand anything.. haiz..

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

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.lang.String;
  3.  
  4.  
  5. public class Switch {
  6.  
  7.  
  8.     public static void main(String args[]) {
  9.  
  10.  
  11.        int theYear;
  12.        int theMonth;
  13.        int theDays;
  14.  
  15.         String Months[] =  {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 
  16.         "July", "Aug", "Sep", "Oct", "Nov", "Dec"};
  17.  
  18.  
  19.         Scanner stdin= new Scanner(System.in);
  20.  
  21.         System.out.print("Please Enter the year : ");
  22.  
  23.         theYear = stdin.nextInt();
  24.  
  25.  
  26.  
  27.         System.out.print("Please Enter the month :");
  28.  
  29.         theMonth = stdin.nextInt();
  30.  
  31.  
  32.  
  33.  
  34.  
  35.        System.out.println("the Number of Days in " +theYear+"," +theMonth+ " is "+theDays);
  36.  
  37.  
  38.  
  39.      }
  40. }
  41.  
Jul 3 '07 #10

Expert 10K+
P: 11,448
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
Jul 3 '07 #11

P: 15
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? ??
Jul 3 '07 #12

10K+
P: 13,264
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.
Jul 3 '07 #13

P: 15
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,
Jul 3 '07 #14

10K+
P: 13,264
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?

Expand|Select|Wrap|Line Numbers
  1. if(month.equals("Jan")) {
  2.    days = 31;
  3. }
  4. else ....
That would solve the problem but wouldn't be neat.
An obvious replacement for bunch of if-else is the switch ...
Jul 3 '07 #15

P: 15
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?

Expand|Select|Wrap|Line Numbers
  1. if(month.equals("Jan")) {
  2.    days = 31;
  3. }
  4. else ....
That would solve the problem but wouldn't be neat.
An obvious replacement for bunch of if-else is the switch ...
using the switch... i see .. but i dono how to merge them all .. from the 1st example i posted ..using the switch and the 2nd using the arrays.. how to merge them ...
Jul 3 '07 #16

10K+
P: 13,264
Also since the input is given as an integer, I don't see why you need the array of months as Strings.
Jul 3 '07 #17

P: 15
So ..the better solution is by using the switch??

but nomad suggested 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
Jul 3 '07 #18

10K+
P: 13,264
using the switch... i see .. but i dono how to merge them all .. from the 1st example i posted ..using the switch and the 2nd using the arrays.. how to merge them ...
It's not a matter of having to merge them. It's coming up with a simple algorithm. Something like when months entered is in (1, 3, 5, 7, 8,10, 12) then days in month is 31. A switch is well suited for this.
Jul 3 '07 #19

10K+
P: 13,264
So ..the better solution is by using the switch??

but nomad suggested 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
There are lots of solutions as you have already heard in this thread. Write them all if you have time and compare them yourself.
Jul 3 '07 #20

P: 15
It's not a matter of having to merge them. It's coming up with a simple algorithm. Something like when months entered is in (1, 3, 5, 7, 8,10, 12) then days in month is 31. A switch is well suited for this.
ERm ..i get the idea d.. but the thing is i don't know much from switch.. i learned not much..can try and put an example..so i can learn? .sorry to bother u so much ><
Jul 3 '07 #21

10K+
P: 13,264
ERm ..i get the idea d.. but the thing is i don't know much from switch.. i learned not much..can try and put an example..so i can learn? .sorry to bother u so much ><
I'm afraid if I give you the link to the tutorial for it, I'll have spoilt it for you and given you the answer ....

Don't have some text book that describes it? Even in c+, the concept/syntax is the same?
Jul 3 '07 #22

10K+
P: 13,264
I'm afraid if I give you the link to the tutorial for it, I'll have spoilt it for you and given you the answer ....

Don't have some text book that describes it? Even in c+, the concept/syntax is the same?
Here's one that looks safe enough.
Jul 3 '07 #23

P: 15
Here's one that looks safe enough.

in the switch statement rite?

i put
Expand|Select|Wrap|Line Numbers
  1. switch(month){
  2.  
  3. case1:
  4. case3;
  5. case5;
  6. numdays=31;
  7. break;
  8.  
  9. default:
  10.  
  11. System.out.println("Invalid month.");
  12.                 break;
  13. }
  14.  
  15. System.out.println("The Number of Days in " +month+"," +year+ " is "+numDays);
  16.  
the output i got. is the number of days in 1,2006 is 31.

how to make the month instead of numbers become words like if u input 1 becomes january. 2 =february
Jul 3 '07 #24

blazedaces
100+
P: 284
in the switch statement rite?

i put
Expand|Select|Wrap|Line Numbers
  1. switch(month){
  2.  
  3. case1:
  4. case3;
  5. case5;
  6. numdays=31;
  7. break;
  8.  
  9. default:
  10.  
  11. System.out.println("Invalid month.");
  12.                 break;
  13. }
  14.  
  15. System.out.println("The Number of Days in " +month+"," +year+ " is "+numDays);
  16.  
the output i got. is the number of days in 1,2006 is 31.

how to make the month instead of numbers become words like if u input 1 becomes january. 2 =february
Why don't you store months in an array or arraylist? Then you would do months[index] and it would call the correct month?
Jul 3 '07 #25

P: 15
Why don't you store months in an array or arraylist? Then you would do months[index] and it would call the correct month?

how do u make them into arrays.. coz i am using switch statements.. can u giv an example?
Jul 4 '07 #26

10K+
P: 13,264
how do u make them into arrays.. coz i am using switch statements.. can u giv an example?
If you have declared an array say

Expand|Select|Wrap|Line Numbers
  1. String[] months = {"January", "February", ...};
Then if the user inputs 1 and you store it into an int variable called month, then you can do
Expand|Select|Wrap|Line Numbers
  1. System.out.println("Month entered is" + months[month -1 ]);
Do you understand that part?
Jul 4 '07 #27

P: 15
not quite but the declaring part i do.. the 2nd part not so clear.. coz i din learn anything bout arrays yet
Jul 4 '07 #28

10K+
P: 13,264
not quite but the declaring part i do.. the 2nd part not so clear.. coz i din learn anything bout arrays yet
Well then you'll just have to learn them. Your initial problem description did not require you to express the month entered as a string so the switch stll solves the problem. If you want to show the name of the month entered then either use a dirty chunk of if-else statements or use the array.
Jul 4 '07 #29

Expert 10K+
P: 11,448
Well then you'll just have to learn them. Your initial problem description did not require you to express the month entered as a string so the switch stll solves the problem. If you want to show the name of the month entered then either use a dirty chunk of if-else statements or use the array.
... or a GregorianCalendar object ...

kind regards,

Jos
Jul 4 '07 #30

Post your reply

Sign in to post your reply or Sign up for a free account.