473,326 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 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. =)
Nov 17 '10 #2
Nepomuk
3,112 Expert 2GB
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
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...
8
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...
1
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
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...
2
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...
7
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...
3
hedonplay
by: hedonplay | last post by:
Please explain this declaration in details it makes confusing!
1
by: manchin2 | last post by:
Hi, Can anybody please provide the information about "&quot" and its use, if possible please provide an example. ...
4
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...
11
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...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
1
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)...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
0
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
0
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...

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.