Nick Smithson

Ranch Hand
+ Follow
since Sep 21, 2015
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
10
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Nick Smithson

That was my first thought, but we can't use regex since it's not been covered yet and we were told split wont work for this because:

The Split method in the String class will not work for this purpose because it discards the delimiters it finds. You will need to write your own similar method using methods and properties of the String class such as Substring, IndexOf, IndexOfAny, IsNullOrEmpty, Empty, PadLeft, PadRight, Remove, Trim, and so forth.

6 years ago
I'm trying to get a driver to accept user input and then return the string with each word and delimiter on it's own line, but not to include spaces like this:

User Input:
"The quick, brown!fox jumps+over"

Output:
1. The
2. quick
3. ,
4. brown
5. !
6. fox
7. jumps
8. +
9.over

The code I have works with a hardcoded string except that it's leaving out all of the delimited characters like , + and so on, but when I try it with user input it won't output anything at all.
What I need help with:
1) Understanding why user input doesn't produce output
2) Understanding why the delimiter characters aren't being output at all

Any other suggestions that might help to make this a bit cleaner are appreciated. I'm also trying to get the top line that prompts for input to be red, and the rest of the text blue, and then after the user inputs a string and the output is displayed I want a top line to say something simple like Output, which I know can be done by putting code for red font where I want it and following that up by code for blue font and then doing that again on the output screen, just didn't know if there's a cleaner way to do that without repeating, but it's not a must, just something I'm curious about to try to find ways to make things cleaner and simpler.


Driver.cs



Tools.cs
6 years ago
I was lurking a bit and read through this post out of curiosity. This is the first time I've heard of a compile time constant. I decided to Google it and found an old post you made on this topic like 7 years ago: https://coderanch.com/t/454384/java/compile-time-constant

Not sure if it's ok for me to add a question to this post or how that works. Apologies in advance if I'm doing this wrong, but even after reading that older post and checking Google, I'm also a bit confused. Why is it a necessary thing to declare final such as with OPs code in order for it to work as long as it's within range? I can understand getting an error if you tried to assign 3.141592 to an int but the range of a short is -32,768 to 32,767 and x = 10 or y = 3 are both within that range. Again, sorry if I doing something wrong by adding a question to this.
6 years ago
Sorry to ask two questions back to back, but I can't get anything to work on a GUI at all. I don't really have much experience with GUI design, so this doesn't make much sense. I'm trying to at least get it to show up correctly before going on to the next step and trying to add in data and such.

So I've made a window in my driver and am trying to put a JScrollPane in the east section. That panel is created in a class of its own.


Contact List panel



I need to make a JScrollPane thats populated with the names of contacts read in from a pipespace delimited text file, and will display the corresponding contact info in fields on the west side, but for now I'm simply trying to get a JScrollPane to work just using test Strings and even that won't show up. Between google and my book, I'm not really understanding how this works.
6 years ago
I have pipespace delimited text file that's opened by file chooser and then stored into an arraylist. I need to sort the arraylist by name alphabetically. This is what I've tried, but it's not even close.





Following this, I am to somehow get this arraylist to show up in a JList, but have no idea how to even begin that I've got a GUI window that shows up, and only have a very vague idea of how panels work, but that's about it. My instructor kinda shoehorned this project in at the last minute due to poor class time management and gave minimal instruction at best. I just want to get through this class so that it doesn't tank my GPA, and switch majors. I'm done with CS. I may continue coding and work on learning more on my own, but the academic side of things feels a lot like watching CSPAN while getting a root canal.
6 years ago

These instructions tell you to Derive 3 types of Monsters. When you're told to "derive" some types, that usually means you're supposed to extend a base class. In this case, the base class is Monster. I'm not sure defining an enum really qualifies as a valid interpretation of those instructions.  You might want to run this by your instructor to make sure you're on the right track with the enum, before you get too far along and it gets more and more difficult to switch gears and head in a different direction with your design.



I asked about that earlier and got the 'ok' to do so. I also had to make sure it was ok that I put the enums inside of the Monster class. Usually we're told that we're to have enums listed in their own separate class.
6 years ago
I misread. I think Monsters caught my attention as a class name in the instructions because it was capitalized.


Derive 3 types of Monsters (for example, Cyclops, King Kong, and Frankenstein) from
the Monster class, each with its own number of health points and its own amount of
damage it can inflict.

6 years ago

Junilu Lacar wrote:Class names, for example, are normally singular, not plural. In the case of your Monsters class, it should be just Monster since an instance of this class represents only ONE monster, not several monsters.  Same thing for Participants; it should be singular, not plural.

I didn't have any control over the class names. I simply named them what I was instructed to name them. The enum thing however, was my fault.
6 years ago
One other small question:

I have this set in a for loop...


but could simply do this and cut out extra steps:


I didn't set it up the second way because I've been told that's a bad way of doing things and have been docked points for doing similar things. Instead I was told to always set method calls to a variable first before passing whatever value is returned someplace else rather than putting a method call directly into something such as a constructor or even in a println statement if the method call is set to return a string. I was told to instead call the method and store its return value into a variable and then run a print on that variable. So is one way better than the other? Good practice or bad?
6 years ago
Not sure I understand why this is showing up as a NullPointerException. There's another class set up in a similar way that works, the only real difference is that it only has one number inside the parenthesis for each enum instead of two. Originally this worked when I created a new Monsters object by setting it's class type to Participants instead of Monsters, but since changing it it seems to have broken everything.

In the GameManager class this was

then I changed it to this...

and now it's broken.

In the GameManager class I am setting up an array of Room objects so that each contains a monster and weapon (some of which may just be empty objects because I was told not to use null)


that calls to a monster spawner method


and then a new monster is created in the Monsters class...


in Rooms class:


I don't have a lot of experience with enums, actually creating multiple 'types' of monsters using the Monsters class is just an extra challenge that I wanted to try and doesn't necessarily have to be done with enumerated types. I could have simply went with the bare minimum and made one default monster only, but I wanted to try to push myself a bit.
I tried to run the Eclipse debugger but am still not sure why it's not setting up Type, if that is in fact where the problem lies. Before I changed the monster object type in GameManager from Participants to Monsters it worked fine, but I think that it was basically just skipping over everything in the Monsters class, so based on that and the error output I think the issue is with the toString in Monsters. I'm still not very familiar with using a debugger, it's something we were left to figure out on our own, but if I was understanding it correctly, it didn't seem like Type was being set to anything, but again when I had that object set with Participants it worked fine and displayed the correct enum name and everything worked as intended. The toString in this method is set up the same way its set up in a similar method (a Stick class and a Sword class, each of which inherit from a Weapons class), and that one works, and again the only difference is that the other one only has one number in the parenthesis for the enum to set the damage for each enum 'type' of sword or each 'type' of stick. Is there something I'm not understanding about enumerated data types?


Exception in thread "main" java.lang.NullPointerException
at game.Monsters.toString(Monsters.java:104)
at game.Rooms.toString(Rooms.java:128)
at game.GameManager.toString(GameManager.java:216)
at java.lang.String.valueOf(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at game.Driver.main(Driver.java:50)




Line 104 in Monsters:


Line 128 in Rooms:

6 years ago
I'm new to Java too, so please take my advice lightly.

I think your variables need to be declared inside the main method


but I also noticed you declared them again inside the method and initialized them too. So basically you have two String studentName variables. You can't have two with the same name.


As for loops, there's three choices: while, do while, and for loop. With any of these you'll need some kind of iterator variable to tell the loop when to stop running.
6 years ago
I know I can use a loop to cycle through rather than having so much duplication, it's just set up that way for now while testing movement. I tried throwing in a few print statements and when I ran it the kb seemed to have E stored in the playerMove variable, so I'm not sure what the issue is. The code that just has strings typed directly in works correctly, but the code that accepts user input doesn't. Probably been staring at this too long and I'm guessing it's some silly little thing I'm just overlooking.

In Driver class


In GameManager class



In Player class which extends Participants
(This bugs me, and I'm sure there's a way to make this cleaner, but I'm just trying to get the mechanics working first).



Here's what the console displays when this is ran and I entered E and e for the kb input:

You're in an unfamiliar room.
To explore please type E to move east and W to move west.
Room 1 contains:
The room is empty

E
Room 1 contains:
The room is empty

E
Room 1 contains:
The room is empty

e
Room 1 contains:
The room is empty

Room 2 contains:
Mordac (26 health point(s) remaining)
Badger on a stick (+4 point(s) of damage)

Room 3 contains:
The room is empty

Room 4 contains:
Toaster (23 health point(s) remaining)
No weapons

Room 3 contains:
The room is empty

Room 4 contains:
Toaster (23 health point(s) remaining)
No weapons

6 years ago

Junilu Lacar wrote:Why don't you have a room class?

Generally we can't make additional classes if they weren't specified as part of the assignment and we weren't told otherwise on this one, so I'm just sticking with what's specified.

Also, the Random class has a nextBoolean() method  that has about a 50-50 chance of being true (a boolean, after all, can only one of two values).

The 50% chance isn't the problem, just setting up the rooms so they can contain both a monster and a weapon as well as a player, but since the player and monster are both derived from  Participants class that's not as much of an issue I don't think...

A Map would be appropriate if you had a key that you mapped to a value. In this case, what would be the key that will uniquely identify a room? Note that you can use an array as a Map, if you consider the index of an element as its key.

Use an array as a map? But how would that allow me to hold two totally different types of objects? I'm not sure I fully understand that. Would that require having another class specifically designed for Rooms? It's limitations like being required to work within very tight guidelines that make some of these projects a pain.
6 years ago
I have an assignment in which I'm supposed to try to create a small text based game similar to the old Zork game.
I have to create a dungeon that can have 5 - 10 rooms (chosen at random) and each room except the start room has a 50% chance of having a monster in it. There's also a chance of finding a weapon in any of the rooms regardless of whether or not it already has a monster. A weapon can only be found in one of the rooms, if one exists at all, and there's a 50/50 chance of it being either a stick or a sword.

I have a driver class, a game manager class designed to make the game function, a sword and a stick class each of which inherits from a weapon class, and a monster and a player class each of which inherits from a participant class. (I'm required to set it up this way)

The problem I'm having is trying to figure out how to create rooms that could contain either nothing, a monster, or a monster and a weapon. Since an array of rooms can only contain one thing per index I'm not sure how to have the potential to have both a monster and a weapon in the same room. If both are found in the same room the user has to fight the monster first before they can get the weapon and they can't leave a room with a monster in it. If they revisit a room then nothing should happen - it's already been cleared out. The rooms have to be designed in such a way as to leave all those possibilities open as well. I've heard of hash maps, but have no idea what they are or even how to use them, and wouldn't be allowed to use something we haven't 'actually' learned about. Any suggestions as to how to tackle this?
6 years ago
I don't know if there's a better way to write that, and even asked my prof, but she said no. With where we're at and what we're "supposed to know", that's the best that can be done. I'm required to have that constructor with 9 parameters and because of how the data is being passed into that method she said that if I wanted to shorted the code a bit I could just leave field[0] etc. as is and pass those values into the constructor instead, but for the sake of readability while being forced to go this route, I chose to add in Strings to give each of the field indexes some sort of meaning. So this works, but is there a neater way of writing this?

6 years ago