Connecting Tech Pros Worldwide Forums | Help | Site Map

Need Java Coding help

Newbie
 
Join Date: Nov 2007
Posts: 4
#1: Nov 29 '07
This is my assignment:
Write a method called validate that accepts three integer parameters. The first two parameters represent a range, and the purpose of the method is to verify that the value of the third parameter is in that range. You may assume that the first parameter is less than or equal to the second. If the third parameter is not in the specified range, the method should prompt the user and read a new value. This new value should be tested for validity as well. The method should only return to the calling method once a valid value has been obtained, and it should return the valid value.

This is what I have so far:

{
public int validate (int x, int y, int z)
{
while((z < x) || (z > y))


return z;
}
}

I am really bad in Java and have no idea if I am heading in the right direction on this, but how would I make it so I can return the z? Any help would be appreciated

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Nov 29 '07

re: Need Java Coding help


Quote:

Originally Posted by weemanbran

This is my assignment:
Write a method called validate that accepts three integer parameters. The first two parameters represent a range, and the purpose of the method is to verify that the value of the third parameter is in that range. You may assume that the first parameter is less than or equal to the second. If the third parameter is not in the specified range, the method should prompt the user and read a new value. This new value should be tested for validity as well. The method should only return to the calling method once a valid value has been obtained, and it should return the valid value.

This is what I have so far:

Expand|Select|Wrap|Line Numbers
  1. {
  2.     public int validate (int x, int y, int z)
  3.     {
  4.         while((z < x) || (z > y)) {
  5.  
  6.             // body here
  7.         }
  8.         return z;
  9.     }
  10. }
  11.  
I am really bad in Java and have no idea if I am heading in the right direction on this, but how would I make it so I can return the z? Any help would be appreciated

Your control flow logic is fine (assuming x <= y); all you have to do now in the
body of the while loop is tell the user that z is incorrect and read a new value
for it. Check the Scanner class and read the appropriate article in the
Howtos section (it's all in there).

kind regards,

Jos
Member
 
Join Date: Nov 2007
Posts: 52
#3: Nov 29 '07

re: Need Java Coding help


Your method should look like this:
Expand|Select|Wrap|Line Numbers
  1. {
  2.     public int validate (int x, int y, int z)
  3.     {
  4.         while((z < x) || (z > y)){
  5.              ask user for input;    
  6.              store input in z    }
  7.            return z;
  8.     }
  9. }
  10.  
to ask user for input try this...
Expand|Select|Wrap|Line Numbers
  1. BufferedReader dataIn = new BufferedReader(new
  2. InputStreamReader( System.in) );
  3. String input = "";
  4. System.out.print("Please Enter something:");
  5. input = dataIn.readLine();
  6. int j = Integer.parseInt(input);
  7.  }
  8.  
remember to import the java IO classes!!!
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Nov 29 '07

re: Need Java Coding help


@MarshMallow: hi, you're new here and (accidentally?) you're close to the edge ;-)
We don't handout spoonfed code in this forum; also please read the Help link
near the top right corner of this page. Pay special attention to the Posting Guide-
lines paragraph.

kind regards,

Jos
Reply