scanner类对键盘输入进行判断:
public class Demo1_Scanner { public static void main(String[] args) { Scanner sc = new Scanner(System.in); if(sc.hasNextInt()) { int i = sc.nextInt(); System.out.println(i); }else { System.out.println("wrong"); } } }
同理,hasNextInt()还可以换成hasNextBoolean() ,hasNextDouble() 等。
nextInt()获取下个输入Int值,同样可以nextDouble(),nextBoolean(),nextLong(),等待
- 键盘录入的是字符串,用nextLine()方法,也可以传入整型,推荐使用。
String 常见问题
* 1.判断定义为String类型的s1和s2是否相等 * String s1 = "abc"; * String s2 = "abc"; * System.out.println(s1 == s2); ? //true,因为是常量,只会创建一个 * System.out.println(s1.equals(s2)); ? //true * 2.下面这句话在内存中创建了几个对象? * String s1 = new String("abc"); //2个,新创的字符串是参数常量的副本,一个在常量池中,一个在堆内存中 * 3.判断定义为String类型的s1和s2是否相等 * String s1 = new String("abc"); * String s2 = abc"; * System.out.println(s1 == s2); ? //false,s1在堆内存,s2在常量池 * System.out.println(s1.equals(s2)); ? //true * 4.判断定义为String类型的s1和s2是否相等 * String s1 = "a" + "b" + "c"; * String s2 = "abc"; * System.out.println(s1 == s2); ? //true,java中有常量优化机制 * System.out.println(s1.euqals(s2)); ? //true * 5.判断定义为String类型的s1和s2是否相等 * String s1 = "ab"; * String s2 = "abc"; * String s3 = s1 + "c"; * System.out.println(s3 == s2); ? //false,s2在常量池,s3在堆内存 * System.out.println(s3.equals(s2)); ? //true
String类的判断功能(方法)
https://blog.csdn.net/euller/article/details/53240916一个简单的模拟用户登录
import java.util.Scanner; public class test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); for(int i = 0; i < 3; i++) { System.out.println("input"); String userName = sc.nextLine(); System.out.println("password"); String password = sc.nextLine(); if("admin".equals(userName) && "admin".equals(password)) { System.out.println("welcome " + userName); break; }else { if(i == 2) { System.out.println("go plz"); }else { System.out.println("wrong, " + (2-i) + " times left"); } } } } }
String常用功能 :获取,判断,转换格式,替换,切割,获取子串,转换大小写,去空格,比较
https://blog.csdn.net/jhfleander/article/details/73905843使用Eclipse抽取封装方法
https://jingyan.baidu.com/article/3d69c5513231a7f0ce02d75c.html- .lengh 和 .length()的区别
length是一般集合类对象的属性,length()是一般字符串类对象的方法。
java中的length属性是针对数组说的,length()方法是对字符串String说的。 统计不同类型字符个数实例
public class test2 { public static void main(String[] args) { String s = "ABJDHJdsadaa123!@#$%"; int big = 0; int small = 0; int num = 0; int other = 0; //1, 获取每一个字符 for(int i = 0; i < s.length(); i++) { char c = s.charAt(i); //通过索引获取每一个字符 //2,判断字符的范围 if(c >= 'A' && c <= 'Z') { big++; }else if(c >='a' && c <= 'z') { small++; }else if(c >= '0' && c <= '9') { num++; }else { other++; } } //3,打印计数器结果 System.out.println("s:" + big + "bigs " + small + "smalls " + num + "nums " + other + "others"); } }
- 搜索长字符串中包含短字符串的实例
public class test3 {
public static void main(String[] args) {
// 定义大串
String max = "woaijava,javashizuishiyongdeyuyan,xuexi" + "javameiyoucuo" + "jav";
// 定义小串
String min = "java";
System.out.println(max.indexOf("q")); // 不出现返回-1
// 定义计数器变量
int count = 0;
// 定义索引
int index = 0;
// 定义循环,判断小串是否在大串中出现,不出现返回-1
while ((index = max.indexOf(min)) != -1) {
count++; // 计数器自增
max = max.substring(index + min.length());
}
System.out.println(count);
}
}