I'm new to programming, so I was wondering if someone could help me. What I'm trying to is make the user input a number, and if the number is in the file, it'll spit something back out, and if it's not in the file, it'll keep asking for a number. I don't know exactly how to do this, but if someone could point me in the right direction, that would be great. Thanks in advance!
First read the file and store it (as a Collection, or as a big
String, the exact type depends on how you actually trying to
achieve this).
Reading a file can be done in numerous ways, some of them are using a Scanner, BufferedReader or File.readAllLines()
Now, create a while loop - that repeatidly asks the user for a
number, until the condition that the number is in your data is met.
Next, you need to process and yield the desired output.
Bonus:
Try to do each of the bullets in a different method and only combine them in your main() - it will help you to learn about basic methods design as well.
Related
I want to write a program that counts the lines of code being executed when
a test, lets say TestA(), runs. More importantly, I need to store the
lines of code being executed
by TestA(), into a codeLines: String.
I find this concept really bizarre and so far, I did not have any luck with
online resources. Do you know of any way that such functionality could be achieved?
From what I understand of the question, it seems like the closest functionality that you can easily attain is by using a debugger, which lets you step through lines, step into lines, and see what line is doing what in the code.
Click here to see a screenshot of the assignment
Here is how the Navigation.csv looks like, where I take data from
https://pastebin.com/JXnaRTzi <-- Click on the link for code - this is my code so far, I am reading the file and making objects from each entry
Guys I need help with this assignment. I chose to do it in Java, but really it doesn't matter. I need advise and help for making the right approach on this kind of problem so it can work on a larger file. What kind of data structures should I use and maybe if it's no bother, give me a solution. I am a new developer and I'm trying to get into backend and frankly I'm a bit lost.
Click on links above to see the task details.
I was looking back at some older class assignments and for one the user had to provide a text file which would be encoded according to an encryption key the user also gave. I essentially solved the problem by placing the content of the text file within a string, retrieving each letter from the string and encrypting it, and then printing the encrypted character back into the same text file. The problem is my professor docked 5% for storing the whole file content within a string, writing something like: "What if the file contents were very large?" I even recall the few people I talked to after the project was graded saying they lost points for the same reason.
At the time I thought he made sense and was too overburdened by my workload so didn't bother seeing if I could fix it because it seemed to be reasonable and simple enough. However now I can't understand how one would be able to edit the text file directly or write on the same file without storing the entire string (because one would otherwise lose its content). How would someone even go about this? Thank you!
Edit: whomever marked my thread as a duplicate to the one above clearly did not understand my question. I am asking how to manipulate the same file without using an absurd amount of memory as the solution I stated would. The other thread clearly asks what the quickest way to read from a file is, which is not at all the same thing. Joop had the right idea of what I meant so I'll try just that, thank you Joop.
need some understanding here on what my objective is in this assignment. I'm not looking for anyone to write my code for me. I'm still a noob in coding, and don't quite understand the lingo as good. However, I will mention what I thought of so far and hopefully I'm right.
The assignment:
"Write a program that reads strings from the user and writes them to an output file called userStrings.txt. Stop processing when the user enters the string "DONE". Do not write the sentinel string ("DONE") to the output file."
I can't copy/paste pics yet since I'm also new to Stack Overflow.
The results however show this in the IDE's terminal:
A string.
Another string.
Yet more text...
DONE
Then right next to that pic is showing text but in notepad:
A string.
Another string.
Yet more text...
So my conflictions lie in "Write a program that reads strings from the user.."
which made me think that I have to write a scanner that reads lines from a user input on the IDE terminal and the program stores it into userStrings.txt.
That seems excessively hard from my point of view compared to an assignment that was 30 points dealing with inheritance and classes; and this only being worth 15 points seems to be asking for a lot.
After I studied and a little bit and search good'o google I found that people mainly program either filewriter (which writes a directly into notepad) or a filereader (that reads a current file in notepad).
So which one is it?
Scanner + filewriter?
Just a plain file writer or reader?
Literally losing my mind lol. Also any brief elaboration is helpful too, in noobish please.
Appreciate the help!
Sounds like you've got the right answer in using the Scanner class. I'm sure FileWriter would work to write to file, but there's a class PrintWriter which is basically the output version of Scanner. Both Scanner and PrintWriter have methods that are pretty easy to understand, so I'd go with those two.
Once you have those set up, it's shouldn't be too difficult to read in a string from Scanner, pass it PrintWriter, and then write that to a file.
Good luck on the homework! :)
I have a program I'm writing. I want the program to be able to stop, and start off where it left off the next time I run it. I have some int variables. The way I'm storing the data only works in Strings, so I have been trying to do this with my integer variables:
attack = Integer.valueOf(wordAttack);
Here is the way I'm trying to get the information:
wordAttack = (prop1.getProperty("dbattack"));
Here is how I'm trying storing the data:
String wordAttack = String.valueOf(attack);
and
prop.setProperty("dbattack", wordAttack);
If there is a way to store the value of an int similar to the way I am know, please let me know. I would really like to know how I can store these integers for the next time I run the programs.
Thanks,
~Rane
If you are doing something big I would suggest you to learn how to use a database.
If you are doing something smaller you can write your own files. Also there is a way to serializa a Java object and save it but if you change the object after then you will lose what you already have.