2017-05-30 30 views
1

我在分割从输入文件读入的字符串时遇到了一些问题,请确保它是有效的,然后将其保存到变量中。如何分割一个字符串并将其保存到一个变量,如果它是有效的?

比方说,这是第一个字符串: 12345 5 59.28

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

我的两个主要障碍是我不能在这个程序中使用数组,所以我不知道如何分割字符串。我试图把每个部分作为一个整数,但这似乎不起作用。

我的其他问题是,我不知道如何验证它。我会使用类似这样的东西:

//Assuming I have a scanner set up and a class, method declared 

//Declare variables 
int numbers; 
int studentID; 

while(fileInput.hasNext()) 
    { 
    numbers = fileInput.nextInt(); //Not sure how to pull a part of the string 
    } 

//Used to validate that it is within the range 
if(numbers < 00000 || numbers > 99999) 
    { 
    studentID = numbers; 
    } 

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

+1

为什么你不能使用数组?看来你最好使用'String'类的'split'方法,它返回一个数组。 –

+0

Javascript问题标记和代码片段被删除 - 这个问题有**没有任何**用Javascript编程。 –

+2

什么,你想要分配给一个变量?你正在处理字符串中的四个数字。如果你想将其中的每一个分配给一个变量,那么你不需要数组吗?请不要告诉我你要声明一堆变量,比如'variable1','variable2','variable3'等等。或者你想在检查有效性之后将整个字符串分配给变量吗? – ajb

回答

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>

+0

嗯......他不允许使用数组,我不知道他是否被允许使用正则表达式? – ajb

+0

没有一个阵列在制作这段代码时受到了伤害... – EyuelDK

+0

OP说的是_“我们不允许在我们分配它之前使用任何显然没有被教导的东西。”_ –

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()); 
+0

@DawoodibnKareem你是对的。该程序将退出堆栈跟踪,但这就是为什么我在“如果你知道文件的结构是什么”的前缀这个答案。这位学生没有寻找防弹不可破解的解决方案。只是一个将有助于他/她的研究。 – Headline

+0

@MichaelFlaherty其实,如果它是无效的,我应该继续与该程序,只是忽略无效的数据。 – Amai

+0

@Amai你知道异常处理吗? – Headline

0

我认为你的老师给这个任务练习你的if-else条件或者switch语句以及循环(基本)技能。

在这里我做了什么,这可能不完全符合你的任务问题,但使用这个,你可以得到完整的想法,想办法减少这种情况。嘿!因为我们不在这里做你的任务。你必须解决你的问题并熟悉这些问题。

试图了解这些,做的改变是什么发生:

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 "); 
     } 
    } 
} 

我不会也解释这一点。如果在处理此代码后遇到任何问题,您必须尝试。在评论中询问任何问题。

希望这会有所帮助!

+0

@Amai:试试这个。在解决这个问题后,从评论中提出任何问题。我没有解释它,因为你必须练习。练习让你完美! – Blasanka

+1

这是有帮助^ -^ 谢谢一束 – Amai

相关问题