473,597 Members | 2,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create a loop that exits when I press enter twice?

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.

Expand|Select|Wrap|Line Numbers
  1. private void run()
  2.  
  3.   {
  4.  
  5.     FileOutputStream outputStream;
  6.     PrintWriter printWriter;
  7.     String fileNam;
  8.     String input;
  9.  
  10.     System.out.println("Creating new backupfile");
  11.     System.out.println("Enter file name: ");
  12.     fileNam = Genio.getString();
  13.     System.out.println("Input: ");
  14.     input = Genio.getString();
  15.  
  16.     try
  17.  
  18.     { 
  19.  
  20.         outputStream = new FileOutputStream(fileNam);
  21.         printWriter = new PrintWriter(outputStream);  
  22.  
  23.       while(true)
  24.          {
  25.         printWriter.println(input);
  26.  
  27.         if(input.equals(""))
  28.           {
  29.           printWriter.flush();
  30.           printWriter.close();
  31.         }
  32.  
  33.       }
  34.     }
  35.  
  36.     catch(Exception e)
  37.  
  38.     { System.out.println("File cannot be written to"); }
  39.  
  40.     System.out.println("backup finished");
  41.  
  42.     }
  43.  
Nov 17 '10 #1
2 4833
Jose Diaz
5 New Member
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. =)
Nov 17 '10 #2
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.
  1. 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.
  2. 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:
    Expand|Select|Wrap|Line Numbers
    1. int i=0;
    2. while(true) {
    3.    i++;
    4.    println(i);
    5.    if(i>100) break;
    6. }
    That will print the numbers 1 - 100 and then stop.
  3. 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:
    Expand|Select|Wrap|Line Numbers
    1. boolean pressedOnce = false;
    2. while(true) {
    3.   /*
    4.   put code for reading input here
    5.   */
    6.   if(input.equals("")) {
    7.     if(pressedOnce)
    8.       break;
    9.     else
    10.       pressedOnce = true;
    11.    }
    12.   else {
    13.     pressedOnce = false; // leave this out, if you don't want to limit yourself to two empty lines directly after each other
    14.     /*
    15.     whatever you want to do if the input was not an empty line
    16.     */
    17.   }
    18. }
  4. If you're not stuck with the while(true) thing, you could also do something more sophisticated, like this:
    Expand|Select|Wrap|Line Numbers
    1. boolean pressedOnce = false;
    2. while((input = Genio.getString) != "" || !pressedOnce) {
    3.   if(input.equals("")) {
    4.     /*
    5.     similar to before
    6.     */
    7.   }
    8. }
    (Untested, but it should work something like that.)
OK, that's all I can think of right now.

Greetings,
Nepomuk
Nov 22 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
2182
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...
8
1915
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...
1
2351
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
2
1553
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
2
5351
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...
7
3283
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()";
3
1482
hedonplay
by: hedonplay | last post by:
Please explain this declaration in details it makes confusing!
1
2831
by: manchin2 | last post by:
Hi, Can anybody please provide the information about "&quot" and its use, if possible please provide an example. 1)<tm:bom-expression>{Conf.getEquityConfLookupFields().getEventFieldText(&quot;AdditionalDisruption&quot;,&quot;Change in Law&quot;)}</tm:bom-expression> 2)07:41:08 Default ( call ( . ( call ( . Conf getEquityConfLookupFields ) ) getEventFieldText ) ( , AdditionalDisruption Change inLaw ) ) value=Not applicable Can you please...
4
3879
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 &quot;&quot; instead of "" otherwise I get an error message. Can anyone suggest how to write it so that it writes &quot; instead of "". I have tried all combinations of adding &quot; to the code but as soon as I think I am there I get throw out again. if...
11
1901
by: LionelAndJen | last post by:
I have an XML file that has a comment field in which the data provider, very kindly, already uses "&quot;" when writing "doesn't", I have doesn&apos;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&apos;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...
0
7885
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,...
0
8271
Oralloy
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...
0
8380
jinu1996
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...
0
6686
agi2029
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...
0
5426
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();...
0
3881
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...
0
3923
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2399
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
0
1231
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.