1 / 13

Importing and Exporting Data

Importing and Exporting Data. CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 20, 2004. Last update: September 16, 2004. Learning Objectives. What are primitive data types in Java?.

vera-hanson
Download Presentation

Importing and Exporting Data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Importing and Exporting Data CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 20, 2004 Last update: September 16, 2004

  2. Learning Objectives • What are primitive data types in Java? • How to input data via an applet? • How to output data via an applet? Reference: Chapter 6 of the textbook. Course Introduction  Aditya P. Mathur 2004

  3. Primitive data types: byte, short, int, long Java offers the several primitive data types for manipulating real-world data such as integers, floating point, or real, numbers, booleans, and characters. We begin with type int. int Example: int x, y; Variables x and y can assume integer values such as -20, 33, 1290893, 0, 45 There are four kinds of integers, int is one of them. The others are: byte, short, long. Refer to page 164 of the text for details. Course Introduction  Aditya P. Mathur 2004

  4. Primitive data types: double, float Example: double x,y; Variables x and y can assume floating point values such as 0.0, -33.9354, 129.0893, 99.9999 Example: float x,y; Variables x and y can assume floating point values such as 0.0, -33.9354, 129.0893, 99.9999 Variables of type double will be represented with higher accuracy than those of type float. Course Introduction  Aditya P. Mathur 2004

  5. Primitive data type: char Example: char x,y; Variables x and y may assume character values such as ‘p’, ‘*’, ‘+’, ‘/’. Notice that character constants are enclosed inside single quotes as in ‘/’ whereas strings are enclosed inside double quotes as in “hello!”. Also, variables of type String are DIFFERENT from variables of type char. See page 166 of your text for details. Course Introduction  Aditya P. Mathur 2004

  6. Primitive data type: boolean Example: boolean x,y; Variables x and y may assume boolean values true and false. Course Introduction  Aditya P. Mathur 2004

  7. Data input: Input an integer value • While all kinds of data can be input via an applet, care needs to be taken to convert it from String to proper format. Let us understand why. • Consider the following declarations: TextField ageField=new TextField(4); int age; • ageField is an object of type TextField and can be displayed on the screen. Course Introduction  Aditya P. Mathur 2004

  8. Data input: String to int? No way! • We want to type in an integer in the ageField area and assign this value to variable age. Can we do the following? age=ageField.getText(); • The answer is NO because ageField.getText() is of type String whereas age is of type int. We cannot assign a value of type String to a variable of type int. Course Introduction  Aditya P. Mathur 2004

  9. Data input: Convert String to int • To assign the number typed into the ageField to variable age, we need to first convert the string typed by the user to an integer. (Note that whatever is typed by the user is treated as a string by the JVM.) A String object can be converted to an int as follows: • First obtain the string typed by the user: String ageTyped=ageField.getText(); • Next convert the String object to an int value: age=Integer.parseInt(ageTyped); Course Introduction  Aditya P. Mathur 2004

  10. Data input: Convert String to double • Similarly a String object can also be converted to a floating point number as follows. • First obtain the string typed by the user: String weightTyped=weightField.getText(); • Next convert the String object to an double value: Double weight; weight=Double.parseDouble(weightTyped); A String object can be converted to other values of primitive types. See Chapter 7.11 of your text. Course Introduction  Aditya P. Mathur 2004

  11. Data output: Convert int to String • To output an integer value to a TextField or a TextArea object, one needs to first convert the value to a String. The string so obtained is then written into the object. int age; ageField.setText(Integer.toString(age)); • The toString(int i) method takes an integer i as input and returns a string. For example, the following call: Integer.toString(-392) will return the string “-392” Course Introduction  Aditya P. Mathur 2004

  12. Data output: Convert double to String • To output a double value to a TextField or a TextArea object, one needs to first convert the value to a String. The string so obtained is then written into the object. double weight; weightField.setText(Double.toString(weight)); Course Introduction  Aditya P. Mathur 2004

  13. Data output: Convert boolean to String • To output a boolean value to a TextField or a TextArea object, one needs to first convert the value to a String. The string so obtained is then written into the object. boolean fail=false; overWeightField.setText(.toString(fail)); Course Introduction  Aditya P. Mathur 2004

More Related