[转载] 将一个整数型字符串转换为一个整数

参考链接: Java中整数到字符串转换的不同方法

package com.utl.string;

 /*

  * 将一组整数字符串转换为整数

  * 例:"234"转换为整数型234

  * 分析:涉及到许多问题,例如非法输入,有正负号,是否为空字符串等等 

  * */

 import java.util.Scanner;

 public class Strtoint {

 

public static void main(String[] args) {

 

Scanner scan=new Scanner(System.in);

 

String Numstr=scan.nextLine();

 

if(Numstr.length()==0){//如果不输入

 

System.out.println("输入错误:输入字符串为空");

 

}

 

else if(Numstr.indexOf(' ')!=0){//输入字符串中有空格

 

System.out.println("输入错误:输入字符串中含有空格");

 

}

 

else{

 

int result_num=StrToInt(Numstr);

 

System.out.println(result_num);

 

}

 

 

}

 

public static int StrToInt(String numstr){

 

int number=0;

 

char []str=numstr.toCharArray();

 

 

 

//"以'-'开头

 

if(str[0]=='-'){

 

 

if(numstr.length()==1){

 //"-"

 

System.out.println("输入错误!");

 

}

 

else if(str[1]<='0'||str[1]>'9'){

 //"-0687"

 

System.out.println("输入错误!");

 

}

 

else if(str[1]>'0'&&str[1]<='9'){

 

for(int i=2;i<numstr.length();i++){

 

if(str[i]<'0'||str[i]>'9'){

 

System.out.println("输入错误!");

 

break;

 

}

 

else

 

 

{

 

number=Integer.parseInt(numstr);

 

System.out.print("数字字符串\t"+numstr+"\t"+ "转换为整数:");

 

 

break;

 

}

 

}

 

}

 

}

 

//非法字符开头

 

else if('9'<str[0]||str[0]<='0'){

 //"a809"或"A89"

 

System.out.println("输入错误!");

 

}

 

//以'0'字符开头

 

else if(str[0]=='0'){

 //"0154"

 

if(numstr.length()>1){

 

System.out.println("输入错误!");

 

}

 

if(numstr.length()==1){

 //"0"

 

number=Integer.parseInt(numstr);

 

System.out.print("数字字符串\t"+numstr+"\t转换为整数:");

 

}

 

}

 

//以'1-9'字符开头

 

else if(str[0]>'0'&&str[0]<='9'){

 

for(int i=1;i<numstr.length();i++){

 

if(str[i]<'0'||str[i]>'9'){

 

System.out.println("输入错误!");

 

break;

 

}

 

else

 

 

{

 

number=Integer.parseInt(numstr);

 

System.out.print("数字字符串\t"+numstr+"\t"+ "转换为整数:");

 

 

break;

 

}

 

}

 

}

 

return number;

 

}

 }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540200.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

升级PowerShell至4.0版本

为了更好的使用Cmder v1.2&#xff0c;不得不升级PowerShell为4.0。 不知道Cmder的&#xff0c;可以点击这里&#xff1a;https://github.com/cmderdev/cmder 和 逆天神器 cmder。 Powershell是运行在Windows机器上实现系统和应用程序管理自动化的命令行脚本环境。 需要.NET环境…

[转载] 字符串太长 pep8_Python f字符串– PEP 498 –文字字符串插值

参考链接&#xff1a; 从Java中的字符串中删除前导零 字符串太长 pep8 Python f-strings or formatted strings are the new way to format strings. This feature was introduced in Python 3.6 under PEP-498. It’s also called literal string interpolation. Python f字符…

备忘(持续更新。。。)

1、在springmvc这个框架里面&#xff0c;创建新的业务逻辑层&#xff0c;dao、service层至少需要一个接口&#xff0c;项目才能跑起来 2、获取当前用户桌面路径 File desktopDir FileSystemView.getFileSystemView() .getHomeDirectory();String desktopPath desktopDir.getA…

[转载] 字符串操作截取后面的字符串_对字符串的5个必知的熊猫操作

参考链接&#xff1a; 修剪Java中的字符串(删除前导和尾随空格) 字符串操作截取后面的字符串 We have to represent every bit of data in numerical values to be processed and analyzed by machine learning and deep learning models. However, strings do not usually co…

更改域控制器的计算机名

林功能级别必须为Windows Server 2003及以上 1. netdom computername Server08-1.contoso.com /add:08Server1.contoso.com 2. netdom computername Server08-1.contoso.com /makeprimary:08Server1.contoso.com 3. Restart your computer 4. netdom computername 08Server1.co…

[转载] Google Java代码规范

参考链接&#xff1a; 使用Java计算文本文件txt中的行数/单词数/字符数和段落数 原文地址&#xff1a;https://google.github.io/styleguide/javaguide.html GIthub上GoogleCode风格的配置文件&#xff08;支持Eclipse IDE和IntelliJ IDE&#xff09;&#xff1a;https://git…

SQL PASS西雅图之行——签证篇

本人有幸通过IT168&itpub的站庆活动http://www.itpub.net/thread-1716961-1-1.html&#xff0c;并应微软邀请参加了在西雅图举办的The Conference for SQL Server Professionals&#xff08;简称SQL-PASS&#xff09;。 SQL-PASS会议计划于2012年11月6日-9日举行&#xff0…

[转载] java8 lambda表达式 List转为Map

参考链接&#xff1a; 使用Lambda表达式检查字符串在Java中是否仅包含字母 public static void main(String[] args) { List<User> userList new ArrayList<User>(); User user0 new User("han1", "男1", 20); User user1 new User("…

11.python并发入门(part5 event对象)

一、引入event。 每个线程&#xff0c;都是一个独立运行的个体&#xff0c;并且每个线程的运行状态是无法预测的。 如果一个程序中有很多个线程&#xff0c;程序的其他线程需要判断某个线程的运行状态&#xff0c;来确定自己下一步要执行哪些操作。 threading模块中的event对象…

[转载] Java 将字符串首字母转为大写 - 利用ASCII码偏移

参考链接&#xff1a; 使用ASCII值检查Java中的字符串是否仅包含字母 将字符串name 转化为首字母大写。普遍的做法是用subString()取第一个字母转成大写再与之后的拼接&#xff1a; str str.substring(0, 1).toUpperCase() str.substring(1); 看到一种效率更高的做法&…

俞永福卸任阿里大文娱董事长,改任 eWTP 投资组长

两天前&#xff08;11月13日&#xff09;&#xff0c;阿里文娱董事长俞永福离职的消息&#xff0c;在互联网圈炸了锅。但很快&#xff0c;俞本人就在微博做了澄清&#xff0c;并称“永远幸福&#xff0c;我不会离开”。然而就在今天&#xff08;11月15日&#xff09;&#xff0…

[转载] java提取字符串中的字母数字

参考链接&#xff1a; 使用Regex检查字符串在Java中是否仅包含字母 String str "adsf adS DFASFSADF阿德斯防守对方asdfsadf37《&#xff1f;&#xff1a;&#xff1f;%#&#xffe5;%#&#xffe5;%#$%#$%^><?1234"; str str.replaceAll("[^a-zA-…

snort的详细配置

前一段一直在做snort入侵检测系统的安装以及配置&#xff0c;看了很多的网上资料&#xff0c;也算是总结了下前辈的经验吧。需要的软件包&#xff1a;1、httpd-2.2.6.tar.gz2、mysql-5.1.22-rc-linux-i686-icc-glibc23.tar.gz3、php-5.2.4.tar.bz24、acid-0.9.6b23.tar.gz5、ad…

[转载] Java:获取数组中的子数组的多种方法

参考链接&#xff1a; Java中的数组Array 我的个人博客&#xff1a;zhang0peter的个人博客 Java&#xff1a;从一个数组中创建子数组 使用Arrays.copyOfRange函数 Arrays.copyOfRange支持&#xff1a;boolean[]&#xff0c; byte[] &#xff0c;char[]&#xff0c;double…

[转载] Java中Array(数组)转List(集合类)的几种方法

参考链接&#xff1a; Java中的数组类Array 1、循环。新建List类&#xff0c;循环填充。 2、利用Arrays类的静态方法asList()。 Arrays.asList(T[])返回Arrays类的一个内部内List(T)&#xff0c;此类继承自AbstractList&#xff0c;不可增删。若想要一个可以增删的List类&am…

Linux查看系统cpu个数、核心书、线程数

Linux查看系统cpu个数、核心书、线程数 现在cpu核心数、线程数越来越高&#xff0c;本文将带你了解如何确定一台服务器有多少个cpu、每个cpu有几个核心、每个核心有几个线程。 查看物理cpu个数 cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l 查看核…

[转载] java中数组的反射的探究

参考链接&#xff1a; Java中的反射数组类reflect.Array 数组的反射有什么用呢&#xff1f;何时需要使用数组的反射呢&#xff1f;先来看下下面的代码&#xff1a; Integer[] nums {1, 2, 3, 4}; Object[] objs nums; //这里能自动的将Integer[]转成Object[] Object obj n…

防火墙iptables之常用脚本

防火墙iptables之常用脚本 转自&#xff1a;http://zhujiangtao.blog.51cto.com/6387416/1286490 标签&#xff1a;防火墙 主机 1。不允许别人ping我的主机&#xff0c;但是我可以ping别人的主机 #!/bin/bash iptables -F iptables -X iptables -Z modprobe ip_tables modprobe…

[转载] java中50个关键字以及各自用法大全

参考链接&#xff1a; Java中的默认数组值 关键字和保留字的区别 正确识别java语言的关键字&#xff08;keyword&#xff09;和保留字&#xff08;reserved word&#xff09;是十分重要的。Java的关键字对java的编译器有特殊的意义&#xff0c;他们用来表示一种数据类型&…

NFS 共享存储

服务器客户端yum -y install rpcbind nfs-utils 服务器 vim /etc/exports /data 192.168.10.0/24(rw,sync,no_root_squash) * ro # 只读权限 * rw # 读写权限 * sync # 同步&#xff0c;数据更安全&#xff0c;速度慢 * async #异步&#xff0c;速度快&#xff0c;效率高&a…