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

Username:   Password: 
 RegisterRegister   
 I want to input a number and program makes # of input lines
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Gerkin




PostPosted: Wed May 04, 2005 6:31 pm   Post subject: I want to input a number and program makes # of input lines

availabe. This may seem simple, but I have no idea how to this. I'm gonna use this as part of my project.
Sponsor
Sponsor
Sponsor
sponsor
Hikaru79




PostPosted: Thu May 05, 2005 9:10 pm   Post subject: (No subject)

OK, this is a pretty small, simple thing, so I'll give it to you as a freebie, although it does sound kinda like homework... Wink
Java:

import java.io.*;
public class Test{

public static void main (String [] args){
try{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int numberOfLines;
numberOfLines = Integer.parseInt(input.readLine());
for (int x=0;x<numberOfLines;x++){
System.out.println(" ");
}
}
catch (IOException e){System.out.println(e);}
catch (NumberFormatException f) { System.out.println(f);}
}
}
wtd




PostPosted: Thu May 05, 2005 9:50 pm   Post subject: (No subject)

I'm shocked. Why the separate declaration and initialization?

Java:
import java.io.*;

public class Test
{
   public static void main(String [] args){
      try
      {
         BufferedReader input = new BufferedReader(
            new InputStreamReader(System.in));
         int numberOfLines = Integer.parseInt(input.readLine());

         for (int x = 0; x < numberOfLines; x++)
         {
            System.out.println(" ");
         }
      }
      catch (IOException e)
      {
         System.out.println(e);
      }
      catch (NumberFormatException f)
      {
         System.out.println(f);
      }
   }
}
Hikaru79




PostPosted: Thu May 05, 2005 9:57 pm   Post subject: (No subject)

wtd wrote:
I'm shocked. Why the separate declaration and initialization?

Of 'numberOfLines'? Because I wanted to keep everything as simple as possible, not fight for every last reduced line count Wink No offense to the original poster, but the question isn't exactly all that difficult, so I'm guessing he is a relative beginner to Java at least. Keeping declarations and intializations seperate just seemed like the more intuitive thing to do.

BTW, thank you for indenting and prettying up my code Smile A lot easier on the eyes now!
wtd




PostPosted: Thu May 05, 2005 10:00 pm   Post subject: (No subject)

Hikaru79 wrote:
Keeping declarations and intializations seperate just seemed like the more intuitive thing to do.


Not in Java, or any other language that supports that kind of syntax.
Gerkin




PostPosted: Sun May 08, 2005 5:03 pm   Post subject: (No subject)

What does try and catch do? Sorry for my inadequate knowledge of Java, I blame it on my com sci teacher.
JackTruong




PostPosted: Sun May 08, 2005 7:50 pm   Post subject: (No subject)

Try/Catch statements allow a program to continue running when something catastrophic could happen.

In this case, the try part, runs through the code inside the braces {} and if it finds any errors (IOException, bad input; NumberFormatException, bad number), it goes to code inside the catch braces and executes.
wtd




PostPosted: Sun May 08, 2005 9:28 pm   Post subject: (No subject)

Gerkin wrote:
What does try and catch do? Sorry for my inadequate knowledge of Java, I blame it on my com sci teacher.


Well, there are a couple of basic approaches to handling errors, which is of course important, because errors are inevitable.

We can have functions return error codes indicating whether they failed or succeeded. But this has a few problems. First, it means we can't have a function return anything else, and secondly, it means a lot of extra code. Consider something like the following psuedocode.

code:
int result = open_file("foo.txt");
if (result == 0)
{
   // read from file and close it
}
else
{
   // print an error message
}


The other approach is exceptions. Exceptions allow us to break out of the current level of the program with an identifiable error. In Java, we have a "try" block which "tries" some code. "catch" blocks are then provided to handle specific types of errors.

In the above example we handle either a fairly general IOException, and a NumberFormatException. The latter occurs when a String cannot be converted to an int, in this case.

The beauty of exceptions is that we don't have to directly mix error-handling code with the rest of our code.
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: