Java For loop validation simple

I have a for loop that put together a sentence. I want to validate that the user does not enter a number and if they do it retries at that part of the sentence.  So if they are at "enter a verb" and the user enters 5 it will ask them again to enter a verb.  Once they do it correctly they move on.  I have tried everything, but I am new to java.  The code is below from the main

  public static void main(String[] args)
    {
       boolean val = false;
       boolean val1=false;
     
       Scanner scan = new Scanner(System.in);
       String[] sent = {"Enter in your subject","Enter in your Verb","Enter in your adjective","Enter in your object","Enter in your Adverb"};
       String[] userSent = new String[sent.length];
       System.out.println("We will be making a sentence together.  Please enter in the following information:");
   
 
        for (int i=0;i<sent.length;i++)
        {
            System.out.println(sent);
             userSent = scan.nextLine();
   
             }
       
 
       for (String s: userSent)
       {
           System.out.printf("%S",s+" ");
       }
       
    }
}
aggiestone99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
You're not far from having the first part working - where you read in the values.

But where you loop through the list of things to ask for you need to use an index when accessing the arrays like this:
	for (int i=0;i<sent.length;i++)
		{
			System.out.println(sent[i]);
			userSent[i] = scan.nextLine();
		}

Open in new window


If you put that part in, you should be able to read all 5 values.  Then you need to write the code to figure out which are correct and which incorrect.

(This looks like a homework problem so we can't help you just write it - but we can help you work through it as you write it).

Doug
0
aggiestone99Author Commented:
I had the [i] but the did not come through. I have tried many different types of validation with using best while and using if inside the for loop. I am stuck and not sure where to look this up. If you can point me in right direction be great. I have banged my head on this for two days and I know it is simple.
0
dpearsonCommented:
OK here's some code to point you in the right general direction.

It will accept anything except if you type the number "5" it will ask you to enter the value again.

Can you see how it works?  And then can you adapt it to do what you need?

Doug

		for (int i=0;i<sent.length;i++)
		{
			boolean tryAgain = true ;

			while (tryAgain) {
				System.out.println(sent[i]);
				userSent[i] = scan.nextLine();

				if (!userSent[i].equals("5")) {
					tryAgain = false;
				}
			}
			
		}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

aggiestone99Author Commented:
Here is what I got.  What I am having problem with now is figuring out if I need to do this in a for loop or another way and what way is the best.  The validation works but I have not figured out how to get the i to not advance once validation fails.

for (int i=0;i<sent.length;i++)
       {
           boolean tryAgain=true;
           boolean inInt = true;
           while(tryAgain){
           System.out.println(sent[i]);
           if(scan.hasNextInt()){
               inInt=true;
           }
           sent[i]= scan.nextLine();
           if(inInt =true) {
               System.out.println("nope");
               tryAgain = false;
           }
           }
       }

Open in new window

Here is my ouput
We will be making a sentence together.  Please enter in the following information:
Enter in your subject
1
nope
Enter in your Verb
0
aggiestone99Author Commented:
Ok I went a different way but cannot get the end result to workout correctly.  Please look at output below.  What can I do to solve this.  I tried use scan.next() but it made me enter the first string twice.

Scanner scan = new Scanner(System.in);
       String[] sent = {"Enter in your subject","Enter in your Verb","Enter in your adjective","Enter in your object","Enter in your Adverb"};
       String[] in = new String[5];
       int i = 0;
       System.out.println("We will be making a sentence together.  Please enter in the following information:");
       
       boolean tryAgain = true;
       while(tryAgain)
       {
           boolean input = true;
        while(input)
        {
          if(i <=4)
          {    
            System.out.println(sent[i]);
           if (!scan.hasNextInt()&&i<sent.length)
           {
            in[i]= scan.nextLine();
            i++;
           } 
                else
                {
               System.out.println("Please do not use numbers");
                input=false;
                scan.next();
                }
          }else 
          {
              input = false;
          }
          }
        if(i>4){ 
        tryAgain = false;
        }else{
            tryAgain=true;
        }
       }
       for (i=0;i<sent.length;i++)
       {
           System.out.printf("%S",in[i]+" ");
       }
       
    }
    
}

Open in new window


Output
We will be making a sentence together.  Please enter in the following information:
Enter in your subject
1
Please do not use numbers
Enter in your subject
1
Please do not use numbers
Enter in your subject
w
Enter in your Verb
Enter in your adjective
0
aggiestone99Author Commented:
Nevermind I got it.  Thank you for your help here is my final solution.
package javaapplication2;

import java.util.Scanner;

public class JavaApplication2 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String[] sent = {"Enter in your subject", "Enter in your Verb", "Enter in your adjective", "Enter in your object", "Enter in your Adverb"};
        String[] in = new String[5];
        int i = 0;
        System.out.println("We will be making a sentence together.  Please enter in the following information:");

        boolean tryAgain = true;
        while (tryAgain) {
            boolean input = true;
            while (input) {
                if (i <= 4) {
                    System.out.println(sent[i]);
                    if (!scan.hasNextInt() && i < sent.length) {
                        in[i] = scan.nextLine();
                        i++;
                    } else {
                        System.out.println("Please do not use numbers");
                        input = false;
                        scan.nextLine();
                    }
                } else {
                    input = false;
                }
            }
            if (i > 4) {
                tryAgain = false;
            } else {
                tryAgain = true;
            }
        }
        for (i = 0; i < sent.length; i++) {
            System.out.printf("%S", in[i] + " ");
        }

    }

}

Open in new window

0
dpearsonCommented:
Excellent - glad you got it working!

Doug
0
aggiestone99Author Commented:
Tell me is there an easier way to solve this?
0
dpearsonCommented:
There's always many ways to solve.

Since you've got a working solution, compare to this one:

		for (int i=0;i<sent.length;i++) {
			boolean tryAgain = true;
			while (tryAgain) {
				System.out.println(sent[i]);

				if (scan.hasNextInt()) {
					System.out.println("Please do not use numbers");
					scan.nextLine();
				} else {
					in[i] = scan.nextLine();
					tryAgain = false;
				}
			}
		}
	}

Open in new window


If this is a homework problem, please submit yours rather than mine for the work.
But it's good to understand how it could be improved.

Doug
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.