Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » User input at runtime??
User input at runtime?? [message #128532] Mon, 09 January 2006 17:24 Go to next message
Eclipse UserFriend
Originally posted by: asdad.asd.com

I have this in a java file:

public class Fibonacci{
public static void main (String[] args) {
int theNum, theFib;

System.out.println("This program computes the kth " +
"Fibonacci number.");
System.out.print("Enter the number k: ");
theNum = new Integer(args[0]).intValue();


but to get input in theNum I need to specify it under Run...Is there no way
to enter input when I run the program?
Re: User input at runtime?? [message #128545 is a reply to message #128532] Mon, 09 January 2006 17:37 Go to previous messageGo to next message
Jeff Myers is currently offline Jeff MyersFriend
Messages: 489
Registered: July 2009
Senior Member
You're reading the command line argument arg[0] as input. If you want to
read console-based input at runtime you'll have to use System.in.read()

- Jeff
Re: User input at runtime?? [message #128558 is a reply to message #128545] Mon, 09 January 2006 18:10 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: asdad.asd.com

Jeff Myers wrote:

> You're reading the command line argument arg[0] as input. If you want to
> read console-based input at runtime you'll have to use System.in.read()
>
> - Jeff

I can now type something in the console but it never gets used:

import java.io.IOException;

public class Fibonacci{
public static void main (String[] args) throws IOException {
int theFib, theNum;

System.out.println("This program computes the kth " +
"Fibonacci number.");
System.out.print("Enter the number k: ");
theNum= System.in.read();

theFib = fib(theNum);

System.out.println("The " + theNum + "th Fibonacci " +
"number = " + theFib + ".");
}

/**
* Recursively calcualte the kth Fibonacci number.
*
* @param k indicates which Fibonacci number to compute.
* @return the kth Fibonacci number.
*/
static int fib(int k) {

// Base Case:
// If k <= 2 then fib(k) = 1.
if (k <= 2) {
return 1;
}
// Recursive Case:
// If k > 2 then fib(k) = fib(k-1) + fib(k-2).
else {
return fib(k-1) + fib(k-2);
}
}
}
Re: User input at runtime?? [message #128582 is a reply to message #128558] Mon, 09 January 2006 18:45 Go to previous message
Eclipse UserFriend
Originally posted by: nobody.there.127.0.0.1

Paminu wrote:
> theNum= System.in.read();
>
> theFib = fib(theNum);

This almost certainly isn't what you intend to do. I'd recommend a
closer look at
http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream. html#read()
- a good Java book should be able to give you more details on its use.

Andrew Jr.

============================================================ ==========
IBM Phoenix Labs (OTI)
2929 North Central Avenue
Phoenix, Arizona, USA 85012
Previous Topic:How to debug a C/C++ executable that is compiled outside of Eclipse?
Next Topic:Quick Fix Modification Help!
Goto Forum:
  


Current Time: Mon Jul 08 16:29:37 GMT 2024

Powered by FUDForum. Page generated in 0.08881 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top