Java 为什么可以';我不能为输入框获取两个字符串中的第一个和最后两个字符吗?

Java 为什么可以';我不能为输入框获取两个字符串中的第一个和最后两个字符吗?,java,Java,我正在尝试获取字符串中的最后两个字符(英寸)和前两个字符(英尺)。有什么我没看到的,因为我试了很多东西 高度应该在两组数字之间有一个空格,比如5 10或12 02,这就是为什么我试图得到某些字符。这样我就可以把它们移动到另一个字符串中,把它们加在一起,因为我想把两个人的身高加在一起,这就是我到目前为止得到的 import javax.swing.JOptionPane; public class TestProgramming { //begin class public static voi

我正在尝试获取字符串中的最后两个字符(英寸)和前两个字符(英尺)。有什么我没看到的,因为我试了很多东西

高度应该在两组数字之间有一个空格,比如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

如您所见,我遇到了一些问题,其中的内容是“从两个高度开始测量英尺和英寸”。

我先将字符串按空格分开:


我首先将字符串按空格拆分:

这两条线:

Integer.toString(h1);
Integer.toString(h2);
不要做你认为他们会做的事

我想你应该期待在它们运行之后,h1和h2现在是字符串。但事实并非如此。变量无法更改类型。h1和h2保持整数

而不是有副作用,
Integer.toString
返回一个结果。目前,您只需丢弃tht结果。您需要对结果执行一些操作,例如将其存储在新变量中。也许是这样的:

String h1AsString = Integer.toString(h1);
String h2AsString = Integer.toString(h2);
这两条线:

Integer.toString(h1);
Integer.toString(h2);
不要做你认为他们会做的事

我想你应该期待在它们运行之后,h1和h2现在是字符串。但事实并非如此。变量无法更改类型。h1和h2保持整数

而不是有副作用,
Integer.toString
返回一个结果。目前,您只需丢弃tht结果。您需要对结果执行一些操作,例如将其存储在新变量中。也许是这样的:

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

您正试图在
int
上使用
substr()
。另外,当您调用
Integer.toString(h1)时。为此,必须:

feet1 = Integer.toString(h1)

feet1 = feet1.substr(0, 1); 

希望这对您尝试在
int
上使用
substr()
有所帮助。另外,当您调用
Integer.toString(h1)时。为此,必须:

feet1 = Integer.toString(h1)

feet1 = feet1.substr(0, 1); 

希望这有帮助

让我们更仔细地看看代码

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#split
在空格上分割输入,例如

height1 = ...
String[] heightsOf1 = height1.split(" ");
String part1 = height1.substring(0, height1.indexOf(" "));
String part2 = height1.substring(height1.lastIndexOf(" ") + 1);
然后需要将每个元素转换为
int

int[] personOneHeights = new int[2];
personOneHeights[0] = Integer.parseInt(heightsOf1[0]);
personOneHeights[1] = Integer.parseInt(heightsOf1[1]);
现在,您确实应该在这里添加一些健全性检查,您可以使用
String#contains
来确定原始
String
是否包含空格字符,如果不包含空格字符,则继续要求用户输入(例如)。您还应该检查
拆分的结果,以确定数组中是否至少有2个元素

如果你不能使用数组,你不应该依赖幻数,但是试着使用你手头的信息,例如

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

让我们仔细看看代码

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#split
在空格上分割输入,例如

height1 = ...
String[] heightsOf1 = height1.split(" ");
String part1 = height1.substring(0, height1.indexOf(" "));
String part2 = height1.substring(height1.lastIndexOf(" ") + 1);
然后需要将每个元素转换为
int

int[] personOneHeights = new int[2];
personOneHeights[0] = Integer.parseInt(heightsOf1[0]);
personOneHeights[1] = Integer.parseInt(heightsOf1[1]);
现在,您确实应该在这里添加一些健全性检查,您可以使用
String#contains
来确定原始
String
是否包含空格字符,如果不包含空格字符,则继续要求用户输入(例如)。您还应该检查
拆分的结果,以确定数组中是否至少有2个元素

如果你不能使用数组,你不应该依赖幻数,但是试着使用你手头的信息,例如

height1 = ...
String[] heightsOf1 = height1.split(" ");
String part1 = height1.substring(0, height1.indexOf(" "));
String part2 = height1.substring(height1.lastIndexOf(" ") + 1);
试试这个:(注意,我硬编码了输入,以便在控制台应用程序中更容易测试)

另外,它是
substring
而不是
subtract
substr

试试这个:(注意,我对输入进行了硬编码,以便于在控制台应用程序中进行测试)


另外,它是
substring
而不是
subtract
substr

发生了什么你不期望的事情?我只是不知道为什么h1和h2不工作,我也不知道为什么。发生了什么你不期望的事情?我只是不知道为什么h1和h2不工作,我也不知道为什么。好的捕获。。。我想用绳子-高1和高2好抓。。。我想使用字符串-height1和height2