Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!

Charles Ormond

Ranch Hand
+ Follow
since Dec 18, 2019
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
15
Given in last 30 days
14
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Charles Ormond

Knute Snortum wrote:
You need a reference to the class:
And the same with the other method calls that are not in the Main class.



That's sorted it. Much appreciated.
3 days ago

I created several methods and implemented them inside the same class as the main method, but I wanted to make the program more modular so put all the methods in their own seperate class, to be called from the main.

I am receiving this error when calling these method:

The method demo is undefined for the type TextProcessorMain



Same for the other two methods I am trying to call.

I am sure its a simple mistake I've made somewhere. I'd appreciate it if someone could point out the error I've made. Thanks in advance!

Main Method code:




External Class code:

3 days ago
What's the error that is being printed?
3 days ago
Apologies if it comes across as though I'm not putting in effort and just want to be given answers, as I genuinely have been experimenting with different things but my arsenal of things I know how to try is limited and so I keep getting stuck.

To read all the lines into one string, rather than it just reading one line, I've looked into the readAllLines method which uses a filepath as a parameter; might this be the right way to go?

I've tried various ways of implementing the readAllLines method but none are working...

This is the current error:

Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1
       at java.nio.charset.CoderResult.throwException(CoderResult.java:281)
       at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
       at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
       at java.io.InputStreamReader.read(InputStreamReader.java:184)
       at java.io.BufferedReader.fill(BufferedReader.java:161)
       at java.io.BufferedReader.readLine(BufferedReader.java:324)
       at java.io.BufferedReader.readLine(BufferedReader.java:389)
       at java.nio.file.Files.readAllLines(Files.java:3205)
       at java.nio.file.Files.readAllLines(Files.java:3242)
       at TextProcessorUsingHashmaps.main(TextProcessorUsingHashmaps.java:22)



And the current code:

5 days ago
Ok, I've removed the toString and replaced it with nextLine, but then this only records the frequency for 1 line of the text file like so:

9 distinct words:
{and=1, Pirrip,=1, name=1, Christian=1, being=1, My=1, father�s=1, family=1, my=1}



What do I need to do for it to process all of the lines?


6 days ago

I should have been clearer. To clarify, I would like the program to process just a text file for the time being. I will work on adding command line text later on. I've ammended the code now to remove args as I didn't know that was just for the command line, and replaced it with an array taken from the text file.

This is now the result of running the program and inputting the filepath:

Please enter the filepath:
Test Text Document.txt
12 distinct words:
{prefix=][negative=1, suffix=][negative=1, string=\Q∞\E]=1, suffix=][NaN=1, valid=false][need=1, separator=\.][positive=1, string=\Q�\E][infinity=1, separator=\,][decimal=1, java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match=1, closed=false][skipped=false][group=1, input=false][source=1, prefix=\Q-\E][positive=1}



There are several thousand words in the test text file.

This is the updated code:

6 days ago

Junilu Lacar wrote:after reverting your code, did you test it to see if it works the way you expect it to?



Just outputs

0 distinct words:
{}

6 days ago

Junilu Lacar wrote:

Charles Ormond wrote:so I think the issue is I had to "put" some words into the hashmap first before I ran the loop.


This issue was not that, so whatever you're doing to fix the non-issue is now giving you an issue. Simply don't do it.

The actual issue was that get() was returning a null because there was no such element in the map. Your code that checks for null on the next line already addresses that issue.



Ok. I've reverted my code. So what is the proper way to put elements into the map?


6 days ago

Campbell Ritchie wrote:You have nothing to say sorry about.
The correct version of the counting program is in the Java™ Tutorials (look for “Map Interface Basic Operations”). See if you can see the difference between what they have there and what you wrote. Then I shall challenge you to explain why you are suffering an NPE in line 36. (Thank you for providing the right line number, which made it easy for me to guess what is happening.)
Also have a look at the put() method and see whether it worries about k being null.

[Addition] Don't write spaces around the < and > when specifying types.
Find out about a much better way to close resources.




Thanks Campbell Ritchie, I have implemented the code from the Tutorial you linked. The code simply returned "0 distinct words" so I think the issue is I had to "put" some words into the hashmap first before I ran the loop.

"message": "The method put(String, Integer) in the type Map<String,Integer> is not applicable for the arguments (String[], int)", line 23

As I'm using an array where a string should be. I understand the error but don't know how to correct the code:


6 days ago
Before I post my issue I'd just like to say sorry for spamming with questions recently.

I've decided to implement my Text Processing program using Hash Tables, in order to output the number of times each word in a text file appears.

My issue seems to lie inside the while loop, when I'm trying to increment the integer by 1 if it comes across the same word again.

This is the error I'm seeing:

Exception in thread "main" java.lang.NullPointerException
       at TextProcessorUsingHashmaps.main(TextProcessorUsingHashmaps.java:36)



Which value is it referring to as being null, word or f?

Here is my code:

1 week ago

Campbell Ritchie wrote:Only use classes with names ending input/output stream for binary files. For text files use classes with names ending reader/writer. You may need to specify the encoding.



Never heard this before. Will do.
1 week ago

I'm creating a program to do various forms of analysis on text input either from a file or directly into the TUI. Managed to get the wordCount method working find but the sentenceCount method needs some work.

I am receiving the error that  

"The method readLine() is undefined for the type String"



So I understand that this is not the correct method to be using, but I'm not sure which one to use instead.

Thanks in advance for any assistance.


1 week ago

I am creating a program to do some basic text processing. I am trying to create the method wordCount but am receiving the below error:

Syntax error, insert "... VariableDeclaratorId" to complete FormalParameterListJava

I am also seeing errors asking me to add in extra brackets, but I have an even number of left and right brackets so I don't know what the problem is. Can someone explain what I'm doing wrong?

Thanks in advance.

Here is my code:



1 week ago

Campbell Ritchie wrote:By the way, did you get the Pizza problem corrected? Have you tested Pizzas to see how they run?



yes, got that one sorted after a lot of playing around 👍
2 weeks ago

Dave Tolls wrote:Ah, that's the disconnect.

Your TestCustomer, TestPizza and TestOrder classes are all entirely separate things.
The stuff declared in the main method in each of them is not visible by the others.

They are, essentially, different programs.



If they are all public classes in the same folder, then why does that matter?
2 weeks ago