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 4833
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. =)
Nepomuk 3,112
Recognized Expert Specialist
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 is appended to each of the entity references, they work.
I'm pretty sure that previous versions of IE rendered them by the
specifications. I first thought this has something to do with XML (i.e.
maybe IE pretends to play a little bit of XML...
|
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 t e ;" and similar.
The problem is that i use this information in XMLDocument object, I load the
string than contains all the information in this object with the method
..LoadXML(string) and this raises an exception, i think it's because it has a...
|
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 immediate window it return this. Anywhere where I
attempt to use two apostrophes, it inserts a backslash. Even a
function in one method that saves its string to a file shows that 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 code below in the Web.Config file defines the authentication as forms
and the aspx file required for login if the user is unauthenticated... this
works well for the root level company web pages however when a user viewing
the website of SubCompanyA...
| |
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
"registerClientScriptBlock" as follows:
string script = "<script language=jscript>";
script += "function test()";
|
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.
1)<tm:bom-expression>{Conf.getEquityConfLookupFields().getEventFieldText("AdditionalDisruption","Change in Law")}</tm:bom-expression>
2)07:41:08 Default ( call ( . ( call ( . Conf getEquityConfLookupFields ) ) getEventFieldText ) ( , AdditionalDisruption Change inLaw ) ) value=Not applicable
Can you please...
|
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 "" instead of "" otherwise I get an error message.
Can anyone suggest how to write it so that it writes " instead of "".
I have tried all combinations of adding " to the code but as soon as I think I am there I get throw out again.
if...
|
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 an XSLT sheet that
transforms this text into a sql insert statement. UNFORTUNATELY, XSLT
translates doesn't into doesn't in the output . How do I tell
my XSLT to leave well-enough alone ? I know it has to be easy, but I
just don't get it...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |