Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Why the "for" isn't working here?

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Why the "for" isn't working here?

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
     
    public class Media {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		InputStreamReader input = new InputStreamReader(System.in);
    		BufferedReader tastiera = new BufferedReader(input);
    		int eta1 = 0, eta2 = 0, eta3 = 0;
    		int media;
    		int flag = 0;
    		String persona1 = null, persona2 = null, persona3 = null;
     
    		System.out.println("******* Programma per il calcolo dell'età media fra 3 persone *******");
    		for(flag = 0;flag == 1;){
    			System.out.print("\nInserisci il nome della prima persona: ");
    			try{
    				persona1 = tastiera.readLine();
    			}
    			catch(IOException e) {
    				System.out.println("Errore nella digitazione del nome, riprovare.");
    				flag = 1;
     
    			}
    		}
     
    		for(flag = 0;flag == 1;) {
    			System.out.print("\nInserisci l'età del soggetto: ");
    			try{
    				String numeroLetto = tastiera.readLine();
    				eta1 = Integer.valueOf(numeroLetto).intValue();
    			}
    			catch(Exception e) {
    				System.out.println("Errore nella digitazione dell'età, riprovare.");
    				flag = 1;
    			}
    		}
     
    		for(flag = 0;flag == 1;) {
    			System.out.print("\nInserisci il nome della seconda persona: ");
    			try{
    				persona2 = tastiera.readLine();
    			}
    			catch(IOException e) {
    				System.out.println("Errore nella digitazione del nome, riprovare.");
    				flag = 1;
    			}
    		}
     
    		for(flag = 0;flag == 1;) {
    			System.out.print("\nInserisci l'età del soggetto: ");
    			try{
    				String numeroLetto = tastiera.readLine();
    				eta2 = Integer.valueOf(numeroLetto).intValue();
    			}
    			catch(Exception e) {
    				System.out.println("Errore nella digitazione dell'età, riprovare.");
    				flag = 1;
    			}
     
    		}
     
    		for(flag = 0;flag == 1;) {
    			System.out.print("\nInserisci il nome della terza persona: ");
    			try{
    				persona3 = tastiera.readLine();
    			}
    			catch(IOException e) {
    				System.out.println("Errore nella digitazione del nome, riprovare.");
    				flag = 1;
    			}	
    		}
     
    		for(flag = 0;flag == 1;) {
    			System.out.print("\nInserisci l'età del soggetto: ");
    			try{
    				String numeroLetto = tastiera.readLine();
    				eta3 = Integer.valueOf(numeroLetto).intValue();
    			}
    			catch(Exception e) {
    				System.out.println("Errore nella digitazione dell'età, riprovare.");
    				flag = 1;
    			}			
    		}
     
    		media = eta1 + eta2 + eta3 / 3;
    		System.out.println("\n\nL'età media tra " + persona1 + ", " + persona2 +" e " + persona3 + " è = " + media);
    		System.out.println("****** Programma terminato ******");
    	}
     
    }
    Last edited by Alex90ar; November 21st, 2019 at 08:49 AM. Reason: Bad format used

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why the "for" isn't working here?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Which for statement is the problem? I see several for statements in the code.

    Read the tutorial on for statements: https://docs.oracle.com/javase/tutor...bolts/for.html
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why the "for" isn't working here?

    Quote Originally Posted by Norm View Post
    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Which for statement is the problem? I see several for statements in the code.

    Read the tutorial on for statements: https://docs.oracle.com/javase/tutor...bolts/for.html
    Hi Norm,
    thanks for the suggest.

    Honestly, I guess the problem are all the "for", when I try to run the code, the program goes directly to the end:

                    media = eta1 + eta2 + eta3 / 3;
    		System.out.println("\n\nL'età media tra " + persona1 + ", " + persona2 +" e " + persona3 + " è = " + media);
    		System.out.println("****** Programma terminato ******");

    Without even enter in one of the statements, I'm using Eclipse and I get no error on the syntax of the code.

    What could be the problem? I really can't get it.

    Thank you

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why the "for" isn't working here?

    Did you read the tutorial about the for statement? It has three sections:
    for (initialization;
    termination;
    increment) {
    statement(s)
    }

    The initialization expression initializes the loop; it's executed once, as the loop begins.
    When the termination expression evaluates to false, the loop terminates. <<<<<<<<<< Look at flag == 1 in the code when is that true?
    The increment expression is invoked after each iteration through the loop
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why the "for" isn't working here?

    Quote Originally Posted by Norm View Post
    Did you read the tutorial about the for statement? It has three sections:
    I was going out mad for this…
    I noticed it later, the statement had to be different, now seems working in the proper way!

    Thank you very much, I will learn from this error!

Similar Threads

  1. Replies: 1
    Last Post: August 20th, 2019, 07:07 AM
  2. Replies: 4
    Last Post: July 18th, 2014, 02:04 AM
  3. Replies: 1
    Last Post: July 16th, 2014, 04:16 AM
  4. Replies: 2
    Last Post: May 22nd, 2014, 01:17 PM
  5. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM