Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Can someone help me with this please?

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Can someone help me with this please?

    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.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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.

    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:

    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.

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    18
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Can someone help me with this please?

    Quote Originally Posted by tai8 View Post
    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?
    ...
    Last edited by copeg; February 6th, 2012 at 10:22 PM.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can someone help me with this please?

    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.