braju.com Java fprintf, printf, sprintf (fscanf, scanf, sscanf)

 

Home

Download
Purchase
History
Examples
Documentation
API/javadoc
FAQ
Feedback
About

Donate

Java scanf

A lot of scanf examples

In this article I will list several commonly used scanf() code snippets. They will mostly be self-explanatory and I will not give than comments about the code.

Note: Since scanf() is currently in a beta development phase, all classes is placed under com.braju.beta. When they are matured and well tested they will be upgraded to com.braju. Both scanf() and printf() make use of a new internal class structure which means that also printf() undergoes a beta phase and are hence also placed under com.braju.beta. If you have the stable Java printf package installed, this means that you can access its Format.printf(...) method by explicitly write com.braju.Format.printf(...).

Square of two values

 1 import com.braju.beta.format.*;
 2 import com.braju.beta.lang.*;
 3 
 4 class CalculateSquare {
 5   public static void main(String args[]) throws Exception {
 6     Parameters p = new Parameters();
 7     NumberVariable iV = new IntegerVariable();
 8     Format.printf("Enter a number: "); 
 9     Format.scanf("%i", p.add(iV)); 
10     int number = iV.intValue();
11     Format.printf("The square is %i.\n", p.add(number*number));
12   }
13 }

Enter a number: 12
The square is 144.

ASCII Code Example

 1 import com.braju.beta.format.*;
 2 import com.braju.beta.lang.*;
 3 
 4 class ASCIICode {
 5   public static void main(String args[]) throws Exception {
 6     Parameters p = new Parameters();
 7     NumberVariable iV = new IntegerVariable();
 8 
 9     Format.printf("Enter an ASCII code and I will show\n"+
10                   "you the character it represents: "); 
11     Format.scanf("%i", p.add(iV));
12     int asc = iV.intValue();
13     if (asc< 32 || asc> 126) { 
14       Format.printf("Not a printable ASCII code.\n"); 
15       System.exit(-1); 
16     }
17     Format.printf("The character is '%c'.\n", p.add(asc)); 
18   }
19 }

Enter an ASCII code and I will show
you the character it represents: 65
The character is 'A'.

References
[1] CS 1233. Computer Programming with C, Mississippi State University, USA, 2001, http://www.cs.msstate.edu/~cs1233/.

Degrees in Fahrenheit to Celsius Converter

 1 import com.braju.beta.format.*;
 2 import com.braju.beta.lang.*;
 3 
 4 class Fahrenheit2Celsius {
 5   public static void main(String args[]) throws Exception {
 6     Parameters p = new Parameters();
 7     NumberVariable dV = new DoubleVariable();
 8     CharacterVariable cV = new CharacterVariable();
 9 
10     char ch;
11     do {
12       Format.printf("Input a temperature in Fahrenheit: ");
13       Format.scanf("%e", p.add(dV));
14       double fahrenheit = dV.doubleValue();
15       double celsius = (5.0/9.0)*(fahrenheit-32);
16       Format.printf("%.2f F is %.2f C\n", p.add(fahrenheit).add(celsius));
17       Format.printf("Another one? ");
18       Format.scanf(" %c", p.add(cV));
19       ch = cV.charValue();
20     } while(ch == 'y' || ch == 'Y');
21   }
22 }

Input a temperature in Fahrenheit: 23.4
23.40 F is -4.78 C
Another one? y
Input a temperature in Fahrenheit: 96.5
96.50 F is 35.83 C
Another one? n

References
[1] Serious Programming, 2001, http://www.seriousprogramming.com/.

Read two integers and a string

1 NumberVariable xV = new IntegerVariable();
2 NumberVariable yV = new IntegerVariable();
3 StringVariable sV = new StringVariable();
4 Format.scanf("%d %d %s", p.add(xV).add(yV).add(sV));

Read an integer and the characters until the end of line

1 NumberVariable xV = new IntegerVariable();
2 StringVariable sV = new StringVariable();
3 Format.scanf("%d %[^\n]\n", p.add(xV).add(sV));

Read (English) letters only

1 StringVariable sV = new StringVariable();
2 Format.scanf("%[a-zA-Z]", p.add(sV));