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.
Related
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]);
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:
Extracting digits of int in Java
(15 answers)
Closed 6 years ago.
So I'm in a process of writing a small encrypter program. I start by asking the user to enter a 4 digit number (>= 1000 and <= 9999). I'd like to extract each digit from the number entered by the user, and store them in a variable.
My assignment gives me hint which doesn't help me much (Hint: integer division and the modulus might be useful here).
The "hint" is actually a nearly direct explanation of how to do what you need to do: dividing in integers by ten gets rid of the last digit, while obtaining modulo ten gives you the last digit:
int x = 12345;
int lastDigit = x % 10; // This is 5
int remainingNumber = x / 10; // This is 1234
Do this in a loop until the remaining number is zero. This gives you digits of the number in reverse.
You can use Integer.toString(int) and then address each digit by index.
int x = 1234;
char[] s = Integer.toString(x).toCharArray();
System.out.println(s[0]);
System.out.println(s[1]);
System.out.println(s[2]);
System.out.println(s[3]);
If you don't want to convert your string to an array you can use the .charAt(int n) method on the string.
int x = 1234;
String s = Integer.toString(x);
System.out.println(s.charAt(0));
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;
}