Studying Java ―― 4

Put a string in a variable

The variable definition for the string is "String"

Be careful ・ First S is uppercase -When putting it in a variable, enclose the character string before and after it with "" ". Is it about?

Add "String" to the usual uninflected word.

Yomogi.java


public class Yomogi{
	public static void main(String[] args){
		String s;
		s = "Hello World";
		
		System.out.println(s);
	}
}

Run

pic007.JPG

With this kind of thing, it is the same even if you write the character string directly in println I tried it as a preparation for inputting from the keyboard after this.

Input from the keyboard

Enter a character string from the keyboard. Remember the new and basic forms that are developed from the basic forms as the basic forms of keyboard input.

Yomogi.java


import java.io.*;

public class Yomogi{
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		String s;
		s = br.readLine();
		
		System.out.println(s);
	}
}

The part added from the basic form is ・ Import java.io. *; ・ Throws IOException -BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); ・ Br.readLine () Wonder.

Entered from the keyboard in the "br.readLine ()" part Is it a place to put a character string in the variable s?

This is a template for keyboard input Hit it in your head once.

Run

pic009.jpg

From this state, enter "test" on the keyboard

pic010.jpg

Enter

pic008.JPG

The same character string you entered is displayed, so it's OK.

Digitize the numbers entered from the keyboard

Even if you enter a number from the keyboard, it is treated as a character string instead of a number. Therefore, it is quantified for calculation in the program.

-Convert to a number using Integer.parseInt ()

Add "Integer.parseInt ()" to the basic form of keyboard input

Yomogi.java


import java.io.*;

public class Yomogi{
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int a;
		String s;

		s = br.readLine();		
		a = Integer.parseInt(s);
		
		a = a + 5;
		
		System.out.println(a);
	}
}

-Enter the number (character string) entered from the keyboard into the variable s. -Variable s is quantified by "Integer.parseInt (s)" and put in variable a ・ Add 5 to a and output

Run

pic011.jpg

Since it was as expected, the conversion from character string to numerical value was completed successfully.

This time, like ↓, I executed ** input ** and ** digitization ** of the character string on separate lines.

int a;
String s;
s = br.readLine();       //input
a = Integer.parseInt(s);      //Quantify

↓ If you do this, it seems that you can put ** input processing ** in ** digitization ** and put it together.

int a;
a = Integer.parseInt(br.readLine());

You can omit the variable definition of the character string, This may be better depending on the situation.

When you want to digitize a character string including a decimal point

Double.parseDouble()

I may use it someday.

This time up to here.

Recommended Posts

Studying Java ―― 3
Studying Java ―― 4
Studying Java -5
Studying Java ―― 1
Studying Java # 0
Studying Java ―― 8
Studying Java ②
Studying Java ―― 7
Studying Java ―― 2
Studying Java ①
Studying Java -10
Studying Java 8 (Optional)
Studying java9 (jShell)
Studying Java 8 (Stream)
Studying Java 8 (Collector / Collectors)
Studying Java 8 (see method)
Studying Java 8 (see constructor)
Java
Studying Java ~ Part 8 ~ Cast
Studying Java 8 (lambda expression)
Java
Java learning (0)
[Java] array
[Java] Annotation
[Java] Module
Java array
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Studying Java # 6 (How to write blocks)
java (array)
Java static
Java serialization
JAVA paid
Java (set)
java shellsort
[Java] compareTo
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
[Java] Array
[Java] Polymorphism
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload