我正在研究 java 中的一些数据结构,我对如何将此字符串拆分为两个整数有点困惑。基本上,用户将输入一个像“1200:10”这样的字符串。我曾经indexOf
检查是否有:
礼物,但现在我需要将冒号前的数字设置为val
并将另一个数字设置为rad
。我想我应该使用substring
orparseInt
方法,但不确定。下面的代码也可以在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);
}
}