473,323 Members | 1,589 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,323 software developers and data experts.

Need Java Coding help

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
Nov 29 '07 #1
3 1215
JosAH
11,448 Expert 8TB
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
Nov 29 '07 #2
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!!!
Nov 29 '07 #3
JosAH
11,448 Expert 8TB
@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
Nov 29 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

55
by: Alex | last post by:
Hello people, The following is not a troll but a serious request. I found myself in a position where I have to present a Pro/Con list to management and architects in our company with regard to...
4
by: Rhino | last post by:
I've been playing with Java UDFs for the last couple of days and I've got some questions about scratchpads. I'm running DB2 LUW V8 (FP8) on WinXP. Somewhere in the manuals, I found some remarks...
3
by: Chris | last post by:
Wait.. before you flame.. If someone can program in Java, or Javascript, or C, or (insert your language here that uses basically the same syntax as C#), and that person knew how to program in...
48
by: Chad Z. Hower aka Kudzu | last post by:
A few of you may recognize me from the recent posts I have made about Indy <http://www.indyproject.org/indy.html> Those of you coming to .net from the Delphi world know truly how unique and...
13
by: Andrew Bell | last post by:
I'm doing a uni course in the UK and one of my semesters is about programming. This is just going to be compilied and executed with no menu just using command promt (javac classfile.class) I am...
0
by: systemania.com | last post by:
We have 3 contract positions open in Herndon Virginia for 3 JAVA/J2EE Developers. The contracts are Long Term (1 year ++). The rate is $45/h negotiable based on experience. To apply for this...
6
by: =?Utf-8?B?VkIgSm9ubmll?= | last post by:
ASP.NET 2.0 / Visual Studio 2005 / VB.Net 2.0 I have a web interface that needs to launch a java application. What a long strange trip it has been... I am using Process.Start process start...
7
by: rohitsripathi | last post by:
i need help writing this program... i am in college and i do not have time do this right now as i need to study for another test....but this is due tonight...i will pay $5 if you accept to do it and...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: 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...
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.