Edit: I redo my coding. It runs fine now but some of my outputs are not printingCannot make a static reference to the non-static methodScanner cannot be resolved to a typeJava Inventory Program problemsWhy does this code using random strings print “hello world”?Method override returns nullGet total cost from arraylistHow to implement a double type methodHow can I sum the product of two two-dimensional arrays?I'm trying to use a method within my class when using a methodhow to run array using for loop and do while loopWould it make any difference giving arguments using scanner class instead of command line arguments?
Why does my String turn into Integers instead of letters after I add characters with +?
Are randomly-generated passwords starting with "a" less secure?
Should I intentionally omit previous work experience when applying for jobs?
What are the bumps on the Vega rocket
Graduate student with abysmal English writing skills, how to help
CentOS 7 -> find: missing Argument for "-exec"
How would my creatures handle groups without a strong concept of numbers?
What species of wasp is this? And how to get rid of them?
Machine learning and operations research projects
Get ids only where one id is null and other isn't
What's the maximum time an interrupt service routine can take to execute on atmega328p?
Parse source code of the RAPID robot-automation language
Is there any word for "disobedience to God"?
How can I effectively communicate to recruiters that a phone call is not possible?
How to know whether a Tamron lens is compatible with Canon EOS 60D?
Can fluent English speakers distinguish “steel”, “still” and “steal”?
How is angular momentum conserved for the orbiting body if the centripetal force disappears?
As the Dungeon Master, how do I handle a player that insists on a specific class when I already know that choice will cause issues?
How do Windows version numbers work?
Why do people keep referring to Leia as Princess Leia, even after the destruction of Alderaan?
Cops: The Hidden OEIS Substring
Is there a word for a message that is intended to be intercepted by an adversary?
Was the Ford Model T black because of the speed black paint dries?
What does "it kind of works out" mean?
Edit: I redo my coding. It runs fine now but some of my outputs are not printing
Cannot make a static reference to the non-static methodScanner cannot be resolved to a typeJava Inventory Program problemsWhy does this code using random strings print “hello world”?Method override returns nullGet total cost from arraylistHow to implement a double type methodHow can I sum the product of two two-dimensional arrays?I'm trying to use a method within my class when using a methodhow to run array using for loop and do while loopWould it make any difference giving arguments using scanner class instead of command line arguments?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am doing a payment code. But its not working. How can I make it work?
Edit: Now I have change my code a bit. It can run now but it still didn't print the output I wanted. It only prints 2 lines out of all the output I wanted. I have no idea what was the problem but I think it was at the main part. Any suggestions?
import java.util.Scanner;
public class BusPayment
public int number_of_adults;
public int number_of_children;
public double adult_price;
public double children_price;
public double totalprice;
public BusPayment()
public int getnumber_of_adults()
return number_of_adults;
public int getnumber_of_children()
return number_of_children;
public double getadult_price()
return adult_price;
public double getchildren_price()
return children_price;
public double gettotal_price()
return totalprice;
public void setadult_price(double adult_p)
adult_price=adult_p;
public void setchildren_price(double children_p)
children_price=children_p;
public void settotalprice(double total_price)
totalprice=total_price;
public BusPayment(double adult_p, double children_p)
adult_price=adult_p;
children_price=children_p;
public void BusPaymentPart()
number_of_adults=0;
number_of_children=0;
adult_price=15.00;
children_price=10.00;
totalprice=0.00;
public double calculate_totalprice()
number_of_adults= 0;
number_of_children= 0;
totalprice= (number_of_adults*adult_price)+
(number_of_children*children_price);
return totalprice;
public static void main (String []args)
BusPayment test = new BusPayment();
Scanner input=new Scanner(System.in);
System.out.println("Payment");
System.out.println("The price of an adult ticket is RM15.00.");
System.out.println("The price of a children's ticket is RM10.00.");
int number_of_adults=input.nextInt();
System.out.println("The number of adults are: " +number_of_adults);
number_of_adults=input.nextInt();
int number_of_children=input.nextInt();
System.out.println("The number of children are: "+number_of_children);
number_of_children=input.nextInt();
double totalpri = test.calculate_totalprice();
System.out.println("The total price you need to pay: " +totalpri);
I wanted the output to be like:
The price of an adult ticket is 15.00. (this was printed)
The price of a children's ticket is 10.00. (this was printed)
The number of adults are:
The number of children are:
The total price you need to pay:
Payment Successful!
java oop
|
show 1 more comment
I am doing a payment code. But its not working. How can I make it work?
Edit: Now I have change my code a bit. It can run now but it still didn't print the output I wanted. It only prints 2 lines out of all the output I wanted. I have no idea what was the problem but I think it was at the main part. Any suggestions?
import java.util.Scanner;
public class BusPayment
public int number_of_adults;
public int number_of_children;
public double adult_price;
public double children_price;
public double totalprice;
public BusPayment()
public int getnumber_of_adults()
return number_of_adults;
public int getnumber_of_children()
return number_of_children;
public double getadult_price()
return adult_price;
public double getchildren_price()
return children_price;
public double gettotal_price()
return totalprice;
public void setadult_price(double adult_p)
adult_price=adult_p;
public void setchildren_price(double children_p)
children_price=children_p;
public void settotalprice(double total_price)
totalprice=total_price;
public BusPayment(double adult_p, double children_p)
adult_price=adult_p;
children_price=children_p;
public void BusPaymentPart()
number_of_adults=0;
number_of_children=0;
adult_price=15.00;
children_price=10.00;
totalprice=0.00;
public double calculate_totalprice()
number_of_adults= 0;
number_of_children= 0;
totalprice= (number_of_adults*adult_price)+
(number_of_children*children_price);
return totalprice;
public static void main (String []args)
BusPayment test = new BusPayment();
Scanner input=new Scanner(System.in);
System.out.println("Payment");
System.out.println("The price of an adult ticket is RM15.00.");
System.out.println("The price of a children's ticket is RM10.00.");
int number_of_adults=input.nextInt();
System.out.println("The number of adults are: " +number_of_adults);
number_of_adults=input.nextInt();
int number_of_children=input.nextInt();
System.out.println("The number of children are: "+number_of_children);
number_of_children=input.nextInt();
double totalpri = test.calculate_totalprice();
System.out.println("The total price you need to pay: " +totalpri);
I wanted the output to be like:
The price of an adult ticket is 15.00. (this was printed)
The price of a children's ticket is 10.00. (this was printed)
The number of adults are:
The number of children are:
The total price you need to pay:
Payment Successful!
java oop
please add more code
– xiehongguang
Mar 26 at 3:36
1
How are you calling an instance method from a static method?
– shmosel
Mar 26 at 3:36
The codes I didn't include are the declarations, accessors, mutators and constructors because it was too long. Should I create another method? Because for it to print, it must be in the main method right?
– novice_coder
Mar 26 at 4:00
You can print from anywhere in the code if you useSystem.out.*
.
– Jason
Mar 26 at 5:17
You are creating aScanner
but not using it to retrieve input from the user. I suspect there is more code that we need to see in order to solve you problem. Please provide a minimal reproducible example that demonstrates your problem. At the moment, it appears that you are trying to use variables before you have calculated / assigned their value. Also, you are calling a non-static method from static scope (you need an instance of an object to call a non-static method on that object).
– Jason
Mar 26 at 5:21
|
show 1 more comment
I am doing a payment code. But its not working. How can I make it work?
Edit: Now I have change my code a bit. It can run now but it still didn't print the output I wanted. It only prints 2 lines out of all the output I wanted. I have no idea what was the problem but I think it was at the main part. Any suggestions?
import java.util.Scanner;
public class BusPayment
public int number_of_adults;
public int number_of_children;
public double adult_price;
public double children_price;
public double totalprice;
public BusPayment()
public int getnumber_of_adults()
return number_of_adults;
public int getnumber_of_children()
return number_of_children;
public double getadult_price()
return adult_price;
public double getchildren_price()
return children_price;
public double gettotal_price()
return totalprice;
public void setadult_price(double adult_p)
adult_price=adult_p;
public void setchildren_price(double children_p)
children_price=children_p;
public void settotalprice(double total_price)
totalprice=total_price;
public BusPayment(double adult_p, double children_p)
adult_price=adult_p;
children_price=children_p;
public void BusPaymentPart()
number_of_adults=0;
number_of_children=0;
adult_price=15.00;
children_price=10.00;
totalprice=0.00;
public double calculate_totalprice()
number_of_adults= 0;
number_of_children= 0;
totalprice= (number_of_adults*adult_price)+
(number_of_children*children_price);
return totalprice;
public static void main (String []args)
BusPayment test = new BusPayment();
Scanner input=new Scanner(System.in);
System.out.println("Payment");
System.out.println("The price of an adult ticket is RM15.00.");
System.out.println("The price of a children's ticket is RM10.00.");
int number_of_adults=input.nextInt();
System.out.println("The number of adults are: " +number_of_adults);
number_of_adults=input.nextInt();
int number_of_children=input.nextInt();
System.out.println("The number of children are: "+number_of_children);
number_of_children=input.nextInt();
double totalpri = test.calculate_totalprice();
System.out.println("The total price you need to pay: " +totalpri);
I wanted the output to be like:
The price of an adult ticket is 15.00. (this was printed)
The price of a children's ticket is 10.00. (this was printed)
The number of adults are:
The number of children are:
The total price you need to pay:
Payment Successful!
java oop
I am doing a payment code. But its not working. How can I make it work?
Edit: Now I have change my code a bit. It can run now but it still didn't print the output I wanted. It only prints 2 lines out of all the output I wanted. I have no idea what was the problem but I think it was at the main part. Any suggestions?
import java.util.Scanner;
public class BusPayment
public int number_of_adults;
public int number_of_children;
public double adult_price;
public double children_price;
public double totalprice;
public BusPayment()
public int getnumber_of_adults()
return number_of_adults;
public int getnumber_of_children()
return number_of_children;
public double getadult_price()
return adult_price;
public double getchildren_price()
return children_price;
public double gettotal_price()
return totalprice;
public void setadult_price(double adult_p)
adult_price=adult_p;
public void setchildren_price(double children_p)
children_price=children_p;
public void settotalprice(double total_price)
totalprice=total_price;
public BusPayment(double adult_p, double children_p)
adult_price=adult_p;
children_price=children_p;
public void BusPaymentPart()
number_of_adults=0;
number_of_children=0;
adult_price=15.00;
children_price=10.00;
totalprice=0.00;
public double calculate_totalprice()
number_of_adults= 0;
number_of_children= 0;
totalprice= (number_of_adults*adult_price)+
(number_of_children*children_price);
return totalprice;
public static void main (String []args)
BusPayment test = new BusPayment();
Scanner input=new Scanner(System.in);
System.out.println("Payment");
System.out.println("The price of an adult ticket is RM15.00.");
System.out.println("The price of a children's ticket is RM10.00.");
int number_of_adults=input.nextInt();
System.out.println("The number of adults are: " +number_of_adults);
number_of_adults=input.nextInt();
int number_of_children=input.nextInt();
System.out.println("The number of children are: "+number_of_children);
number_of_children=input.nextInt();
double totalpri = test.calculate_totalprice();
System.out.println("The total price you need to pay: " +totalpri);
I wanted the output to be like:
The price of an adult ticket is 15.00. (this was printed)
The price of a children's ticket is 10.00. (this was printed)
The number of adults are:
The number of children are:
The total price you need to pay:
Payment Successful!
java oop
java oop
edited Mar 27 at 15:04
novice_coder
asked Mar 26 at 3:26
novice_codernovice_coder
66 bronze badges
66 bronze badges
please add more code
– xiehongguang
Mar 26 at 3:36
1
How are you calling an instance method from a static method?
– shmosel
Mar 26 at 3:36
The codes I didn't include are the declarations, accessors, mutators and constructors because it was too long. Should I create another method? Because for it to print, it must be in the main method right?
– novice_coder
Mar 26 at 4:00
You can print from anywhere in the code if you useSystem.out.*
.
– Jason
Mar 26 at 5:17
You are creating aScanner
but not using it to retrieve input from the user. I suspect there is more code that we need to see in order to solve you problem. Please provide a minimal reproducible example that demonstrates your problem. At the moment, it appears that you are trying to use variables before you have calculated / assigned their value. Also, you are calling a non-static method from static scope (you need an instance of an object to call a non-static method on that object).
– Jason
Mar 26 at 5:21
|
show 1 more comment
please add more code
– xiehongguang
Mar 26 at 3:36
1
How are you calling an instance method from a static method?
– shmosel
Mar 26 at 3:36
The codes I didn't include are the declarations, accessors, mutators and constructors because it was too long. Should I create another method? Because for it to print, it must be in the main method right?
– novice_coder
Mar 26 at 4:00
You can print from anywhere in the code if you useSystem.out.*
.
– Jason
Mar 26 at 5:17
You are creating aScanner
but not using it to retrieve input from the user. I suspect there is more code that we need to see in order to solve you problem. Please provide a minimal reproducible example that demonstrates your problem. At the moment, it appears that you are trying to use variables before you have calculated / assigned their value. Also, you are calling a non-static method from static scope (you need an instance of an object to call a non-static method on that object).
– Jason
Mar 26 at 5:21
please add more code
– xiehongguang
Mar 26 at 3:36
please add more code
– xiehongguang
Mar 26 at 3:36
1
1
How are you calling an instance method from a static method?
– shmosel
Mar 26 at 3:36
How are you calling an instance method from a static method?
– shmosel
Mar 26 at 3:36
The codes I didn't include are the declarations, accessors, mutators and constructors because it was too long. Should I create another method? Because for it to print, it must be in the main method right?
– novice_coder
Mar 26 at 4:00
The codes I didn't include are the declarations, accessors, mutators and constructors because it was too long. Should I create another method? Because for it to print, it must be in the main method right?
– novice_coder
Mar 26 at 4:00
You can print from anywhere in the code if you use
System.out.*
.– Jason
Mar 26 at 5:17
You can print from anywhere in the code if you use
System.out.*
.– Jason
Mar 26 at 5:17
You are creating a
Scanner
but not using it to retrieve input from the user. I suspect there is more code that we need to see in order to solve you problem. Please provide a minimal reproducible example that demonstrates your problem. At the moment, it appears that you are trying to use variables before you have calculated / assigned their value. Also, you are calling a non-static method from static scope (you need an instance of an object to call a non-static method on that object).– Jason
Mar 26 at 5:21
You are creating a
Scanner
but not using it to retrieve input from the user. I suspect there is more code that we need to see in order to solve you problem. Please provide a minimal reproducible example that demonstrates your problem. At the moment, it appears that you are trying to use variables before you have calculated / assigned their value. Also, you are calling a non-static method from static scope (you need an instance of an object to call a non-static method on that object).– Jason
Mar 26 at 5:21
|
show 1 more comment
2 Answers
2
active
oldest
votes
It looks like you are calculating the values after you are trying to print them:
System.out.println("The number of adults are: " + number_of_adults);
System.out.println("The number of children are: " + number_of_adults);
System.out.println("The total price you need to pay: " + totalprice);
calculate_totalprice();
Also, you are calling a non-static method calculate_totalprice()
from within static scope:
public static void main (String []args)
...
calculate_totalprice();
Should I move the whole calculate price() into the main method instead? But it still doesn't work though. Or should I put the totalprice calculation into the main method ?
– novice_coder
Mar 26 at 3:57
add a comment |
I think you might need to call the calculations first.
calculate_totalprice();
System.out.println("The number of adults are: " + number_of_adults);
System.out.println("The number of children are: " + number_of_adults);
System.out.println("The total price you need to pay: " + totalprice);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349428%2fedit-i-redo-my-coding-it-runs-fine-now-but-some-of-my-outputs-are-not-printing%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It looks like you are calculating the values after you are trying to print them:
System.out.println("The number of adults are: " + number_of_adults);
System.out.println("The number of children are: " + number_of_adults);
System.out.println("The total price you need to pay: " + totalprice);
calculate_totalprice();
Also, you are calling a non-static method calculate_totalprice()
from within static scope:
public static void main (String []args)
...
calculate_totalprice();
Should I move the whole calculate price() into the main method instead? But it still doesn't work though. Or should I put the totalprice calculation into the main method ?
– novice_coder
Mar 26 at 3:57
add a comment |
It looks like you are calculating the values after you are trying to print them:
System.out.println("The number of adults are: " + number_of_adults);
System.out.println("The number of children are: " + number_of_adults);
System.out.println("The total price you need to pay: " + totalprice);
calculate_totalprice();
Also, you are calling a non-static method calculate_totalprice()
from within static scope:
public static void main (String []args)
...
calculate_totalprice();
Should I move the whole calculate price() into the main method instead? But it still doesn't work though. Or should I put the totalprice calculation into the main method ?
– novice_coder
Mar 26 at 3:57
add a comment |
It looks like you are calculating the values after you are trying to print them:
System.out.println("The number of adults are: " + number_of_adults);
System.out.println("The number of children are: " + number_of_adults);
System.out.println("The total price you need to pay: " + totalprice);
calculate_totalprice();
Also, you are calling a non-static method calculate_totalprice()
from within static scope:
public static void main (String []args)
...
calculate_totalprice();
It looks like you are calculating the values after you are trying to print them:
System.out.println("The number of adults are: " + number_of_adults);
System.out.println("The number of children are: " + number_of_adults);
System.out.println("The total price you need to pay: " + totalprice);
calculate_totalprice();
Also, you are calling a non-static method calculate_totalprice()
from within static scope:
public static void main (String []args)
...
calculate_totalprice();
edited Mar 26 at 5:23
answered Mar 26 at 3:32
JasonJason
10.2k3 gold badges35 silver badges45 bronze badges
10.2k3 gold badges35 silver badges45 bronze badges
Should I move the whole calculate price() into the main method instead? But it still doesn't work though. Or should I put the totalprice calculation into the main method ?
– novice_coder
Mar 26 at 3:57
add a comment |
Should I move the whole calculate price() into the main method instead? But it still doesn't work though. Or should I put the totalprice calculation into the main method ?
– novice_coder
Mar 26 at 3:57
Should I move the whole calculate price() into the main method instead? But it still doesn't work though. Or should I put the totalprice calculation into the main method ?
– novice_coder
Mar 26 at 3:57
Should I move the whole calculate price() into the main method instead? But it still doesn't work though. Or should I put the totalprice calculation into the main method ?
– novice_coder
Mar 26 at 3:57
add a comment |
I think you might need to call the calculations first.
calculate_totalprice();
System.out.println("The number of adults are: " + number_of_adults);
System.out.println("The number of children are: " + number_of_adults);
System.out.println("The total price you need to pay: " + totalprice);
add a comment |
I think you might need to call the calculations first.
calculate_totalprice();
System.out.println("The number of adults are: " + number_of_adults);
System.out.println("The number of children are: " + number_of_adults);
System.out.println("The total price you need to pay: " + totalprice);
add a comment |
I think you might need to call the calculations first.
calculate_totalprice();
System.out.println("The number of adults are: " + number_of_adults);
System.out.println("The number of children are: " + number_of_adults);
System.out.println("The total price you need to pay: " + totalprice);
I think you might need to call the calculations first.
calculate_totalprice();
System.out.println("The number of adults are: " + number_of_adults);
System.out.println("The number of children are: " + number_of_adults);
System.out.println("The total price you need to pay: " + totalprice);
answered Mar 26 at 5:25
Ed YuEd Yu
12 bronze badges
12 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349428%2fedit-i-redo-my-coding-it-runs-fine-now-but-some-of-my-outputs-are-not-printing%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
please add more code
– xiehongguang
Mar 26 at 3:36
1
How are you calling an instance method from a static method?
– shmosel
Mar 26 at 3:36
The codes I didn't include are the declarations, accessors, mutators and constructors because it was too long. Should I create another method? Because for it to print, it must be in the main method right?
– novice_coder
Mar 26 at 4:00
You can print from anywhere in the code if you use
System.out.*
.– Jason
Mar 26 at 5:17
You are creating a
Scanner
but not using it to retrieve input from the user. I suspect there is more code that we need to see in order to solve you problem. Please provide a minimal reproducible example that demonstrates your problem. At the moment, it appears that you are trying to use variables before you have calculated / assigned their value. Also, you are calling a non-static method from static scope (you need an instance of an object to call a non-static method on that object).– Jason
Mar 26 at 5:21