Results 1 to 8 of 8
  1. #1
    steve0827 is offline Member
    Join Date
    Oct 2012
    Location
    CT
    Posts
    9
    Rep Power
    0

    Default Question on user inputs and strings

    If this is in the wrong section, mods please feel free to move it.

    Hi everyone,
    I'm not looking for answers, just some direction/hints.

    That being said, I am trying to create a program that can take one sentence of input and create separate lines of out with the us of if-else statements.

    For example, the user would type the following tweet in the format of name, twitter username, tweet text(surrounded by double quotes), short link(optional), hashtag(optional), and date: John Doe @johndoe "I enjoy being anonymous!" http://bit.ly.ttyw32 #anonymous 27 Sept 12

    And then have it output as
    @johndoe
    John Doe
    27 Sept 12
    "I enjoy being anonymous!" http://bit.ly.ttyw32 #anonymous
    "I enjoy being anonymous!"
    http://bit.ly.ttyw32
    #anonymous


    So far I have started off with:
    import java.util.Scanner;

    public class Tweet {
    public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter a Tweet, eg:John Doe @johndoe \"I enjoy being anonymous!\" http://bit.ly.ttyw32 #anonymous 27 Sept 12");

    }
    }



    I am having a tough time figuring out how to "tag" each part of the user's entry in order to output it in different lines. Once I have figured that out, I will work on the if-else statement for the short link and hashtag.

    Once again, I'm not looking for the direct answer....just some direction/hints.

    Thanks in advance!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Question on user inputs and strings

    You've posted this into the applet section of the forum. Is this an applet? If so you'll have to get user input through a GUI, not a command line.

    As for taking apart the String, you could either just iterate over it yourself and take the appropriate actions, or you could look into the Pattern class for using a regular expression.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    steve0827 is offline Member
    Join Date
    Oct 2012
    Location
    CT
    Posts
    9
    Rep Power
    0

    Default Re: Question on user inputs and strings

    Quote Originally Posted by KevinWorkman View Post
    You've posted this into the applet section of the forum. Is this an applet? If so you'll have to get user input through a GUI, not a command line.

    As for taking apart the String, you could either just iterate over it yourself and take the appropriate actions, or you could look into the Pattern class for using a regular expression.
    Woops, my bad. This isn't an applet. I'm trying to get this to work through Eclipse. If it's possible for a mod to move this to the appropriate section, I would greatly appreciate that.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Question on user inputs and strings

    I moved the thread for you, but you might want to try out my suggestions and let us know where that gets you.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    steve0827 is offline Member
    Join Date
    Oct 2012
    Location
    CT
    Posts
    9
    Rep Power
    0

    Default Re: Question on user inputs and strings

    Quote Originally Posted by KevinWorkman View Post
    I moved the thread for you, but you might want to try out my suggestions and let us know where that gets you.
    Will do....I'll post back with my results. Thanks!

  6. #6
    steve0827 is offline Member
    Join Date
    Oct 2012
    Location
    CT
    Posts
    9
    Rep Power
    0

    Default Re: Question on user inputs and strings

    After going through both of the things you mentioned, I realized that isn't the way my professor wants me to do it as we haven't learned anything about that yet.
    This is what I've done so far....unfortunately, it does not allow me to make a "tweet" longer than one word and if name entered is shorter or longer than 3 words, everything is messed up.

    Java Code:
    import java.util.Scanner;
    
    public class Assignment3 {
    	public static void main(String[] args) {
      
    		Scanner keyboard = new Scanner(System.in);
    		
    	System.out.println("Enter a Tweet, eg: Doug Robertson @dougnut \"Let's do Java\" [url]http://bit.ly.ghRE67[/url] #bentley 25 Sep 12");
    	
    	String name1 = keyboard.next();
    	String name2 = keyboard.next();
    	String name3 = keyboard.next();
    	String shortname = keyboard.next();
    	String tweet = keyboard.next();
    	String link = keyboard.next();
    	String hashtag = keyboard.next();
    	String date1 = keyboard.next();
    	String date2 = keyboard.next();
    	String date3 = keyboard.next();
    	
    	
    	System.out.println(shortname);
    	System.out.println(name1 + " " + name2 + " " + name3);
    	System.out.println(date1 + " " + date2 + " " + date3);
    	System.out.println(tweet + " " + link + " " + hashtag);
    	System.out.println("	" + tweet);
    	System.out.println("	" + link);
    	System.out.println("	" + hashtag);
    	}
    }

    I tried using delimiters like this:
    Java Code:
    Scanner keyboard = new Scanner(System.in);
    		Scanner keyboard1 = new Scanner(System.in);
    		keyboard1.useDelimiter("@");
    String name1 = keyboard.next();
    String shortname = keyboard1.next();
    System.out.println(name1);
    System.out.println(shortname);
    but after entering something like "John the deer @johnthedeer" and hitting enter, nothing happens. I also don't think I will be able to keep @ and " if I use delimiters, because I'm not supposed to be entering those in the user input.


    I will get to the If-Else statements after I figure this out.
    Last edited by steve0827; 10-03-2012 at 05:40 AM.

  7. #7
    steve0827 is offline Member
    Join Date
    Oct 2012
    Location
    CT
    Posts
    9
    Rep Power
    0

    Default Re: Question on user inputs and strings

    Bump

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Question on user inputs and strings

    Quote Originally Posted by steve0827 View Post
    Bump
    Some things to consider: https://www.coderanch.com/how-to/java/PatienceIsAVirtue

    Anyway, I'm not sure exactly what you want us to say. We've provided suggestions, which you've refused to follow based on some constraints that we don't know anything about. You've said that "nothing happens", but that's a little vague- do you get an error? Does your program freeze up? Have you tried debugging this, or at least stepping through it with a piece of paper and a pencil?

    Hint: Try printing out each token once you read it in.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 1
    Last Post: 03-19-2012, 01:11 AM
  2. Controlling User Inputs
    By time-space in forum New To Java
    Replies: 5
    Last Post: 05-30-2011, 02:50 PM
  3. Sum positive numbers using 10 inputs from user
    By pvictory1 in forum New To Java
    Replies: 15
    Last Post: 10-10-2010, 01:30 AM
  4. Replies: 0
    Last Post: 09-13-2008, 12:59 AM
  5. Java program that stores user inputs
    By staticy2003 in forum Advanced Java
    Replies: 6
    Last Post: 01-24-2008, 07:46 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
  •