需要帮助将字符串拆分为两个单独的整数进行处理
我正在研究java中的一些数据结构,但我对如何将此字符串拆分为两个整数有点困惑。基本上,用户将输入类似“1200:10”的字符串。我使用 indexOf
来检查是否存在 :
,但现在我需要获取冒号之前的数字并将其设置为 val
并将另一个数字设置为rad
。我认为我应该使用 substring
或 parseInt
方法,但我不确定。下面的代码也可以在 http://pastebin.com/pJH76QBb 查看
import java.util.Scanner; // Needed for accepting input
public class ProjectOneAndreD
{
public static void main(String[] args)
{
String input1;
char coln = ':';
int val=0, rad=0, answer=0, check1=0;
Scanner keyboard = new Scanner(System.in); //creates new scanner class
do
{
System.out.println("****************************************************");
System.out.println(" This is Project 1. Enjoy! "); //title
System.out.println("****************************************************\n\n");
System.out.println("Enter a number, : and then the radix, followed by the Enter key.");
System.out.println("INPUT EXAMPLE: 160:2 {ENTER} "); //example
System.out.print("INPUT: "); //prompts user input.
input1 = keyboard.nextLine(); //assigns input to string input1
check1=input1.indexOf(coln);
if(check1==-1)
{
System.out.println("I think you forgot the ':'.");
}
else
{
System.out.println("found ':'");
}
}while(check1==-1);
}
}
I am working on some data structures in java and I am a little stuck on how to split this string into two integers. Basically the user will enter a string like '1200:10'. I used indexOf
to check if there is a :
present, but now I need to take the number before the colon and set it to val
and set the other number to rad
. I think I should be using the substring
or parseInt
methods, but am unsure. The code below can also be viewed at http://pastebin.com/pJH76QBb
import java.util.Scanner; // Needed for accepting input
public class ProjectOneAndreD
{
public static void main(String[] args)
{
String input1;
char coln = ':';
int val=0, rad=0, answer=0, check1=0;
Scanner keyboard = new Scanner(System.in); //creates new scanner class
do
{
System.out.println("****************************************************");
System.out.println(" This is Project 1. Enjoy! "); //title
System.out.println("****************************************************\n\n");
System.out.println("Enter a number, : and then the radix, followed by the Enter key.");
System.out.println("INPUT EXAMPLE: 160:2 {ENTER} "); //example
System.out.print("INPUT: "); //prompts user input.
input1 = keyboard.nextLine(); //assigns input to string input1
check1=input1.indexOf(coln);
if(check1==-1)
{
System.out.println("I think you forgot the ':'.");
}
else
{
System.out.println("found ':'");
}
}while(check1==-1);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Substring 可以工作,但我建议查看 String.split。
split 命令将创建一个字符串数组,然后您可以使用 parseInt 获取其整数值。
String.split 接受一个正则表达式字符串,因此您可能不想只在其中放入任何字符串。
尝试这样的操作:
"Your|String".split("\\|");
,其中|
是分割字符串两部分的字符。两个反斜杠将告诉 Java 您想要那个确切的字符,而不是 | 的正则表达式解释。这仅对某些角色真正重要,但更安全。
来源:http://www.rgagnon.com/javadetails/java-0438.html
希望这能让您开始。
Substring would work, but I would recommend looking into String.split.
The split command will make an array of Strings, which you can then use parseInt to get the integer value of.
String.split takes a regex string, so you may not want to just throw in any string in it.
Try something like this:
"Your|String".split("\\|");
, where|
is the character that splits the two portions of the string.The two backslashes will tell Java you want that exact character, not the regex interpretation of |. This only really matters for some characters, but it's safer.
Source: http://www.rgagnon.com/javadetails/java-0438.html
Hopefully this gets you started.
做这个
make this
您可以使用 indexOf 知道
:
出现在哪里。假设字符串长度为 n 并且:
出现在索引i
处。然后请求 子字符串(int beginIndex,int endIndex)从0到i-1和i+1到n-1。更简单的是使用 字符串::分割You knew where
:
is occurs using indexOf. Let's say string length is n and the:
occurred at indexi
. Then ask for substring(int beginIndex, int endIndex) from 0 to i-1 and i+1 to n-1. Even simpler is to use String::split