Results 1 to 8 of 8
- 10-02-2012, 04:31 PM #1
Member
- Join Date
- Oct 2012
- Location
- CT
- Posts
- 9
- Rep Power
- 0
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!
- 10-02-2012, 04:36 PM #2
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!
- 10-02-2012, 04:40 PM #3
Member
- Join Date
- Oct 2012
- Location
- CT
- Posts
- 9
- Rep Power
- 0
- 10-02-2012, 05:13 PM #4
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!
- 10-02-2012, 05:21 PM #5
Member
- Join Date
- Oct 2012
- Location
- CT
- Posts
- 9
- Rep Power
- 0
- 10-02-2012, 07:32 PM #6
Member
- Join Date
- Oct 2012
- Location
- CT
- Posts
- 9
- Rep Power
- 0
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:
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.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);
I will get to the If-Else statements after I figure this out.Last edited by steve0827; 10-03-2012 at 05:40 AM.
- 10-03-2012, 10:15 PM #7
Member
- Join Date
- Oct 2012
- Location
- CT
- Posts
- 9
- Rep Power
- 0
Re: Question on user inputs and strings
Bump
- 10-04-2012, 02:49 PM #8
Re: Question on user inputs and strings
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
-
How to store multiple user inputs - should be a simple answer but I am lost.
By doowstados in forum New To JavaReplies: 1Last Post: 03-19-2012, 01:11 AM -
Controlling User Inputs
By time-space in forum New To JavaReplies: 5Last Post: 05-30-2011, 02:50 PM -
Sum positive numbers using 10 inputs from user
By pvictory1 in forum New To JavaReplies: 15Last Post: 10-10-2010, 01:30 AM -
[SOLVED] How do I make user inputs appear bolded??
By mjm1189 in forum EclipseReplies: 0Last Post: 09-13-2008, 12:59 AM -
Java program that stores user inputs
By staticy2003 in forum Advanced JavaReplies: 6Last Post: 01-24-2008, 07:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks