Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
This may seem like a bit of a strange question, but I want to make an "enter" keystroke into a string. I made a rather basic program to scan my papers (that I write for school) to find things such as prepositions at the end of a sentence or contractions I may have accidentally added. My issue is, when i put the paper into my code, it try to tokenize all the sentences at periods. When it reaches the point where I hit "enter" to move to the next line, it breaks and stops tokenizing. I thought of maybe making "enter" into a string so that i could replace it with a space, but I can't figure out how to get the effects of an "enter" into a string. Is there another way to stop the "enters" from breaking my code?
Sum up:
I enter my paper into my code and the effects of hitting "enter" in the paper cause the code to err. I want to be able to enter the paper and have the code ignore the effects of the "enter."
Thanks for any help!
In java, "enters" are considered new lines. There are several ways to combat your problem. The most simple way is to compare each index of the string with the '\n' char which denotes a new line.
Please post a sample code of your problem if the problem persists. It is easier to correct issues when someone can see exactly what you are doing.
I would not look for just \n\ or \r\n as the line feed can be different from computer to computer, especially with different OS.
I would use the System Properties to get the specified line feed you need:
String enterKey = System.getProperty("line.separator");
This gets the sequence used by operating system to separate lines in text files
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to know: is there impact of any kind (for example memory related issues) when I have empty lines between the code blocks in a java class?
Not at all.
Empty lines do not matter at runtime, as the java compiler turns your source code into bytecode in the very first place. The only "consequence" of using empty lines will be the line number information that the compiler can include in the bytecode files. More empty lines, resulting in higher numbers. But that is really of no concern at all.
From that point of view, you strive to use vertical spacing for one purpose, and one purpose only: to communicate intent to human readers of your code. You use empty lines to "group" things that belong together. Of course, you use them with care: too many empty lines don't help with readability a bit. You don't want the reader to scroll around when there is not need to do so.
And just to add the concern by Ernest: yeah, theoretically you could add so many empty lines that your methods get too long/big ( see here). But well, that seems to be a limit for byte code size. As said, no empty lines in byte code, so not even a theoretical problem with that.
There is no impact on the compiled code, but having too much (or too little) whitespace may make your code harder to read.
For example, put blank lines between methods, and you can use it to make 'paragraphs' in your code. Where the paragraph contains closely related code, this can help readability (and it can also help when refactoring a single method into multiple methods).
However overdoing that (eg using a lot of blank lines or using blank lines between each line of code) can actually make it harder to read.
The only impact is for us
Your java code will be compiled in bytecode, i.e. a list of simple computation, ignoring whitespaces and empty lines.
However, that doesn't mean that tabs, spaces and empty lines are useles: they improve a lot code readability, so it's a good practice to indent code, and make some block using empty lines
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
There are ways to program that use little text, but many lines of code. There are others that require more typing, but use fewer lines of code. If there is a maximum length for a line of code, this means it exists as a predefined space of memory in the computer, and making many short lines will waste this predefined space. If this is the case, your program can be a lot smaller by putting in the time to consolidate onto fewer lines. Otherwise, many short, easier to program lines would be the obvious choice.
A method has give or take 65k bytes of bytecode. However there are no limits on how many lines you write apart from the system's possible limitations (if any).
However you should always follow code-style guidelines in respect to your language to make code readable.
Read more
To directly answer your question (as I should've done already) - No. There is no maximum length of a line in Java.
There is no maximum length for a line of Java except the maximum your computer can handle however I doubt you'll end up writing a line that long.
If there is a maximum length for a line of code, this means it exists as a predefined space of memory in the computer
Just because there's a limit doesn't mean the memory is preoccupied up to that limit. The allocation could happen dynamically.
And the code is not executed, but compiled into the program.
So the lines of code do not exist in the program.
"If there is a maximum length for a line of code, this means it exists as a predefined space of memory in the computer, and making many short lines will waste this predefined space."
Being able to read code is important. Writing short lines would be better for readability. In fact, this is why we have such practices like DRY (don't repeat yourself) and Object oriented programming for methods (you can class similar functions together if need be).
Imagine writing an entire program, and you write it all up on 1 line without using any white space.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to make a program that :
accepts a string of characters(initial), i.e. a DNA sequence.
The number of characters that are accepted should be given as an input before inserting the string.
Accepts a number, then receives the same number of characters
Find all the possible 'mutations' that could occur from input 2, and check in input 1 whether there are occurrences of these mutations.
Mutations are a number of strings that could be made from input 2. For example, with AGGT, GAGT, GGAT, TGGA, AGGT, ATGG, and AGTG.
So a sample run of the program would be like
2
6 4
ATGGAT
AGGT
3
Usually, when you ask a question on this site, you should show us the effort you put in your work and exactly show or tell us where do you think what you did does not work.
I am not giving you an answer, but I'm giving you better: the opportunity to learn more on the subject. You can learn about what we call in java "regular expressions" here:http://docs.oracle.com/javase/tutorial/essential/regex/
If you make an update of your query showing us what you did, people might be more encline to help you. Stackoverflow has a great community of programmers who are willing to help you just as long as you show them respect by showing where you failed and not directly asking answers for your type of problems !
Learning about regex will definetly help you out to solve this situation. If you build a method using regex and matches and you have issues with it, post what you did and what was intended with this method !
Hope it helps you :)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to create a "morse code decoder" for Android, very similar to this app: https://play.google.com/store/apps/details?id=org.jfedor.morsecode
My app must listen a sounds (morse code) from the microphone. And translate the code in original text.
To be honest, this feature is part of a larger project. My intent is create a system:
ENCODE: a Java Application that translate a text in sound (in this case I have chosen the morse code... we don't have much time for create a our "alphabet"...). So, it is text-to-sound.
DECODE: an Android App for "listen" this sound (the morse code) and obtain the original text. So, sound-to-text.
Creating the java application isn't problem, but it is for the android app... to listen the sound is ok, but TO UNDERSTAND IT is the issue.
Just break the problem down into the parts. There's:
1) recording from the microphone [ok, no problem]
2) detecting the start times of the tones
3) building up this into a sequence of dots and dashes.
4) translating this into text
I would start from step 2)... thought to act like this: I set the app to listen to the sound at a certain frequency and speed. Must recognize morse code... translate it and print the original text for the user... but how? I do not know where to start. Any ideas?
Just break the problem down into the parts. There's :
1) recording from the microphone
2) detecting the start times of the tones
3) building up this into a sequence of dots and dashes.
4) translating this into text
None of those seems particularly difficult on its own. 2) and 3) are probably hardest, especially if the speed of the signal varies a lot or if you need to handle errors. So perhaps you could start there with some pre-recorded audio files.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to determine if an unknown 5 or 6 letter string is a valid word, i.e. is in the dictionary. I could submit the string/word to an online dictionary, but I need to check this string/word, which will be different each time, for about 100 to 150 times. This seems to be a bit time consuming.
My next thought would be to try to get a dictionary program of my own. It would need to be in Java as my program is written in Java. Does the Java API already have a class for doing this? Can I get a descent one that someone has already coded, and all I have to do is submit the string/word to it?
My program is not being used for spell checking. I want to write a program for unscrambling the Jumbled Word Puzzles when I get stuck on a scrambled word. Thanks for your suggestions.
You could use one of the open source dictionaries and load it into a database: ftp://ftp.cerias.purdue.edu/pub/dict/ and ftp://ftp.ox.ac.uk/pub/wordlists/
For scrambled words, you might want to look at the Jumble algorithm, an implementation of which is seen here.
If you don't need spell checking this would be really easy. Just load all your words into a HashSet and then check to see if that set contains the word you want to test. There are tons of word lists available.
If you do need a spell checker, then check out aspell or other free APIs.
aspell and its associated word lists and dictionaries might be the answer.
I think aspell has a Java version.
edit: actually it looks like you might do better with this aspell spinoff called Jazzy.
Maybe you can check some wordlist:
http://wordlist.sourceforge.net/
This page has some word lists in text format, so you can process in Java yourself, most easily using a HashSet. You need to use more efficient data structures if efficiency is important.
Maybe you could try Peter Norvig's spelling checker. I think it's an elegant way to get 80-90% accuracy.