Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!

Carey Brown

Saloon Keeper
+ Follow
since Nov 19, 2001
Carey likes ...
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
Cows and Likes
Cows
Total received
48
In last 30 days
0
Total given
1
Likes
Total received
707
Received in last 30 days
8
Total given
94
Given in last 30 days
2
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Carey Brown

Time and time again I see Java beginners not taking the time to clean up their code before posting it so I whipped up this very rudimentary code beautifier "Indentify". My beautifier of choice is the Eclipse IDE but most beginners haven't gotten to the point of using IDEs yet.  Maybe, just maybe, this will help someone out and make our collective lives easier.

4 hours ago
You could use File#exists() to see if a file exists before you even try to open it. Don't wait until an exception is thrown because this is not actually an exceptional condition for your program.

If the file does not exist just initialize 'users' to be an empty List. No need to create a file, quit() will do that.
12 hours ago
Vectors have been replaced by ArrayList. The only thing to watch out for is that ArrayLists are not thread safe, which is not an issue in your case.

Seems like your Dictionary needs two constructors:
public Dictionary() // creates an empty HashMap
public Dictionary( String fileName ) // creates HashMap then loads it with words from the file

And maybe methods like:
public ? compare( Dictionary d2 ) // compare two Dictionaries. what output do you need?
public void merge( Dictionary d2 ) // merge d2 into this

Then in your other class you could have:
13 hours ago
Start by cleaning up your indenting. Yes, this takes time, but you're asking volunteers to give their time to help you. Show us you care.
19 hours ago
Cut and past complete error, including stack trace, into a post.
1 day ago
I think to be of further help we'd need a description of what you are trying to do (not how to do it). Seeing the code for the other class would help.
1 day ago
You "call" methods, you don't "call" a type. Your Dictionary class is a type. It contains a Set which is a type. You can call fileScanner() because that is a method.

You can have another class that has-a Dictionary instance. The only method that your Dictionary class has is fileScanner(). So, in your other class, once you've created an instance of your Dictionary class, you can then call fileScanner() using that instance. Also, you probably need to define another Dictionary constructor, one that takes no arguments.
1 day ago
Here's the doc for substring(). It even has examples. You say you already know how to use indexOf() to get the index. Substring() uses an index. I fail to see where you're stuck. Edit: Do you find the Javadocs difficult to understand?


public String substring​(int beginIndex)

Returns a string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

Examples:

     "unhappy".substring(2) returns "happy"
     "Harbison".substring(3) returns "bison"
     "emptiness".substring(9) returns "" (an empty string)

Parameters:
    beginIndex - the beginning index, inclusive.
Returns:
    the specified substring.
Throws:
    IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.
1 day ago
The use of indexOf() typically looks like this
I suggest reading the Javadocs for the String class and its methods: indexOf() and substring().

Alternatively
1 day ago
You need to break up your code. Really.

Variable names need to be descriptive.

In an ideal world you would have posted code that would compile and run. In the end I put in dummy placeholders for undefined variables that the compiler was complaining about. It now runs but aborts on a divide by zero.

I'm not sure if this is the code you are using for your time studies but just the IO (i.e. println()) would account for more than half of it. Output to a file is faster. Output to a List<String> would be even faster.

Some of your calculations are only needed when an "if()" is true. I moved the ones like that inside the if() block so that they wouldn't be computed unnecessarily.

Moved some more responsibility to your Agent#setState() method which cleaned up the body of the code so that it was easier to see what was going on.

Here's what I came up with.

1 day ago
You also have to override hashCode() for T
2 days ago
Henry has mentioned a way to deal with the problem. I'm not sure I agree with Henry though. Seems like you could use hasNextInt() to do what you want.
2 days ago
6 days ago

Campbell Ritchie wrote:...but you may find you can only go to the correct century end (31-dec-2100).

Please explain.
6 days ago
I see two things you are trying to achieve:
  • Color highlighting. This is console dependent. You'd have to research the escape sequences that your particular console uses. The eclipse console does not support color highlighting.
  • Spacing. Are you required to use tabs for spacing or can you use spaces? You can use printf() with a format string for consistent spacing. You'd have to first concatenate your "Abc" + "[" + "720" + "]" because your spacing will depend on the concatenated width not the individual widths.
  • 6 days ago