1

I'm looking to have a string.split have 4 parts name1, age1, name2, age2 but turning the "String age1 = parts[1];" and "String age2 = parts[3];" into intergers so I can sort people by age.

Any help would be appreciated!

My code so far:

import java.util.Scanner;

public class CharTIntStringSplit
{
   public static void main(String[] args) {

       Scanner kb = new Scanner(System.in);

       String nameage;

       System.out.println("Enter Firstname and age seperated by , e.g. Firstname, Age");
       nameage = kb.nextLine();

       String[] parts = nameage.split(",");
       String name1 = parts[0];
       String age1 = parts[1];
       String name2 = parts[2];
       String age2 = parts[3];/*after age1 and age2 are converted to 
                              intergers the if statement underneith should work*/

       if (age1 >= age2) {
          System.out.println(name1 + " " + age1 + ", " + name2 + " " + age2);

        }

       else if (age1 < age2)  {
           System.out.println(name2 + " " + age2 + ", " + name1 + " " + age1);

       }
    }
}
4
  • 1
    Look into the parseInt method of the Integer class. Commented Mar 20, 2014 at 0:35
  • I have tried that, I couldn't really understand as I'm a beginner, could you maybe link to a post that explains it simply for me =) Thanks Commented Mar 20, 2014 at 0:38
  • 1
    I could google it for you, yes. I'm not going to. Commented Mar 20, 2014 at 0:41
  • Lol thnx anyway, haha! Commented Mar 20, 2014 at 0:43

3 Answers 3

1

Use Integer.parseInt(String s) method to convert a valid number string to int value. Here is a sample:

int intAge1 = Integer.parseInt(age1);

Note that this method throws NumberFormatException in case the input string is not a valid number.

Sign up to request clarification or add additional context in comments.

3 Comments

@SamsinOzo Glad it helped..just accept the answer by clicking on the tick mark left to my answer. Accepting an answer helps others facing the same problem.
Yeah I tried to earlier but it wouldn't allow me since it had only been 6 minutes, thanks again for the help
Already hit another issue with it while trying to loop the string, I've hit this issue before but fixed it by firing an empty Scanner.nextLine doing that this time did not fix the error, I think it's because 1st time it only had 2 parts, this one has 4, time to research! :3
1

do this:

int age1 = Integer.valueOf(parts[1]).intValue();

Integer.valueOf(parts[1]) will convert the string to the object Integer. the .intValue() method will convert the Integer object to the primitive int type.

do the same to the attribute age2.

Good luck!

Comments

0

You should be able to convert the String to int using

int x = Integer.parseInt("String that you want to convert to int");

I would suggest you to look into the API and see the java docs on how this works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.