1

I'm trying to get the last two characters in a string for inches and the first two characters for feet. Is there something I'm not seeing because I've tried a lot of things.

The heights are supposed to have a space between two sets of numbers something like 5 10 or 12 02 so that's why I'm trying to get certain characters. It's so I can move them into another string to add them together because I'm suppose to add two peoples heights together and this is what I got so far...

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

As you can see, I'm having problems where it says "taking the feet and inches from the two heights".

Seth Tisue
  • 29,985
  • 11
  • 82
  • 149

5 Answers5

1

Let's take a little closer look at the code...

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

Once you have the String input from the user, you can use String#split to split the input on the spaces, for example...

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

You would then need to convert each element into an int...

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

Now, you really should add some sanity checking here, you can use String#contains to determine if the original String contains a space character or not and keep asking the user for input if it doesn't (for example). You should also check the results of the split to determine if it has at least 2 elements in the array.

If you can't use arrays, you shouldn't rely on magic numbers, but try and use the information you have at hand, for example...

String part1 = height1.substring(0, height1.indexOf(" "));
String part2 = height1.substring(height1.lastIndexOf(" ") + 1);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

I'd split the string by spaces first:

How to split a String by space

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] );
Community
  • 1
  • 1
Derek
  • 7,615
  • 5
  • 33
  • 58
0

These two lines:

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

don't do what you think they do.

I think you're expecting that after they run, h1 and h2 are now Strings. But that isn't what happens. Variables can't change type. h1 and h2 remain ints.

Rather than having a side effect, Integer.toString returns a result. Currently you are simply discarding tht result. You need to do something with the result, such as store it in a new variable. Maybe something like this:

String h1AsString = Integer.toString(h1);
String h2AsString = Integer.toString(h2);
Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
0

You are trying to use substr() on an int. Also, when you call Integer.toString(h1); that does not set the variable to a string. To do so, it would have to be:

feet1 = Integer.toString(h1)

feet1 = feet1.substr(0, 1); 

Hope this helps

Ducksauce88
  • 640
  • 3
  • 12
  • 26
0

Try this: (Note that I hardcoded the inputs for easier testing in a console app)

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

Also, it's substring not subtract or substr.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189