Can someone help me with this please?
Hello, I am a beginner and I am so stumped with these two problems. The first one is, how do I get the output to look like this?
Enter the number of pods followed by
the number of peas in a pod:
22 10
22 pods and 10 peas per pod.
The total number of peas = 220
I have attempted for hours and so far I could only get the output to display this.
Enter the number of pods followed by
the number of peas in a pod:
This is what I wrote in my code.
import java.util.Scanner;
public class JavaApplication6 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number of pods followed by");
System.out.println("the number of peas in a pod:");
int numberOfPods = keyboard.nextInt();
int peasPerPod = keyboard.nextInt();
int totalNumberOfPeas = numberOfPods*peasPerPod;
System.out.print(22 + " pods and ");
System.out.println(peasPerPod + " peas per pod.");
System.out.println("The total number of peas = "
+ totalNumberOfPeas);
}
}
I'm supposed to use a scanner for this, but the thing that gets me the most is how am I supposed to write the code that would display the numbers 22 and 10? I am having so many problems trying to get those two numbers to display.
Also, how do you output all the letters in a string to uppercase?
Like I'm trying to get a sentence to output in all uppercase letters but to no avail.
I used this code.
System.out.println("I like soda.");
String text = console.nextLine();
text.toUpperCase();
I used the above code but all I get is "I like soda." without that sentence returning in all uppercase letters.
If anyone can help me with these two problems it would be greatly appreciated. I'm sorry if I'm asking too much, I'm a beginner and I am so lost with this. Thank you.
Re: Can someone help me with this please?
Quote:
text.toUpperCase();
I used the above code but all I get is "I like soda."
It is important to realise that toUpperCase() does not alter the sequence of characters that makes up the string text. What it does is create a brand new string with the sequence of characters "I LIKE SODA.". And that new string is the one you should be printing, not text. Have a go at turning this idea into code and post if you get stuck.
-----
By the way, this sort of behaviour is pretty standard. Programmers have come to like things like String that are said to be immutable: that is they can be counted on to never change their state. One side effect of this when we *want* an altered string we have to deal with the fact that a new one is created.
Re: Can someone help me with this please?
For the pea/pod thing, your code is already reporting the number of peas per pod. Basically you should replace the 22 with the variable representing the number of pods.
Code :
System.out.print(22 + " pods and "); //<-- change this line to resemble that following
System.out.println(peasPerPod + " peas per pod.");
-----
I don't get the output you posted when I run that code. What I see is this:
Code :
Enter the number of pods followed by
the number of peas in a pod:
22 10
22 pods and 10 peas per pod.
The total number of peas = 220
If you don't see that, check you are running the most uptodate version of your program.
Re: Can someone help me with this please?
Quote:
Originally Posted by
tai8
Hello, I am a beginner and I am so stumped with these two problems. The first one is, how do I get the output to look like this?
Enter the number of pods followed by
the number of peas in a pod:
22 10
22 pods and 10 peas per pod.
The total number of peas = 220
I have attempted for hours and so far I could only get the output to display this.
Enter the number of pods followed by
the number of peas in a pod:
This is what I wrote in my code.
import java.util.Scanner;
public class JavaApplication6 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number of pods followed by");
System.out.println("the number of peas in a pod:");
int numberOfPods = keyboard.nextInt();
int peasPerPod = keyboard.nextInt();
int totalNumberOfPeas = numberOfPods*peasPerPod;
System.out.print(22 + " pods and ");
System.out.println(peasPerPod + " peas per pod.");
System.out.println("The total number of peas = "
+ totalNumberOfPeas);
}
}
I'm supposed to use a scanner for this, but the thing that gets me the most is how am I supposed to write the code that would display the numbers 22 and 10? I am having so many problems trying to get those two numbers to display.
Also, how do you output all the letters in a string to uppercase?
Like I'm trying to get a sentence to output in all uppercase letters but to no avail.
I used this code.
System.out.println("I like soda.");
String text = console.nextLine();
text.toUpperCase();
I used the above code but all I get is "I like soda." without that sentence returning in all uppercase letters.
If anyone can help me with these two problems it would be greatly appreciated. I'm sorry if I'm asking too much, I'm a beginner and I am so lost with this. Thank you.
Is this you looking for?
...
Re: Can someone help me with this please?
Quote:
Is this you looking for?
@isuru, I highly recommend you read the forum rules and http://www.javaprogrammingforums.com...n-feeding.html
I recommend you follow the excellent example of pbrockway2: teach as opposed to help cheat.