i am trying to create a loop that exits when i press enter twice with blank lines. I've tried different methods but still can't think of a method that works. When this codes entered the input is saved to a file however i can't exit the loop no matter how many times i press enter with blank lines. help please. -
private void run()
-
-
{
-
-
FileOutputStream outputStream;
-
PrintWriter printWriter;
-
String fileNam;
-
String input;
-
-
System.out.println("Creating new backupfile");
-
System.out.println("Enter file name: ");
-
fileNam = Genio.getString();
-
System.out.println("Input: ");
-
input = Genio.getString();
-
-
try
-
-
{
-
-
outputStream = new FileOutputStream(fileNam);
-
printWriter = new PrintWriter(outputStream);
-
-
while(true)
-
{
-
printWriter.println(input);
-
-
if(input.equals(""))
-
{
-
printWriter.flush();
-
printWriter.close();
-
}
-
-
}
-
}
-
-
catch(Exception e)
-
-
{ System.out.println("File cannot be written to"); }
-
-
System.out.println("backup finished");
-
-
}
-
2 4805
try '\0' or '\n' if equals those, or if NULL has being define you can say if equals NULL, i haven't written much in java but if you really need help then tell so i can work on it. =)
Hi there!
It's a bit difficult to answer the question completely, as you didn't include the code defining and initializing the "Genio" object, but there are a few things you'll have to change. - You'll want to reread the input in that loop of yours, so better put the input = Genio.getString(); line inside the loop. The way it is, it's not reading whatever you type. Mind you, if "input" is just the name of the file, you'll have to create some kind of FileReader.
- As your loop has the condition "true" (which is always true of course), you need a different way to get out of it. If you insist on keeping the loop that way, you will want to use a break command. Here's a quick example of how to use break:
- int i=0;
-
while(true) {
-
i++;
-
println(i);
-
if(i>100) break;
-
}
That will print the numbers 1 - 100 and then stop. - As you want it to stop if you press enter twice, you'll have to add code to remember if it's pressed once. For example you could do something like this:
- boolean pressedOnce = false;
-
while(true) {
-
/*
-
put code for reading input here
-
*/
-
if(input.equals("")) {
-
if(pressedOnce)
-
break;
-
else
-
pressedOnce = true;
-
}
-
else {
-
pressedOnce = false; // leave this out, if you don't want to limit yourself to two empty lines directly after each other
-
/*
-
whatever you want to do if the input was not an empty line
-
*/
-
}
-
}
- If you're not stuck with the while(true) thing, you could also do something more sophisticated, like this:
- boolean pressedOnce = false;
-
while((input = Genio.getString) != "" || !pressedOnce) {
-
if(input.equals("")) {
-
/*
-
similar to before
-
*/
-
}
-
}
(Untested, but it should work something like that.)
OK, that's all I can think of right now.
Greetings,
Nepomuk
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jukka K. Korpela |
last post by:
I noticed that Internet Explorer (6.0, on Win XP SP 2, all fixes
installed) incorrectly renders e.g.
&harr &euro &Omega
literally and not as characters denoted by the entities, but if a
semicolon...
|
by: Javier |
last post by:
Hello:
I have a problem with encoding. I get information from a web page, and
sometimes it contains "strange" symbols like "á" and "é", and these come
encoding like "& a a c u t e ;", "& e a c u...
|
by: G Fernandes |
last post by:
Hi, can someone tell me what the following words mean as per C/clc:
1) token
2) token sequence
3) scalar variable
4) vector
|
by: Johann Blake |
last post by:
I posted a related problem today. The problem is this:
string str1 = @"""";
When I execute this code (even in a bare bones application), in the
IDE it returns "\"""
Why? Even in the...
|
by: Murphy |
last post by:
Our website contains subdirectories for each subsidiary company, each
company has it's own look and feel to the pages in their subdirectory
although they are all part of the main website.
The...
|
by: RFS666 |
last post by:
Hello,
I would like to use variables with a type in jscript.NET.
I declare them as follows:
var x : double = 5.03;
This doesn't work in my script, that I write to the page in codebehind with...
|
by: hedonplay |
last post by:
Please explain this declaration in details
it makes confusing!
|
by: manchin2 |
last post by:
Hi,
Can anybody please provide the information about """ and its use, if possible please provide an example.
...
|
by: fran7 |
last post by:
Hi, from help in the javascript forum I found the error in some code but need help. This bit of code works perfectly, trouble is I am writing it to a javascript function so the height needs to be in...
|
by: LionelAndJen |
last post by:
I have an XML file that has a comment field in which the data
provider, very kindly, already uses """ when writing "doesn't", I
have doesn't .
it's PERFECT, because that xml is then fed to...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |