Results 1 to 14 of 14
  1. #1
    amorosa19 is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Question enter a string sentence

    hi everyone

    i am new to java, and i am having some problems with inputting a sentence. i wanted to write a program that will be able to read a sentence. this is what i have so far :

    import java.util.*;

    public class Sentence
    {
    public static void main (String[] args)
    {
    String name;
    String surname;
    String IDcard;
    String description;
    String hobbies;

    Scanner input = new Scanner (System.in);

    System.out.print ("Enter your name: ");
    name = input.next();
    System.out.print ("Enter your surname: ");
    surname = input.next();
    System.out.print ("Enter your ID card number: ");
    IDcard = input.next();
    System.out.print ("Enter a description of yourself: ");
    description = input.next();
    System.out.print ("Enter your hobbies: ");
    hobbies = input.next();
    }
    }

    i tried using delimiters, but i don't actually know how to use them. any ideas :confused: ?

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Scanner methods...

    Read the scanner methods, specially the one that reads lines (hint,hint)
    Scanner (Java Platform SE 6)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello and welcome to the forum!

    A few questions for you regarding your questions:
    i wanted to write a program that will be able to read a sentence.
    Please go into a little more detail here. Just what exactly do you mean by "read a sentence"? The more detail the better.
    this is what i have so far :
    Thanks for posting your code as this helps us. Does your code not work for you, and if so, how so? Again, details will help.

    Good luck in your coding!

    edit: mi amigo CJSLMAN may have found your problem. Please follow his lead first before worrying about my questions. Again, good luck.

  4. #4
    amorosa19 is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    With this class, i want to be able to ask the user to enter their hobbies and description as a string. The code currently just reads one word, so when the user isn't able to enter the hobbies section.

    for example:

    Enter your name: amorosa
    Enter your surname: amorosa
    Enter your ID card number: 123456
    Enter a description of yourself: brown hair
    Enter your hobbies:

    By this program, the user currently can't enter his hobbies as 'hair' will be stored as his hobbies whilst 'brown' as his/her description. In the end, I want to be able to make a menu where the user will be able to see the previous entries.

    for example:

    1. Enter a user's profile
    2. View all profiles

    But currently, I only need to ask the user to enter a sentence.

    Thanks for your help :)

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default hhhmmm...

    From your answer I'm assuming you haven't read my post yet. The link (and the hint) I gave you is what you're looking for. I'll try again...
    use the method that reads the next line (super hint, hint).

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    amorosa19 is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    Yes, I have already read your post. But my program is still not working =\

    But actually, I was trying to use delimiters so that first the sentence is entered, and then it skips a line by ' \n '

    But I'm not managing, and I don't know what I'm doing wrong :(

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Code please...

    Post your code and let's see what you have...

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    CJSLMAN suggested you look at the Scanner class's API. Perhaps you want to experiment with other scanner methods, ones that involve or have the term "Line" somewhere in them?

  9. #9
    amorosa19 is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    import java.util.Scanner;

    public class Sentence
    {
    public static void main (String[] args)
    {
    String name;
    String surname;
    String IDcard;
    String description;
    String hobbies;

    Scanner input = new Scanner (System.in);

    System.out.print ("Enter your name: ");
    name = input.next();
    System.out.print ("Enter your surname: ");
    surname = input.next();
    System.out.print ("Enter your ID card number: ");
    IDcard = input.next();
    System.out.print ("Enter a description of yourself: ");
    description = input.nextLine();
    System.out.print ("Enter your hobbies: ");
    hobbies = input.nextLine();
    }
    }


    Currently, I have this code. So the program is reading "Enter your hobbies" as a description. But i was not intending to use the nextLine. I was trying to make use of delimiters. For example, something like this:

    String input = "1 fish 2 fish red fish blue fish";
    Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
    System.out.println(s.nextInt());
    System.out.println(s.nextInt());
    System.out.println(s.next());
    System.out.println(s.next());
    s.close();

    I have already declared the Scanner above, so there is no use to declare it again but i don't know how I can call it ...

  10. #10
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you call it like this:

    input.useDelimiter("\\s*fish\\s*");
    hobbies1 = input.next();
    hobbies2 = input.next();
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  11. #11
    VinceGuad is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    After a user enters text you have to use the scan method to scan the line.

  12. #12
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default huh?

    After a user enters text you have to use the scan method to scan the line.
    huh? To began with, there is no "scan" method... it's "Scanner" and it's a class and it contains lots of methods.
    Why suggest something (incomplete and pretty useless) that has been explained in multiple posts above?

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  13. #13
    deermen001 is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Thumbs up thanks CJSLMAN

    Thank u
    u gave a wonderful hint to use nextLine instead of next

  14. #14
    deermen001 is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

Similar Threads

  1. String/sentence to unicode convertion
    By sandeepvreddy in forum New To Java
    Replies: 5
    Last Post: 11-20-2008, 04:33 PM
  2. reading in 2 sentence for java, I am lost ...
    By madatlis in forum New To Java
    Replies: 16
    Last Post: 10-10-2008, 01:21 PM
  3. Applet - enter URL and view the page
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-10-2008, 03:38 PM
  4. Javascript and enter button
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 06-11-2007, 10:39 PM
  5. How to extract info from a sentence
    By luisarca in forum XML
    Replies: 1
    Last Post: 06-07-2007, 06:43 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
  •