Docsity

Java: Processing Input and Parameters - Command Line Input and StringTokenizer, Study notes of Java Programming

Toggle sidebar
bg1
1
© 2005 Sarah E. Smith
Processing Input, Parameters, and
Return Values
CSC 116 – Section 002
March 14, 2005
© 2005 Sarah E. Smith
Review of Command Line Input
• Use BufferedReader and InputStreamReader
classes to read input
• Use System.in to specify that input comes
from standard input
• Syntax:
BufferedReader console =
new BufferedReader(new InputStreamReader(System.in));
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Java: Processing Input and Parameters - Command Line Input and StringTokenizer and more Study notes Java Programming in PDF only on Docsity!

© 2005 Sarah E. Smith

Processing Input, Parameters, and

Return Values

CSC 116 – Section 002 March 14, 2005

© 2005 Sarah E. Smith

Review of Command Line Input

  • Use BufferedReader and InputStreamReader

classes to read input

  • Use System.in to specify that input comes

from standard input

  • Syntax: BufferedReader console = new BufferedReader(new InputStreamReader(System.in));

© 2005 Sarah E. Smith

Review of Command Line Input (2)

  • Use readLine() method to read a line from the terminal window
  • readLine() method may cause exceptions so wrap in a try-catch block try { String input = console.readLine(); } catch (IOException e) { System.out.println(“Error: “ + e); }

© 2005 Sarah E. Smith

Reading More Than One Line

  • You can use a BufferedReader to read a list

of input by using a while loop

© 2005 Sarah E. Smith

StringTokenizer Methods

  • StringTokenizer(String line)
    • Constructor
  • StringTokenizer(String line, String delimiters)
    • Tokenizes the String in line with the given delimiters
  • StringTokenizer.hasMoreTokens()
    • Returns true if the StringTokenizer has more tokens
  • StringTokenizer.countTokens()
    • Counts the remaining tokens
  • StringTokenizer.nextToken()
    • Returns a String that contains the next token

© 2005 Sarah E. Smith

StringTokenizer Example

StringTokenizer tokenizer = new

StringTokenizer(line);

int tokenCount = tokenizer.countTokens();

for (int i=0; i < tokenCount; i++) {

String token = tokenizer.nextToken();

System.out.println(token);

© 2005 Sarah E. Smith

StringTokenizer.countTokens()

  • Return the number of tokens that are left in

the Tokenizer

  • Whenever a token is retrieved from the list, it

is deleted!

  • Do not use this method in a for loop
    • Create a variable to store the total number of tokens before processing the tokens

© 2005 Sarah E. Smith

Formal and Actual Parameters

  • Formal Parameters – include this and all declared parameters (the data types and names in the parenthesis)
  • Actual Parameters – values that you supply a method
  • Actual parameters copy their values to the formal parameters memory locations - this gets the location in memory of the calling object - The parameter gets the value passed into it

© 2005 Sarah E. Smith

Execution Flow Example

amt=yConvert.fromD(200);

Memory

public double fromDollar(double d) { double amt, fee; fee = er – fr; amt = d * fee; return amt; }

amt

fee

amt

d 200.

© 2005 Sarah E. Smith

Execution Flow Example

amt=yConvert.fromD(200);

Memory

public double fromDollar(double d) { double amt, fee; fee = er – fr; amt = d * fee; return amt; }

amt

fee 124.

amt 24846.

d 200.

© 2005 Sarah E. Smith

Execution Flow Example

amt=yConvert.fromD(200);

Memory

public double fromDollar(double d) { double amt, fee; fee = er – fr; amt = d * fee; return amt; }

amt 24846.

© 2005 Sarah E. Smith

Execution Flow Example (2)

int x = 10; int y = 20; tester.myMethod(x,y);

Memory

public void myMethod (int one, double two) { one = 25; two = 35.4; }

y 20

x 10

© 2005 Sarah E. Smith

Execution Flow Example (2)

int x = 10; int y = 20; tester.myMethod(x,y);

Memory

public void myMethod (int one, double two) { one = 25; two = 35.4; }

y 20

x 10

© 2005 Sarah E. Smith

References

  • Jason Schwarz’s Lecture 14 and 15 slides:

http://courses.ncsu.edu/csc116/

  • Example of execution flow from Chapter 4.

in Wu p 172 and 174