Jump to content

Java Help Requested

Sturalke
Go to solution Solved by Philosobyte,

I've added a few test cases including those that were included with the instructions as examples.

 

I have tried printing out certain variables all throughout the code, and sometimes it's correct and sometimes the variable "tweet" prints a single space.

Well, it's good that on my setup, all of the examples that you marked as returning incorrect output actually work correctly with the code you posted. That means nothing is wrong with your code. It's a problem with your compiling process, or more likely, an input process. If "tweet" outputs as a single space, then there's a problem with you typing it in/the program receiving the text because "tweet" is never modified after you assign the initial String. 

 

Are you running your program in an IDE (specify which one) or the terminal? I ran it successfully in IntelliJ Idea. edit: It works correctly with Windows Command Prompt, too. 

I'm having some issues with a project for school. We're supposed to be learning incredibly simple things with Java, but since the program they use to check the code only checks the outputs I decided to sacrifice some of the incredible lead I have versus the other students in the class. I tried to do something using my knowledge of other languages and Google as a resource, and now I've backed myself into a hole here. Here's the instructions for the activity:

In this assignment you will write a Tweet Tester.
Twitter allows users to send messages of 140 characters or less. Users direct tweets to specific users by using @mentions and label tweets by using #hashtags. Tweets may also contain links that start with http://.
For this lab you will ask the user to enter a potential tweet. First you will check to see if it is a valid tweet by checking that the length is less than or equal to 140 characters.
If the tweet is too long print out the number of characters over 140.
If the tweet is valid print Length Correct and count the number of @mentions, #hashtags and number of links using the following rules:
Every mention will start with the '@' character and have at least one non-space or non-tab character following it.
All hashtags will start with the '#' character and have at least one non-space or non-tab character following it.

All links will start with the string "http://". Twitter ignores case, so HTTP and http are counted as the same set of characters. You do not need to check for any characters following the http:// .

 

The code I've written doesn't always work. With certain strings it will be just fine. Also, I can't see why the 140 character limit never seems to work. I've been stumped over this for 4 hours now and my Googling skills have failed me. D:

import java.util.*;import java.lang.*;import java.io.*;class Main {	public static void main(String[] args) throws IOException {		int hash = 0, att = 0, link = 0;		Scanner s = new Scanner(System.in);		System.out.println("Please enter a tweet: ");		String tweet = s.nextLine();		if (tweet.length() <= 140) {			System.out.println("Length Correct");			String lo = tweet.toLowerCase();			String[] words = lo.split(" ");			for (int x = 0; x < words.length; x++) {				if (words[x].length() > 1 && words[x].startsWith("#")) {					hash++;				}				if (words[x].length() > 1 && words[x].startsWith("@")) {					att++;				}				if (words[x].length() > 1 && words[x].startsWith("http://")) {					link++;				}			}			System.out.println("Number of Hashtags: " + hash + "\nNumber of Attributions: " + att + "\nNumber of Links: " + link);		} else {			System.out.println("Excess Characters: " + (tweet.length() - 140));		}	}}

Test cases that fail:

A long tweet with 185 characters. Should fail.

Input:

This is a #long tweet. An extra long #link. So, when @You write your code it should ignore all of the #hastags and @mentions since it is too long. It should also ignore all http://links

Output:

Length Correct
Number of Hashtags: 0
Number of Attributions: 0
Number of Links: 0

 

A shorter tweet that should go through the parsing section of the code. It has 3 hashtags, 2 attributions, and one link.

Input:

This #tweet is #short and has several #hashtags. And HTtp://links @and @mentions

Output:

Length Correct

Number of Hashtags: 0
Number of Attributions: 0
Number of Links: 0

 

Input:

#Yeah go @me http://thebomb.com random #stuff

Output:
Length Correct
Number of Hashtags: 2
Number of Attributions: 0

Number of Links: 0

 

Test cases that pass:

Input:

#Yeah @me http://thebomb.com

Output:

Length Correct
Number of Hashtags: 1
Number of Attributions: 1
Number of Links: 1

 

I'm trying to find more.

Link to comment
Share on other sites

Link to post
Share on other sites

Please include in your post a couple example tweets which don't work so we can play around with it. 

 

Have you tried pinpointing the errors by printing out the values of the variables "tweet" or "words" to see if the http and/or length problems are due to escape characters or some other corruption?

Link to comment
Share on other sites

Link to post
Share on other sites

Please include in your post a couple example tweets which don't work so we can play around with it. 

 

Have you tried pinpointing the errors by printing out the values of the variables "tweet" or "words" to see if the http and/or length problems are due to escape characters or some other corruption?

 

I've added a few test cases including those that were included with the instructions as examples.

 

I have tried printing out certain variables all throughout the code, and sometimes it's correct and sometimes the variable "tweet" prints a single space.

Link to comment
Share on other sites

Link to post
Share on other sites

I've added a few test cases including those that were included with the instructions as examples.

 

I have tried printing out certain variables all throughout the code, and sometimes it's correct and sometimes the variable "tweet" prints a single space.

Well, it's good that on my setup, all of the examples that you marked as returning incorrect output actually work correctly with the code you posted. That means nothing is wrong with your code. It's a problem with your compiling process, or more likely, an input process. If "tweet" outputs as a single space, then there's a problem with you typing it in/the program receiving the text because "tweet" is never modified after you assign the initial String. 

 

Are you running your program in an IDE (specify which one) or the terminal? I ran it successfully in IntelliJ Idea. edit: It works correctly with Windows Command Prompt, too. 

Edited by Raymondbl
Link to comment
Share on other sites

Link to post
Share on other sites

Well, it's good that on my setup, all of the examples that you marked as returning incorrect output actually work correctly with the code you posted. That means nothing is wrong with your code. It's a problem with your compiling process, or more likely, an input process. If "tweet" outputs as a single space, then there's a problem with you typing it in/the program receiving the text because "tweet" is never modified after you assign the initial String. 

 

Are you running your program in an IDE (specify which one) or the terminal? I ran it successfully in IntelliJ Idea. edit: It works correctly with Windows Command Prompt, too. 

 

Well, that tells me that I should stop using Browxy.com as a compiler. I was too lazy to try and find something else. I can't install anything on the school computer so I was only looking for in browser IDEs. Surprisingly there aren't very many for Java from what I've seen.

 

I swear though, I've written this same code 4 or 5 times over. I don't know what I changed that now it works. I was judging it mostly with the course's program checker which discretely runs tests and only tells me what percentage of the test cases pass. Before I was only getting 87% or so. I'll try and find an older copy I have saved somewhere and compare them. Thanks for the sanity check though.

Link to comment
Share on other sites

Link to post
Share on other sites

I'm having some issues with a project for school. We're supposed to be learning incredibly simple things with Java, but since the program they use to check the code only checks the outputs I decided to sacrifice some of the incredible lead I have versus the other students in the class. I tried to do something using my knowledge of other languages and Google as a resource, and now I've backed myself into a hole here. Here's the instructions for the activity

 

You say the class is for learning Java. You also say you're using your knowledge of other languages to complete the project. Yet you're using Java too. I'm confused.

 

Did you try it in another language, then realize it wasn't working and go back to using Java?

 

 

Well, that tells me that I should stop using Browxy.com as a compiler. I was too lazy to try and find something else. I can't install anything on the school computer so I was only looking for in browser IDEs. Surprisingly there aren't very many for Java from what I've seen.

 

If the class is teaching Java, what kind of environment are they giving you to work with? I'm assuming you have access to the command line compiler. What do they have installed as an editor for everyone?

 

 

Anyway, here's a couple more options for online compilers (they may or may not be better than browxy)

If you are looking for something more substantial, then look into a cloud IDE (link says web developers but many of the options there support Java and other languages). Many of them have free plans which might be all you need.

 

Other potential options include

  • Portable editors
  • Connecting remotely to your home machine
Link to comment
Share on other sites

Link to post
Share on other sites

 

You say the class is for learning Java. You also say you're using your knowledge of other languages to complete the project. Yet you're using Java too. I'm confused.

 

Did you try it in another language, then realize it wasn't working and go back to using Java?

 

If the class is teaching Java, what kind of environment are they giving you to work with? I'm assuming you have access to the command line compiler. What do they have installed as an editor for everyone?

 

Have you ever seen or heard of the online curriculum called Edhesive? It's poorly made, in my opinion, but all you learn is Java and the program they use to test code is for Java only. I was stating that since I am well versed in some other languages that I was using those experiences to figure out ways to solve problems without extensively googling how to use "Math.max()" for example. Sorry for the misunderstanding.

 

The IDE they have installed on the computers there at the school is something called Dr Java. It's so basic and awful at formatting that I tired to have them install Notepad++, but the IT guys at the school refuse to do it because the course says to use Dr Java for some reason.

 

 

Anyway, here's a couple more options for online compilers (they may or may not be better than browxy)

If you are looking for something more substantial, then look into a cloud IDE (link says web developers but many of the options there support Java and other languages). Many of them have free plans which might be all you need.

 

Other potential options include

  • Portable editors
  • Connecting remotely to your home machine

 

I'll have to try some of these out. Definitely the remote access one. Maybe for once I can get around Lightspeed Systems and go on 9gag or something to waste my day away. xD

Thank you, sir!

Link to comment
Share on other sites

Link to post
Share on other sites

Have you ever seen or heard of the online curriculum called Edhesive? It's poorly made, in my opinion, but all you learn is Java and the program they use to test code is for Java only.

 

Nope, I haven't. To my knowledge they didn't offer any AP Comp Sci program in my high school at the time I attended. So it might not be perfect, but at least you have one ;)

 

As for learning one language, that's pretty normal. Universities often only teach one language in their Intro To Comp Sci courses as well.

 

I was stating that since I am well versed in some other languages that I was using those experiences to figure out ways to solve problems without extensively googling how to use "Math.max()" for example. Sorry for the misunderstanding.

 

I see.

 

The IDE they have installed on the computers there at the school is something called Dr Java. It's so basic and awful at formatting that I tired to have them install Notepad++, but the IT guys at the school refuse to do it because the course says to use Dr Java for some reason.

 

Haven't used DrJava myself either. After looking up a couple videos that use it, it doesn't look too bad but I can understand why you'd want to use something else. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×