Question

I am working on some data structures in java and I am a little stuck on how to split this string into two integers. Basically the user will enter a string like '1200:10'. I used indexOf to check if there is a : present, but now I need to take the number before the colon and set it to val and set the other number to rad. I think I should be using the substring or parseInt methods, but am unsure. The code below can also be viewed at http://pastebin.com/pJH76QBb

import java.util.Scanner;  // Needed for accepting input

 public class ProjectOneAndreD
 {
    public static void main(String[] args)
    {
        String input1;
        char coln = ':';
        int val=0, rad=0, answer=0, check1=0;

        Scanner keyboard = new Scanner(System.in);  //creates new scanner class
        do
        {
            System.out.println("****************************************************");
            System.out.println("             This is Project 1. Enjoy!              ");     //title
            System.out.println("****************************************************\n\n");

            System.out.println("Enter a number, : and then the radix, followed by the Enter key.");
            System.out.println("INPUT EXAMPLE:  160:2   {ENTER} ");     //example

            System.out.print("INPUT:  ");               //prompts user input.
            input1 = keyboard.nextLine();       //assigns input to string input1


            check1=input1.indexOf(coln);

            if(check1==-1)
            {
                System.out.println("I think you forgot the ':'.");

            }
            else
            {
                System.out.println("found ':'");

            }
        }while(check1==-1);
    }
 }
Was it helpful?

Solution

make this

    if(check1==-1)
    {
        System.out.println("I think you forgot the ':'.");

    }
    else
    {
     String numbers [] = input1.split(":"); //if the user enter 1123:2342 this method 

     //will
     // return array of String which contains two elements numbers[0] = "1123" and numbers[1]="2342"
    System.out.print("first number = "+ numbers[0]);
    System.out.print("Second number = "+ numbers[1]);
    }

OTHER TIPS

Substring would work, but I would recommend looking into String.split.

The split command will make an array of Strings, which you can then use parseInt to get the integer value of.

String.split takes a regex string, so you may not want to just throw in any string in it.

Try something like this:

"Your|String".split("\\|");, where | is the character that splits the two portions of the string.

The two backslashes will tell Java you want that exact character, not the regex interpretation of |. This only really matters for some characters, but it's safer.

Source: http://www.rgagnon.com/javadetails/java-0438.html

Hopefully this gets you started.

You knew where : is occurs using indexOf. Let's say string length is n and the : occurred at index i. Then ask for substring(int beginIndex, int endIndex) from 0 to i-1 and i+1 to n-1. Even simpler is to use String::split

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top