簡體   English   中英

為什么輸入框的兩個字符串中的前兩個字符不能開頭?

[英]Why can't I get the first and last two characters in two strings for a input box?

我正在嘗試獲取字符串中的最后兩個字符(英寸)和前兩個字符(英尺)。 因為我嘗試了很多事情,所以我沒有看到什么。

高度應該在兩組數字之間有一個空格,例如5 10或12 02,所以這就是為什么我要獲取某些字符的原因。 因此,我可以將它們移到另一個字符串中以將它們添加在一起,因為我想將兩個人的高度相加,這就是我到目前為止所得到的...

import javax.swing.JOptionPane;

public class TestProgramming
{ //begin class
public static void main(String[] args)
{ // begin main

// ***** variables *****

    int h1;
    int h2;

    String height1 = "0";
    String height2 = "0";

    String  feet1;
    String  feet2;
    String inches1;
    String inches2;

    String FinalFoot = "0";
    String FinalInch = "12";

// ***** input box 1 ***** 

    height1 = JOptionPane.showInputDialog (null, "Please enter the height of the first person in inches.");

// ***** input box 2 ***** 

    height2 = JOptionPane.showInputDialog (null, "Please enter the height of the second person in inches.");  

// ***** parse ***** 

    h1 = Integer.parseInt(height1);
    h2 = Integer.parseInt(height2);

// ***** integer to string *****

    Integer.toString(h1);
    Integer.toString(h2);

// ***** taking the feet and inches from the two heights ***** 

    feet1 = h1.substr(0, 1);            // subtract the last 2 numbers
    inches1 = h1.substr(2, 4);      // subtract the first 2 numbers

    feet2 = h2.substr(0, 1);        // subtract the last 2 numbers
    inches2 = h2.substr(2, 4);      // subtract the first 2 numbers

如您所見,我遇到了問題:“距離兩個高度只有幾英尺”。

讓我們仔細看一下代碼...

int h1;
int h2;

String height1 = "0";
String height2 = "0";

//...

// This is good...
height1 = JOptionPane.showInputDialog (null, "Please enter the height of the first person in inches.");
height2 = JOptionPane.showInputDialog (null, "Please enter the height of the second person in inches.");  

// Ahh, here is a problem...
// If the String values contain any thing that is not a numerical character,
// these will throw a NumberFormatException, meaning anything that follows won't
// be processed...
h1 = Integer.parseInt(height1);
h2 = Integer.parseInt(height2);

// This does nothing.  Integer.toString will RETURN a String representation
// of the int's you pass it, but you're not assigning them to anything...
Integer.toString(h1);
Integer.toString(h2);

// Last time I checked, String don't have a method called `substr`
feet1 = h1.substr(0, 1);

從用戶獲得String輸入后,可以使用String#split在空格上分割輸入,例如...

height1 = ...
String[] heightsOf1 = height1.split(" ");

然后,您需要將每個元素轉換為一個int ...

int[] personOneHeights = new int[2];
personOneHeights[0] = Integer.parseInt(heightsOf1[0]);
personOneHeights[1] = Integer.parseInt(heightsOf1[1]);

現在,您確實應該在此處添加一些完整性檢查,您可以使用String#contains確定原始String包含空格字符,並繼續要求用戶輸入(例如)。 您還應該檢查split的結果,以確定其在數組中是否至少包含2個元素。

如果您不能使用數組,那么您不應該依賴幻數,而應該嘗試使用現有的信息,例如...

String part1 = height1.substring(0, height1.indexOf(" "));
String part2 = height1.substring(height1.lastIndexOf(" ") + 1);

我先將字符串用空格分開:

如何按空格分割字符串

String[] split1 = height1.split("\\s+");
feet1 = Integer.parseInt( split1[0] );
inches1 = Integer.parseInt( split1[1] );

String[] split2 = height2.split("\\s+");
feet2 = Integer.parseInt( split2[0] );
inches2 = Integer.parseInt( split2[1] );

這兩行:

Integer.toString(h1);
Integer.toString(h2);

不要做你認為他們做的事。

我認為您期望它們運行后,h1和h2現在是Strings。 但這不是事實。 變量不能更改類型。 h1和h2保持整數。

Integer.toString不會產生副作用,而是返回結果。 目前,您只是在丟棄結果。 您需要對結果進行處理,例如將其存儲在新變量中。 也許是這樣的:

String h1AsString = Integer.toString(h1);
String h2AsString = Integer.toString(h2);

您正在嘗試在int上使用substr() 另外,當您調用Integer.toString(h1); 不會將變量設置為字符串。 為此,它必須是:

feet1 = Integer.toString(h1)

feet1 = feet1.substr(0, 1); 

希望這可以幫助

嘗試以下操作:(請注意,我對輸入進行了硬編碼,以便在控制台應用程序中進行更輕松的測試)

public static void main(String []args){

// removed the early declarations here. This is C-style, old, unnecessary and makes 
// code harder to read

// ***** input box 1 ***** HARCODED - replace with UI call
String height1 = "6 10"; 

// ***** input box 2 ***** HARCODED - replace with UI call
String height2 = "3 10"; 

// ***** parse feet and inches from each input ***** 

String feet1 = height1.substring(0, 1);        // get the first digit
String inches1 = height1.substring(2, 4);      // get the last 2 digits

String feet2 = height2.substring(0, 1);        // get the first digit
String inches2 = height2.substring(2, 4);      // get the last 2 digits

// convert parsed string data to their integer counterparts
int f1 = Integer.parseInt(feet1);
int i1 = Integer.parseInt(inches1);

int f2 = Integer.parseInt(feet2);
int i2 = Integer.parseInt(inches2);

// calculate total feet
int totalFeet = f1 + f2 + (i1 + i2) / 12;

// calculate total inches (using modulus operator)
int totalInches = (i1 + i2) % 12;

// and do the output... assuming this is what you want...
System.out.println(totalFeet + " " + totalInches);
 }

另外,它的substring不是subtractsubstr

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM