如何将字符串拆分并将其保存到变量(如果有效)?

时间:2017-05-30 02:18:38

标签: java string split

我在分割从输入文件读入的字符串时遇到一些问题,确保它有效,然后将其保存到变量。

让我们说这是第一个字符串: 12345 5 59.28

我想分开12345,5和59.28。 在验证它们是正确的格式(00000-99999,0-5,000.00 0 100.00)之后,我会将其分配给变量。

我的主要两个障碍是我不能在这个程序中使用数组,因此我不确定如何分割字符串。我试过把每个部分作为一个整数来拉,但这似乎没有用。

我的另一个问题是,我不确定如何验证它。我会使用这样的东西:

        conm            fyear    dvpayout   industry    firmycount  ipodate  dv   age
46078   CAESARS ...     2003    0.226813    Services    22  19891213.0   Q2  27
46079   CAESARS ...     2004    0.226813    Services    22  19891213.0   Q2  27
46080   CAESARS ...     2005    0.226813    Services    22  19891213.0   Q2  27
46091   CAESARS ...     2016    0.226813    Services    22  19891213.0   Q2  27
114620  CAESARSTONE LTD 2010    0.487543    Manufacturing   10  20120322.0  Q3  4
114621  CAESARSTONE LTD 2011    0.487543    Manufacturing   10  20120322.0  Q3  4
114622  CAESARSTONE LTD 2012    0.487543    Manufacturing   10  20120322.0  Q3  4
114623  CAESARSTONE LTD 2013    0.487543    Manufacturing   10  20120322.0  Q3  4
114624  CAESARSTONE LTD 2014    0.487543    Manufacturing   10  20120322.0  Q3  4
114625  CAESARSTONE LTD 2015    0.487543    Manufacturing   10  20120322.0  Q3  4
114626  CAESARSTONE LTD 2016    0.487543    Manufacturing   10  20120322.0  Q3  4
132524  CAFEPRESS INC   2010    0.000000    Retail Trade    7   20120329.0  Q1  4
132525  CAFEPRESS INC   2011    0.000000    Retail Trade    7   20120329.0  Q1  4
132526  CAFEPRESS INC   2012    -0.000000   Retail Trade    7   20120329.0  Q1  4
132527  CAFEPRESS INC   2013    -0.000000   Retail Trade    7   20120329.0  Q1  4
132528  CAFEPRESS INC   2014    -0.000000   Retail Trade    7   20120329.0  Q1  4
132529  CAFEPRESS INC   2015    -0.000000   Retail Trade    7   20120329.0  Q1  4
132530  CAFEPRESS INC   2016    -0.000000   Retail Trade    7   20120329.0  Q1  4
120049  CAI INTERNATIONAL INC   2006    0.000000    Services    12  20070516.0 Q1  0
120050  CAI INTERNATIONAL INC   2007    0.000000    Services    12  20070516.0 Q1  0
3896    CALAMP CORP 1999    -0.000000   Manufacturing   23  NaN   Q1  Nan  
3897    CALAMP CORP 2000    0.000000    Manufacturing   23  NaN   Q1  Nan
3898    CALAMP CORP 2001    0.000000    Manufacturing   23  NaN   Q1  Nan
3899    CALAMP CORP 2002    0.000000    Manufacturing   23  NaN   Q1  Nan
21120   CALATLANTIC GROUP INC   1995    -0.133648   Construction    22  NaN   Q1  Nan
21121   CALATLANTIC GROUP INC   1996    -0.133648   Construction    22  NaN   Q1  Nan
21122   CALATLANTIC GROUP INC   1997    -0.133648   Construction    22  NaN   Q1  Nan
21123   CALATLANTIC GROUP INC   1998    -0.133648   Construction    22  NaN   Q1  Nan
21124   CALATLANTIC GROUP INC   1999    -0.133648   Construction    22  NaN   Q1  Nan
21125   CALATLANTIC GROUP INC   2000    -0.133648   Construction    22  NaN   Q1  Nan
21126   CALATLANTIC GROUP INC   2001    -0.133648   Construction    22  NaN   Q1  Nan
21127   CALATLANTIC GROUP INC   2002    -0.133648   Construction    22  NaN  Q1  Nan
21128   CALATLANTIC GROUP INC   2003    -0.133648   Construction    22  NaN  Q1  Nan

我是Java的初学者,请原谅我的困惑。

3 个答案:

答案 0 :(得分:0)

试试这个。无效格式将在next方法调用期间抛出异常。

import java.util.Scanner;

class Main {

  public static void main(String[] args) {

    Scanner in = new Scanner("12345 5 59.28");
    in.useDelimiter(" "); // reads per space

    String next = in.next("\\d{5}");  // reads next 5 digits
    int numbers = Integer.valueOf(next);
    System.out.println(numbers);

    next = in.next("\\d{1}"); // reads next 1 digit
    int studentId = Integer.valueOf(next);
    System.out.println(studentId);

    next = in.next("\\d{2}\\.\\d{2}"); // reads next a decimal with two digits before and after point
    float floatingNumbers = Float.valueOf(next);
    System.out.println(floatingNumbers);
  }
}

<script src="//repl.it/embed/IWzC/0.js"></script>

答案 1 :(得分:0)

如果你知道文件的结构是什么,例如它的格式如下:

int int double

然后,您只需致电nextInt()nextInt(),然后nextDouble()即可通过该方式解析数据。

也许是这样的

do
{
    num1 = scanner.nextInt();
    num2 = scanner.nextInt();
    num3 = scanner.nextDouble();
} while (scanner.hasNextInt());

这样做是为了收集您的所有数据,但如果您有大量的数据需要阅读,那么您可能需要很多变量

或者,如果有错误的数据有时会在它之后立即生成正确的数据,那么就可以跳过错误的数据,即使它不是很漂亮

do
{
    if (scanner.hasNextInt())
    {
        num1 = scanner.nextInt();
    }
    else
    {
        scanner.next() // move past whatever bad data there was
        num1 = scanner.nextInt();
    }

    if (scanner.hasNextInt())
    {
        num2 = scanner.nextInt();
    }
    else
    {
        scanner.next() // move past whatever bad data there was
        num2 = scanner.nextInt();
    }

    if (scanner.hasNextDouble())
    {
        num3 = scanner.nextDouble();
    }
    else
    {
        scanner.next() // move past whatever bad data there was
        num3 = scanner.nextDouble();
    }
} while (scanner.hasNext());

答案 2 :(得分:0)

我认为你的老师会给你这个作业来练习你的if-else条件或转换语句以及循环(基本)技能。

在这里我做了什么,这可能与你的作业问题不完全匹配,但使用它你可以得到完整的想法,并想出一种方法来减少这一点。嘿!因为我们不在这里做你的任务。你必须解决你的问题并熟悉它们。

尝试理解这些,做出改变看看会发生什么:

public static void main(String[] args) {

    Scanner fileInput = new Scanner(System.in);
    //Declare variables
    String numbers = "";

    String firstNum = "";
    String secondNum = "";
    String thirdNum = "";

    int studentID = 0;
    int secondDigit = 0;
    double thirdDigit = 0;

    System.out.print("Input: ");
    numbers = fileInput.nextLine(); 

    int firstIndex = 0;
    int secondIndex = 0;
    int thirdIndex = 0;

    firstIndex = numbers.indexOf(" ");
    if(firstIndex <= 4){
        System.out.println("Number should be 5");
    }else{

        firstNum = numbers.substring(0, firstIndex);
        numbers = numbers.substring(firstIndex+1);

        studentID = Integer.parseInt(firstNum);

        if(studentID > 0 && studentID < 99999){
            System.out.println("First num: " +firstNum);
        }else{
            System.out.println("first digits not in a range ");
        }
    }

    secondIndex = numbers.indexOf(" ");
    if(secondIndex == 0){
        System.out.println("no number");
    }else{
        secondNum = numbers.substring(0, secondIndex);

        numbers = numbers.substring(secondIndex+1);

        secondDigit = Integer.parseInt(secondNum);

        if(secondDigit >= 0 && secondDigit <= 5){
            System.out.println("Second num: " +secondNum);
        }else{
            System.out.println("second digit not in a range ");
        }
    }


    thirdIndex = numbers.length();
    if(thirdIndex < 3){
        System.out.println("3 numbers should be there");
    }else{
        thirdNum = numbers.substring(0, thirdIndex);
        thirdDigit = Double.parseDouble(thirdNum);

        if(thirdDigit >= 0 && thirdDigit <= 100){
            System.out.println("third num: " +thirdNum);
        }else{
            System.out.println("third digit not in a range ");
        }
    }  
}

我也不打算解释这个。如果您在使用此代码后遇到任何问题,则必须尝试。在评论中提出任何问题。

希望这会有所帮助!