This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 2 years ago.
I want to get a user input using a JOptionPane input box and split the user's input into 2 sections. (I'm a student so I have to use JOptionPane.)
For example, I want to get the start time, 20:54, and split it to
startHour = 20;
startMin = 54;
you can use the split function of String class which return an array of string values then index 0 contain the hour, index 1 contain the minutes:
note: you need to cast the string value to int
String value = "20:54";
String [] parts = value.split(":");
int startHour = Integer.parseInt(parts[0]);
int startMin = Integer.parseInt(parts[1]);
Related
This question already has answers here:
Splitting and converting String to int
(5 answers)
Closed 3 years ago.
String code = "U 12 24";
int s = Integer.parseInt(String.valueOf(code.charAt(2)));
System.out.println(s);
that would be print 1,
however, i want to try print 12 or i mean i want take 2 digits number, but i can't do it because the only way i know is just take one digit number.
how if i want take 12 and convert to int
int s = Integer.parseInt(String.valueOf(code.substring(2, 4)));
If you want to get all digits in a given string, you have to tokenize the string by space and parse every chunk into a number.
This question already has answers here:
How do I use a dot as a delimiter?
(2 answers)
How do I use a delimiter with Scanner.useDelimiter in Java?
(3 answers)
Closed 5 years ago.
I want to make a user to enter integers separated with dot like (11.56.98)
to use after that x=11 y=56 z=98
`Scanner s = new Scanner(System.in);
System.out.print("Enter Your number like (36.52.10): ");
int x = s.nextInt();
int y = s.nextInt();
int z = s.nextInt();`
now how to usedelimiter will change whitespace to dot and how to return to whitespace again
If you want it in the same line use: s.next().
If you want the text in the next line you need to do: s.nextLine()
Whatever method you use it will return a java.lang.String.
You can use String[] numbers = yourString.split(".")
The array what you get if you split the string you can get all numbers:
int x = Integer.valueOf(numbers[0]);
int y = Integer.valueOf(numbers[1]);
int z = Integer.valueOf(numbers[2]);
//Dont forget it can throw a NumberFormatException if the current String is not a valid number.
This question already has answers here:
How to increment the number in a String by 1?
(7 answers)
Closed 5 years ago.
For example:
public static void main(String[] args)
{
String a="1";
int inc= Integer.parseInt(a+1);
System.out.println(inc);
}
I'm getting 11 but i want to get 2. How can i do it in a very efficient way?
Integer.parseInt(a+1); parses the String that results from concatenating the value of the String a ("1") to the int literal 1, which is "11".
Change it to
int inc = Integer.parseInt(a) + 1;
This way "a" would be parsed to the integer 1 and then 1 would be added to it to give you the value 2.
Since a is a String object this operation is not giving the desired input
Integer.parseInt(a+1);
because will be equivalent to do
Integer.parseInt("1"+"1");
or
Integer.parseInt("11");
you need to parse the string first and then increment
Integer.parseInt(a)+1
This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Closed 7 years ago.
If I ask the user for input, how do I put it as the length of an array?
Example:
String teamnum = Input.nextLine();
int teams[]=new int[teamnum];
You need to cast the input as a integer also that's not the way you declare arrays. Try this:
String teamnum = Input.nextLine();
int newnum = Integer.parseInt(teamnum);
int[] teams = new int[newnum];
This question already has answers here:
How to Convert an int to a String? [duplicate]
(6 answers)
Closed 8 years ago.
I'm trying to create a method which generates a 4 digit integer and stores it in a string.
The 4 digit integer must lie between 1000 and below 10000. Then the value must be stored to PINString.
Heres what I have so far. I get the error Cannot invoke toString(String) on the primitive type int. How can I fix it?
public void generatePIN()
{
//generate a 4 digit integer 1000 <10000
int randomPIN = (int)(Math.random()*9000)+1000;
//Store integer in a string
randomPIN.toString(PINString);
}
You want to use PINString = String.valueOf(randomPIN);
Make a String variable, concat the generated int value in it:
int randomPIN = (int)(Math.random()*9000)+1000;
String val = ""+randomPIN;
OR even more simple
String val = ""+((int)(Math.random()*9000)+1000);
Can't get any more simple than this ;)
randomPIN is a primitive datatype.
If you want to store the integer value in a String, use String.valueOf:
String pin = String.valueOf(randomPIN);
Use a string to store the value:
String PINString= String.valueOf(randomPIN);
Try this approach. The x is just the first digit. It is from 1 to 9.
Then you append it to another number which has at most 3 digits.
public String generatePIN()
{
int x = (int)(Math.random() * 9);
x = x + 1;
String randomPIN = (x + "") + ( ((int)(Math.random()*1000)) + "" );
return randomPIN;
}