for and while loops

Hello, im new to java, and now I have an assignment. I need to analyze a phone number like this-> (656) 345 6544 to 6563456544. I wrote the following code which works very well with a while loop. Now, I would like to know how can I make it work with a loop for.

and the code is
import java.io.*;
public class Tema
{

  public static String removeChar(String str, int i)
  {
    String first= str.substring(0,i);
    String last= str.substring(i+1,str.length());
    str=first+last;
    return str;
  }
  public static String cautaTrimite(String str)
  {
     /*for(int i=0; i<str.length(); i++)
    {
       if ((str.charAt(i)==' ')  ||(str.charAt(i)=='5')  ||(str.charAt(i)=='(')
          ||(str.charAt(i)==')') ||(str.charAt(i)=='-'))
          {
          str = removeChar(str,i);
          }*/
       int i=0;
        while(i<str.length())
         {
             if(( str.charAt(i)== ' ' )||( str.charAt(i)== '8' )
                     ||(str.charAt(i)==')')||(str.charAt(i)=='('))
      {
        str = removeChar(str,i);
      }   
            
      else
      {
        i++;
      }

         }
    
  return str;}
  public static void main (String [] args)
  {
  try{
     BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
     System.out.println("Introduceti un numar de telefon oarecare: ");
     String str= in.readLine();
     
     str= cautaTrimite(str);
     System.out.println(str);
  
     }
  catch(IOException e){System.out.println("S-a produs o eroare de IO");}
  }
}
Published by: 1002443 on 30-Apr-2013 05:17

In the while loop, you have full control on when you increment the i and when not, and what you're doing is correct.
Do this with the because the loop always increments the i. To avoid this, you can discover that when you removeChar, you decrement the i, just to increment the loop right after.
Everything in that this would mean by code more obscure all loop, so I wouldn't use it.

Edit: Why you remove 8 in the while loop and 5 in the loop remains a great mystery.

Published by: baftos on April 25, 2013 11:56

Tags: Java

Similar Questions

  • loop for and while loop with empty table entry

    Hello

    I have a question with loop and loop.

    When a constant empty array (zero element) is connected to the loop For with "allowing the index", there are no interactions performed in loop For. But, if the loop is replaced by any loop, no problem.

    LabVIEW 2010

    Hello

    It is ok. I have no problem at all.

    For the 'loop' For when you connect the table thanks to indexing, the number of iterations is set to the size of the array. The iteration number assigned to N (in your case 10) is ignored.

    For the 'While' loop the number of iteration is defined by the Boolean Condition and the size of the array is ignored.

    Paul

  • Time for a while loop to run once

    Hello guys,.

    I want to measure the time for a while loop to run once. There is a piece of code raised. So I just created a simple VI to try, please let me know which is the right way to do it?

    And I wondered, when I run the VI without highlighting the execution, he wouldn't give me a number, maybe it's because the code is simple and really fast? I have to highlight all the time?

    Thank you

    Not quite right.  Both get primitive value time will run at the same time.  use an image sequence to force the order of execution, as shown.  I also brought in the relitve of accuracy seconds vi of VI. LIB\utilities because it depends on the clock of the system rather than the mSec timer accuracy.

  • How to create a control of time for the while loop?

    Hi all

    I want to control my time running of the loop. In other words, I want to stop everything in the execution of the loop after a time (in ms) that is defined by the user. For example user defines Control Panel for 5 seconds before a while loop runs and stops after 5 seconds.

    I don't know is it possible with the while loop. I managed with the timed loop, but this must be wrong with my application. Y at - it of the other loops for timed control if it is not possible with while loop? There is also a simple loop with .vi random number (0-1) as an attachment.

    Best regards

    Jick

    Hi again,

    I managed to solve my problem right after I posted my first message. I did it with the passage of time and the register shift (for the elapsed time of reset).

    Thanks for your replies anyway!

    Best R

    Jick

  • VI is paused and while loop has highlighted

    I'm in parallel while loops and for some reason when it should start to run loops, it pauses for the VI and highlights one loops. It was not last week when I ran it so I don't know what is the issue. I've included a screenshot of the beginning of the code, the loop that is highlighted is the down right. Also, what is the red dot on the wire in error right after the structure of sequence?

    Thank you

    lvuser333 wrote:

    I'm in parallel while loops and for some reason when it should start to run loops, it pauses for the VI and highlights one loops. It was not last week when I ran it so I don't know what is the issue. I've included a screenshot of the beginning of the code, the loop that is highlighted is the down right. Also, what is the red dot on the wire in error right after the structure of sequence?

    Thank you

    Someone snuck into a breakpoint on you.  The red dot is the stopping point, and that's why your VI made a break.

  • event structure and while loop iteration

    Hello

    It took me time to identify the problem I have, but I think I've nailed it and need help to solve.

    I am right to say that a while loop that contains a structure of the event will travel only once and then wait for another event to occur before a loop again?

    The question I have is that I need mark to a timestamp of a data acquisition driver (DataTaker) value, and whenever he changes the Labview event structure would take care of the rest of the incoming data.

    So is there a way to make everything in continuous loop on questioning my value of timestamps from my Datataker driver so that I can create an event Value (Signaling) for the Structure of the event to take care of?

    Thank you

    Richard

    Two options.

    1 put the stuff event to query a regular base in its own while loop that runs in parallel to the loop containing the structure of the event.

    2 put the stuff that you need mark in the case of delay of the structure of the event and a thread time-out value in the upper left corner of the structure.  Now, if no event to occur in this time, the structure of the event will run the case of timeout and the loop will iterate again.  The only problem with this method is that the case of timeout to run irregularly, or never at all if it turns out that events come more frequently than the timeout value.

  • Serial port visa and while loop

    Hello

    I am trying to acquire data of a device through serial communication. This is an RS232 interface. I use a port of visa and I know open and close the port of visa again and again while the program is running is bad style. So, I trieed to use a while - excluding the Visa open loop and nearby (see the attachment of a second, program 2). Unfortunately this work of isn´t program and I can´t find the bug. However the first attachment (program 1) works properly (except that the speed is maybe more low). Sombody could me to get the second work programme.

    Thank you!

    Best regards

    Soffi

    What you need to do is to add a register to offset for the name of resource VISA and error. See attachment a screenshot.

    I couldn't find this anywhere in the examples of NEITHER. (This one is based on the basic series example write and read vi.) When I started with LV, I asked tech support how to do this, and they told me. I WISH THEY WOULD SHARE THIS TECHNIQUE WITH USERS IN THE UNITED STATES!

    I hope this helps. (I can reach the vi if you wish.)

    Ed

  • a clear indicator for the while loop

    Hello guys,.

    I try a very easy application to show a number of iteration (or County by second time encoder) and show a partial and total on two indicators.

    I would like to reset the indicator and the card when I press a button 'clear', but the only clean is the card, while the indicator reset to zero quickly (with reset to the default value), but a souvenir of the iteration made up for the moment.

    What type of solution you can suggest me? ) I tried to use the registry to offset, structure of the case, but I can't find a solution

    Thanks again for your valuable help or suggest...

    Here, I posted my VI

    This is the same thing I did with your counter stuff.

  • Save all the data for a while loop

    I already asked a similar question, but it is perhaps not very clear, and there are a few concepts that I did not understand.

    1. I have a keyboard to generate flow DTMF signal (I call it stream because it contains several shades that consist of a full phone number)

    2. I would like to save it as a wav file, but I guess that another format is correct and because the writing on a wav file does not support add so for this particular case, I use writing waving instead of writing to wav format

    My approach is to have a structure of queue so that I can write each iteration results in a file.

    (i) when I try to retrieve the data, there's nothing but 0.

    (II) my intention is to get 8000 samples per second (fs = 8 000) but he back up data so fast

    I have attached the file so if there is a way to solve problems, please help me.

    This vi includes many elements discussed above. There are no provisions for the intervals 'no signal '.

    Changes: Replaced with native features of LabVIEW Mathscript node. Mechanical Action to latch release button.  Cluster to array replaces Unbundle and table to build. Reshape the table to match the shape of the keyboard. Separate searches for frequencies of line and column. Note that this is easily extensible to handle the 4 x 4 keyboard. Structure of the event added. Added the stop command (but not implemented stop to the loop of the file). Added indicators for diagnostic purposes. Added FFT spectrum and graphic.

    I disabled writing to part of the file for testing.

    Lynn

  • Y at - it an easy way to highlight the current iteration of a currently running on the front panel of a for or while loop?

    Y at - it an easy way to show on the Panel before which iteration of a loop is running during the time the loop runs.

    Thank you

    Chuck M.

    Unless I'm missing something...

  • Series of reading and DAQ in same While loop

    Hello world

    I have a while loop that has a DAQ reading and a reading series of VISA.

    DAQ readings slow while loop for series com & therefore the iterative nature of the measures to constantly check byte-by-byte (for the full range of the code: 22 bytes) does not work. (it will check 8 bytes, 24,.. etc. because more bytes is collected in a buffer before it can browse new loop.)

    Is it possible to synchronize these two data acquisition devices?

    Data acquisition speed is fast enough, so if there is a way to keep the series bytes in a queue and take only the first 22 bytes for every while loop iteration?

    Thank you for your help. I can't join VI, but I can give you a reference image.

    I recommend having separate loops of data acquisition and reading series.  If you simply connect the latest value, the local variable is great for sending to playback in series to the other loop for logging.

  • While loop and table

    I'm weak and I can't get up.
    I'm playing a series of clips in order, so movieClip2 does not play until the end of movieClip1 (label = 'end'). I started with a three separate movieClips, each with its own listening port. Because all three looked like, I thought me save time by using a table and while loops. I have a dozen of these total movieClips, then rather than cut and paste each individual listener and if statement, it seems that there is a better way. And you smart people are the people who know. The output I am receving is: TypeError: Error #1006: value is not a function.

    Thanks in advance for any direction.

    I tried to use another method successfully. In my previous attempt, I used too many while and loops enterFrame, which caused problems of circular logic. Using only a few statements seem to solve the problem, as below. Thank you all for taking a glance at the problem.

  • How to run a while loop continuously without iterate loop inside the fi, a condition is met

    I enclose a VI that once the answer 28 comes the loop must stop and loop should continue an iteration. Can you show me a way for her

    You can stop the loop when a certain condition occurs: http://zone.ni.com/reference/en-XX/help/371361H-01/lvhowto/add_cond_to_for_loop/

    Add a register to offset for the while loop and to implement the feature as below:

  • force stop while loop

    Hello

    I'm writing a VI that make a transaction within a while loop. I replaced the operation with a simple mathematical operation in the attached photo of the VI. For loop in the picture, I just want to add 1 every 15 secs until it reaches 10 and stops. However, I also want to be able to force the while loop to stop immediately all 15 sec until the next iteration. Is there a way out of the waiting time for the while loop? Thanks for the help!

    Altenbach says:

    Use the event with a timeount 15000ms structure and an event for the stop button. Urgent stop immediately interrupts the time-out.

    Here's what I had in mind (LV 8.5).

  • How to get out of a while loop?

    How can I get out a while loop?

    I'm new to Labview and I tried to make my first Labview program.

    I took an old example, a sieve to primes. The old Erasthotenes sieve.

    Similar to (Visual Basic example):

    While (l<= k)="" and="" teiler=""><=>

    Teiler = TeilerArray (l)

    Hans,

    What you linked is for the loop FOR, that I had attached is for the WHILE loop.

    Only the type of the loop is different, but the concept or the logic is the same for both.

Maybe you are looking for