Split function giving incorrect result in java [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
import java.util.*;
class Solution {
public static void main(String d[]) {
Scanner sc=new Scanner(System.in);
String s0=sc.next();
String p[]=s0.split(" ");
System.out.println(p.length);
}
}
I am using jdk version 8 and when i am giving input as "hello world java", it is printing length as 1 ,whereas it should print the length as 3,please help me in resolving this

Scanner.next() will get the input from the user till a space is encountered. For the input "hello world java" it only assign "hello"
If you want to include spaces also use Scanner.nextLine()

Related

Why won't this scanner accept input on the println? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
When my application compiles, it will not accept the input for roomNum on the same line in which it asks, "Please enter a room to search for:"
System.out.println();
if(roomNum < 0);
{
System.out.println("Please enter a room to search for: ");
roomNum = input.nextInt();
}
If I just use next instead of nextInt, it doesn't compile correctly.
The code above works, but will not accept the input on the same line which is the functionality I need.
Two things: Remove semi-colon after if condition and use System.out.print() instead if you want input on the same line.

I need to get two numbers of a string like like this "5+7", but when im using .split(\\+) only get the first, how can i get both? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
String x = "5+7";
String []n = x.split("\\+");
System.out.println(n[0]); // =5
System.out.println(n[1]); // =\
Your code should work fine, but I'd try to make it more robust:
String test = "5 + 7";
String[] tokens = test.split("\\s*\\+\\s*");
for (String token : tokens) {
System.out.println(token);
}
The \\s* will allow for possible white-space between the numbers and the + char.
use
x.split("[+]");
That will split it correctly.

Need help using subString [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
public class project5{
public static void main(String args[]){
String[] storage = {"123457897", "123456","654654654"};
int current;
current = Integer.parseInt(storage[1].subString(1,5));
System.out.println(current);
}
}
So I'm trying to, as an exercise, just get the first 5 numbers in the first thing of the array and parse it as an integer and store it as the variable current. It gives me the error:
test.java:5: error: cannot find symbol
current = Integer.parseInt(storage[1].subString(1,5));
^
symbol: method subString(int,int)
location: class String
1 error
What is it that I'm doing wrong?
There is no subString method on the String class. There only is substring (all lowercase).
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
For variable, class and method names casing matters.

Why assertions in Java is not working? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I tried to use assertions in java from an example put forth from a site but it is not throwing an Assertion Error even if it does not conform to the requirements.
Where am I doing wrong?
package Sources;
import java.util.Scanner;
public class MyAssertion
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your age");
int age = scanner.nextInt();
assert age>=18:"Not Valid !!";
System.out.println("Age is -- "+age);
}
}
I have also used the following command :
java -ea -cp ./classes Sources.MyAssertion
But even if I enter the age as 33 it is not throwing an error . Why?
Any help is much appreciated :)
Assertions fire when the condition is not true. If you enter 33 then that is greater than equal to 18, so all is well.

Illegal format string in TextIO.putf() method aka wrong Array Type? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I guess its a rather simple question but i just cant find my mistake.
int[] myIntArray = new int[20];
myIntArray[5] = 5;
int a = myIntArray[5];
TextIO.putf("arr[i]: d%",a );
The error I get is Illegal format string in TextIO.putf() method.
So I assume the value at the index 5 is not an int?
The error message says exactly what the problem is: your format string is wrong. You probably meant %d (or better yet, %d%n to add a newline).
The format string elements are in the form
%[modifiers]type
not
something%
Change d% to %d.

Categories