Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need help looping....
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
GAT-X303




PostPosted: Wed Nov 14, 2007 12:50 pm   Post subject: Need help looping....

OK, so we just started java yesterday in class, and my teacher just goes, "Write a java program that will accept two (2) numbers from a user. The first number is where the loop begins and the second number is where the loops ends. Display all numbers in between, incrementing the numbers by 12." and then says, go. Now, he hasn't taught us how to do this, so could someone help me out please. I'm like, really new to java.

Thanks.
Sponsor
Sponsor
Sponsor
sponsor
Euphoracle




PostPosted: Wed Nov 14, 2007 3:57 pm   Post subject: RE:Need help looping....

If he hasn't taught you how to do it, you should really see him and ask him how he expects you to do it. TRUST ME! My highschool Java teacher is a PAIN, and if you don't do it her way, you get no marks. She marks it [essentially] from how different it is from her 'answer key' [I assume].

What this question is asking you to do is take 2 numbers from the user (probably through the statement 'BufferedReader br = new BufferedReader(new InputStreamReader(System.in)))' because all the textbooks in highschool like to force you to memorize this) and then create a for-loop using these values. The generic for-loop is created in the following way:

Java:
for (int counter = 0; counter < max; counter++)
{
<code>
}


Explained:

Line 1:
This is the major line where we tell Java how we want our loop to operate. The first statement reads 'int counter = 0'. This creates a new variable within the scope of the loop for the counter. This will be incremented by the incrementor (see later on)
The following statement reads 'counter < max' where max is defined above somewhere to denote how many times our loop will continue for. When this condition is not true, the loop will end. Therefore if we set it to counter != max, there is a possibility that it may never end (as well as with some other options, such as checking if it is < a number and decrementing, etc)
The final statement is our incriminator. This tells Java how we want to modify the value of our counter per iteration. At the moment, we are using the suffix incriminator to increase the value by 1 each iteration ( <variable>++; ). This can be any arithmetic statement (or any statement for that matter). One could do counter += 2, to skip numbers in between, etc.

Line 2:
A brace, used for noting the entrance into the loop's scope.

Line 3 to *:
Your code goes here. The value of counter will only be accessible in here, and you can also modify it in here if you wish to skip, or repeat whatnot, etc. at any time.

Last line:
A brace, used for noting the exit of the loop scope. Nothing below this will execute with this loop.

Application
Now, what you want to do is to take 2 values from the user and use them as your starting and ending values. This can be achieved by using the BufferedReader (stated above) and the Integer library. The basic stencil for a highschool Java 'application' is the following (as forced by my teacher):

Java:
import java.io.*;
class MyLoop
{
public static void main(String args[]) throws java.io.IOException
{
<code>
}
}


Explained:

Line 1:
Imports Java's basic IO library. Although I would prefer not to do this, my teacher forces us to, your teacher may do the same, best stay safe.

Line 2:
Defines our class. In Java, your filename must be the same as your class (in order for your main method to execute).

Lines 3, 5, last - 1 and last:
Braces used to control scope.

Line 4:
This is where most of your code 'must' be in, for most highschool courses and must be memorized. It is the main entry point for a Java application. This is a method (you will learn more about these later [I hope]) and takes a String array (named 'args' in this case) as a parameter. You will also need the throws...Exception statement because teachers don't like to teach in-code error handling with java's nifty try/catch/finally structure.

Line 6:
Your code goes here (see above).

The BufferedReader
In order for your highschool application to run, you are going to be required to use a BufferedReader object (you will hardly learn about why, just 'memorize it' for the time being). To do this, we use the following code:

Java:


This will go inside your main method.

Your Variables
In order to complete this, you will need 3 variables (as 'defined' by my teacher). Two ints named 'start' and 'end' and a string called 'temp' (don't ask why, you'll probably be forced to use it). Do declare these, we use the following code:

Java:
int start = 0, end = 0;
String temp = "";


Getting your input
To get your input, you will require use of the BufferedReader's readLine method. We use it as such:

Java:
temp = br.readLine();


This will create a prompt in the console for keyboard text entry. After doing this, we will convert the string to an int and assign it to the variable 'start'.

Java:
start = Integer.parseInt(temp);


If you do not enter what it wants, this WILL throw an error at you and cause your application to crash--don't worry. Teachers of highschool don't bother teaching you how to prevent this yet. Your best bet as of now is to simply give it the input it wants. Repeat this for 'end'.

Putting your loop together
Now that you have values set, you can begin your loop. Following the basic for-loop structure above, simply replace the '0' in the first statement with 'start' and end in the second statement stays the same. You will also have to change the incrementing statement from 'counter++' to 'counter += 12' or similar. Inside the loop, you will want to add the following code:

Java:
System.out.println("The current value of the counter is:  " + counter + ".");


This will print a message with the number incrementing every loop by 12, as the question requires. I trust you can put this together yourself, and for the sake of preventing you from a simple copypaste of the final work, I leave you to complete the assignment with these tidbits of information.
HeavenAgain




PostPosted: Wed Nov 14, 2007 6:48 pm   Post subject: RE:Need help looping....

I think they uses Scanner class now, BufferedReader still works, but its not "up to date?"

to use Scanner you can do this
Java:
import java.util.*;
class Hi
{
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    String line = input.nextLine();
    System.out.println("you entered "+ line);
   }
}
Euphoracle




PostPosted: Wed Nov 14, 2007 11:04 pm   Post subject: RE:Need help looping....

We stil use BufferedReader Mad
HeavenAgain




PostPosted: Wed Nov 14, 2007 11:11 pm   Post subject: RE:Need help looping....

well, with Scanner you could read files, as well as the standard input
and plus theres way more methods for Scanner than BufferedReader, and plus, you can not only read String, you can read int, double, etc.... good stuff! Cool
and thiss im not so sure, i think it takes up less space in memory, and in java every little bit helps Very Happy
change your stlye to Scanner before its too late!!!!
Euphoracle




PostPosted: Thu Nov 15, 2007 4:04 pm   Post subject: RE:Need help looping....

Same with BufferedReader, and you can just parse them, but meh. My teacher forces it, I have no option, dictatorship, etc.
wtd




PostPosted: Thu Nov 15, 2007 7:12 pm   Post subject: RE:Need help looping....

Try reading the Introduction to Java. I'm pretty sure I covered loops in there somewhere.
GAT-X303




PostPosted: Fri Nov 16, 2007 11:50 am   Post subject: RE:Need help looping....

I did read it, but it wasn't completely what I was looking for, however, some of the other arts were helpful in other things I was doing.

To everyone else, thank-you for the help. My teacher encourages us to simply get it done however we can make it work, and said to go find more libraries for java if we wanted to. I'm trying out how to do these things right now.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: