Results 1 to 8 of 8
Like Tree3Likes
  • 1 Post By SurfMan
  • 1 Post By SurfMan
  • 1 Post By SurfMan

Thread: Hi, Need help with java program

  1. #1
    The James is offline Member
    Join Date
    Apr 2017
    Posts
    8
    Rep Power
    0

    Default Hi, Need help with java program

    This is the specification:
    Write a program that asks the user to type in a sentence. The program reports the following to the screen
    • the number of characters in the sentence, including white spaces
    • the first letter of the sentence
    • the last letter of the sentence
    • the middle character of the sentence, including white spaces

    Finally the program prints the phrase “oi” at the start of the sentence and “oi, oi, oi!” at the end of the sentence, as shown below.

    Here is the Code so far, however it only counts the first word. Also I dont know how get first letter,last letter, and middle character. Thanks


    import java.util.Scanner;
    public class Sentence counter
    {
    public static void main(String[] args)
    {
    Scanner scan = new Scanner(System.in) ;

    System.out.println ( "Enter a sentence ");
    String message = scan.next();

    int numberOfLetters = message.length( ) ;
    System.out.println ( "the number of letters is " + numberOfLetters ) ;}
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Hi, Need help with java program

    Use scan.nextLine()
    The James likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  3. #3
    The James is offline Member
    Join Date
    Apr 2017
    Posts
    8
    Rep Power
    0

    Default Hi, Need help, java program

    Here are the specification of program.

    Write a program that asks the user to type in a sentence. The program reports the following to the screen
    • the number of characters in the sentence, including white spaces
    • the first letter of the sentence
    • the last letter of the sentence
    the middle character of the sentence, including white spaces
    Finally the program prints the phrase “oi” at the start of the sentence and “oi, oi, oi!” at the end of the sentence, as shown below.

    Here is mine but it only reads first word and I don't know how to print the first letter of the sentence, the last letter of the sentence and
    the middle character of the sentence, including white spaces.



    import java.util.Scanner;
    public class sentence reader
    {
    public static void main(String[] args)
    {
    Scanner scan = new Scanner(System.in) ;


    System.out.println ( "Enter a sentence ");
    String message = scan.next();

    int numberOfLetters = message.length( ) ;
    System.out.println ( "the number of letters is " + numberOfLetters ) ;}
    }

  4. #4
    The James is offline Member
    Join Date
    Apr 2017
    Posts
    8
    Rep Power
    0

    Default Re: Hi, Need help with java program

    Thanks SurfMan.

    What code reads middle letter, first letter.

    Thanks

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Hi, Need help with java program

    String has a method to get a character at a specific location. There is no getMIddleCharachter() method. Sometimes, programming actually requires a bit of thinking. :) Something like "the length of the String, divided by two".
    The James likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  6. #6
    The James is offline Member
    Join Date
    Apr 2017
    Posts
    8
    Rep Power
    0

    Default Re: Hi, Need help with java program

    What about getting first letter

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Hi, Need help with java program

    Strings are just arrays of characters, or arrays of char if you like. Arrays always start at index 0, so by definition, the first letter would be at position 0. It's all in the documentation that the JDK has/includes: String (Java Platform SE 8 )

    Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.
    You might wonder why I (and many other forum members) don't give you a working solution. Reason is that there are a lot of people trying to get their homework done, instead of trying to learn something. I don't wanna spoil your learning experience by handing out a full working code example. So keep up the good work, keep experimenting and keep asking (smart) questions.
    Norm likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  8. #8
    The James is offline Member
    Join Date
    Apr 2017
    Posts
    8
    Rep Power
    0

    Default Re: Hi, Need help with java program

    Yeah Thanks, I figured it out

Similar Threads

  1. Replies: 19
    Last Post: 07-17-2015, 03:40 PM
  2. Replies: 6
    Last Post: 12-15-2013, 06:40 AM
  3. Replies: 4
    Last Post: 11-21-2012, 01:19 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •