Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I am doing an assignment and I have to do several simple string manipulations. I think I have the last one figured out, and it works by itself, but it does not work when I put it together with the other string manipulations, giving me an arrayoutofbounds exception error. Any advice?
Here is the small code I made
public static void main (Strings[] args) {
Scanner sc = new Scanner(System.in);
String theSentence = sc.nextLine();
String [] theWords = theSentence.split(" ");
Arrays.sort(theWords);
System.out.println(theWords[1]);
System.out.println(Arrays.toString(theWords));
}
This does not work when it is put together with the rest of the code even though it works by itself. For reference, this code is supposed to take in a small sentence and give me the smallest word lexicographically. Ex: input: "4 WHAT WAIT IS THIS" output would be "IS"
The code is assuming that theWords will always have at least two elements in it. If the sentence provided by the user does not have any spaces, theWords will never get an element in position 1 and so the program will crash on System.out.println(theWords[1]);
In Java arrays are indexed from zero, where the 0th element is the first.
As Mark said you are assuming that there are always two words entered in this case. You should check the length of theWords before trying to access an element.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Here is my code, I am supposed to read in data such as AAABBBCCCDDD and output a3b3c3d3.
I have updated the code, and now the code compiles and runs, however nothing is output. I dont know if its the way im reading data in or if the code is incorrect.
String text;
FileReader data = new FileReader("input.txt");
BufferedReader in = new BufferedReader(data);
text=in.readLine();
in.close();
//Counter looks at length of data
int counter=0;
//Counter2 looks at current letter or number to make see if its the same then iterates it
int counter2=0;
while (text.charAt(counter)<=text.length())
{
while (text.charAt(counter)==text.charAt(counter2+1))
{
counter2++;
}
System.out.println(text.charAt(counter) + counter2);
counter=counter2;
}
The reason the compiler is complaining is because in this loop:
while (in.readLine()!=null)
{
text = in.readLine();
}
it's possible that the body of the loop will never get executed, which means it's possible that text will never be set to anything. And the Java compiler doesn't like it when you use a variable that may not have been set to anything.
But the whole loop is wrong anyway. You're only using one input string, so why is this a loop? Before we can help fix this problem, we need to know what you're trying to accomplish. And if you really do want a loop, it would be wrong to call in.readLine() twice as you have above, since that means it will read two lines each time through the loop.
Assuming you've properly imported all the java.io classes, here are the two problems causing compilation to fail:
String text;
This must be initialized to something. Such as
String text = null;
Possibly setting it in the while loop is not good enough.
The other problem is this variable, output doesn't exist anywhere, which is why this line won't compile:
System.out.println(output);
I think you mean for it to be text
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm doing a small project in Java whilst I'm learning the language.
Basically what I'd like is for a user to input a string via JOptionPane.showInputDialog();, if that string is empty, I want it to make them re-enter a valid string and then the program will continue.
What I did consider doing is using goto but I read up on it and it said it's not good practice.
Any help would be greatly appreciated.
Thanks :)
I would do it in an infinite loop asking for input (using OptionPane.showInputDialog()).
Here you have some pseudo code:
message = ""
while message.equals(""):
message = ask_for_input() // (.trim() if needed)
end
You could do it in a loop. So you take a while-loop where the condition is that the input is empty. Then the message will pop up until the user entered something.
Despite goto is a reserved keyword in Java, it's actually (implicitly) meant for not being used.
JOptionPane.showInputDialog(...) returns a String.
I suggest using an infinite while loop and comparing it to empty, through String.isEmpty().
For instance:
String input = null;
while (true) {
input = JOptionPane.showInputDialog("Your input (not empty):");
if (!input.trim().isEmpty()) {
// TODO something with the input variable
break;
}
}
This is what do ... while loops are for. You could also use the Apache commons StringUtils class.
String input;
do {
input = JOptionPane.showInputDialog("Please enter your string");
} while (StringUtils.isBlank(input));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a String which contain the duplicate word,i need to calculate the only duplicate word in the string which are occures simultaneously.
String a="am abc am am am xyz as xyz xyz xyz xyz";
above string i need to count am which occures simultaneously i.e. am occures 3 time and xyz occures 4 time and in this i need to display only xyz=4 which is maximum simultaneously repeated. can any one help me please.
1. Create a HashMap of Strings and Integer.
2. Split the given String with the delimiter and create an array of Strings[].
3. Loop through the array of Strings[] and check if the String exists in HashMap,
3.1 if yes, increment the value by 1.
KeyWords here:
HashMap
Split the Strings
Hope this helps.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For this string, 16.82080560, 96.13055810 I want to get
"String one = 16.82080560"
and
"String two = 96.13055810"
in android.
Admin that I suck in string manipulation and regex.
Please let me know how can I get such two value from a string.
String[] components = original.split(",");
If the Strings are always separated by a comma you can use String.split
For a better regex pattern see the comment from #npinti:
Minor side note, it might be better to do \\s*,\\s* instead of just ,.
Just , might cause problems should the OP wish to cast these to
floats, since the extra white space at the beginning of the second
number will most likely not be recognized as a proper number.
Another option:
String str = "16.82080560, 96.13055810";
StringTokenizer st = new StringTokenizer(str,", ");
String one = st.nextToken();
String two = st.nextToken();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this xml file from where I'm reading this string,
http://localhost:8080/sdpapi/request/10/notes/611/
My question is how can I get just the 611, which is of variable, can be 100000, for example, from this string?
Split the string
String input = "http://localhost:8080/sdpapi/request/10/notes/611/";
String output = input.split("notes/")[1].split("/")[0];
output is the value you need
What language?
Anyway, in most cases it's a syntax like:
String.substring(begin, length);
... where 'begin' is the number of the letter in the string-1. For extracting http from the above string you would write
substring(0, 4);
In case you always need the last string between the last two '/'s, you can retrieve the position of the slashes with index-functions (as stated in the answer of #Liran for example).
// EDIT: In Java the second parameter of substring is not length, but endIndex:
String s = "http://localhost:8080/sdpapi/request/10/notes/611/";
s.substring(46, s.lastIndexOf('/'));
It depends on programming language you use, but Regular Expressions should be the same in most of them:
/(\d+)\/$/
well, it depend in what language are you writing... in c# for example
string s = #"http://localhost:8080/sdpapi/request/10/notes/611/";
s.SubString(s.LastIndexOf('/'));
or
Path.GetFileName(s);
for java
new File(s).getName();