需要帮助将字符串拆分为两个单独的整数进行处理

发布于 2024-12-03 16:45:48 字数 1636 浏览 1 评论 0原文

我正在研究java中的一些数据结构,但我对如何将此字符串拆分为两个整数有点困惑。基本上,用户将输入类似“1200:10”的字符串。我使用 indexOf 来检查是否存在 : ,但现在我需要获取冒号之前的数字并将其设置为 val 并将另一个数字设置为rad。我认为我应该使用 substringparseInt 方法,但我不确定。下面的代码也可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如若梦似彩虹 2024-12-10 16:45:48

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.

心意如水 2024-12-10 16:45:48

做这个

    if(check1==-1)
    {
        System.out.println("I think you forgot the ':'.");

    }
    else
    {
     String numbers [] = input1.split(":"); //if the user enter 1123:2342 this method 

     //will
     // return array of String which contains two elements numbers[0] = "1123" and numbers[1]="2342"
    System.out.print("first number = "+ numbers[0]);
    System.out.print("Second number = "+ numbers[1]);
    }

make this

    if(check1==-1)
    {
        System.out.println("I think you forgot the ':'.");

    }
    else
    {
     String numbers [] = input1.split(":"); //if the user enter 1123:2342 this method 

     //will
     // return array of String which contains two elements numbers[0] = "1123" and numbers[1]="2342"
    System.out.print("first number = "+ numbers[0]);
    System.out.print("Second number = "+ numbers[1]);
    }
夜无邪 2024-12-10 16:45:48

您可以使用 indexOf 知道 : 出现在哪里。假设字符串长度为 n 并且 : 出现在索引 i 处。然后请求 子字符串(int beginIndex,int endIndex)0到i-1i+1到n-1。更简单的是使用 字符串::分割

You knew where : is occurs using indexOf. Let's say string length is n and the : occurred at index i. 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文