Scanner issue with math text and too many tokens - java.util.scanner

So, i am trying to make a java, text based calculator but scanner is giving me a hell of a headache. Here is a code snipit...
Scanner input=new Scanner(in);
while (input.hasNext()){
System.out.print(input.next()+".");
}
System.out.print("\n");
My input is this: "1 + cake(3,4,6,10*30+sin(4)) * 3"
and my output SHOULD be this: "1.+.cake(3,4,6,10*30+sin(4)).*.3."
Scanner should only tokenize the whitespace, but this is what i get:
"1.+.cake.(.3.,.4.,.6.,.10..30.+.sin.(.4.).)..3."
for some reason it removes every space, but also separates every single word, number and bit of punctuation!
Now i have done some homework and tried various delimiters using input.useDelimiter(String in).
Some of the ones i have used are: "\s" "\s+" "\s*" "\s*+" "[ ]" and many more but to no avail. what am i doing wrong!?!?!?!?!

Try using the StringTokenizer instead.
public class demo
{
public static void main(String args[])
{
StringTokenizer st = new StringTokenizer("1 + cake(3,4,6,10*30+sin(4)) * 3");
while (st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
}
}

Related

Get String Between 2 Strings with Arduino

I am looking for a way to get a String between 2 Strings using Arduino. This is the source String:
Hello, my name is John Doe# and my favourite number is 32#.
The output has to be:
String name = "John Doe"; //Between "name is " and "#"
String favouriteNumber = "32"; //Between "number is " and "#"
How can this be achieved with Arduino?
I am not able to find any information online about this. Those examples for C are not working anyway. I understand that using String is not recommended in Arduino, but I have to do it this way to make things simpler.
By the way, this method of using a '#' to indicate the end of the data is not an ideal way to do it as I would like the input to be more human readable and more natural. Would anyone please suggest another way to do this as well?
Thanks in advance!
Function midString find the substring that is between two other strings "start" and "finish". If such a string does not exist, it returns "". A test code is included too.
void setup() {
test();
}
void loop() {
delay(100);
}
String midString(String str, String start, String finish){
int locStart = str.indexOf(start);
if (locStart==-1) return "";
locStart += start.length();
int locFinish = str.indexOf(finish, locStart);
if (locFinish==-1) return "";
return str.substring(locStart, locFinish);
}
void test(){
Serial.begin(115200);
String str = "Get a substring of a String. The starting index is inclusive (the corresponding character is included in the substring), but the optional ending index is exclusive";
Serial.print(">");
Serial.print( midString( str, "substring", "String" ) );
Serial.println("<");
Serial.print(">");
Serial.print( midString( str, "substring", "." ) );
Serial.println("<");
Serial.print(">");
Serial.print( midString( str, "corresponding", "inclusive" ) );
Serial.println("<");
Serial.print(">");
Serial.print( midString( str, "object", "inclusive" ) );
Serial.println("<");
}
just searched for this and saw no answer so i cooked one up.
i prefer working with String as well because of code readability and simplicity.
for me its more important than squeezing every last drop of juice out of my arduino.
String name = GetStringBetweenStrings("Hello, my name is John Doe# and my favourite number is 32#." ,"name is ","#");
String GetStringBetweenStrings(String input, String firstdel, String enddel){
int posfrom = input.indexOf(firstdel) + firstdel.length();
int posto = input.indexOf(enddel);
return input.substring(posfrom, posto);
}
watch out for the first case its fine, but for the second one you would have to change the second filter sting to "#." so it doesn't use the first occurrence of the #

Parsing a CSV string and using elements of the string for computation- homework

I am very new to java, and this is homework. Any direction would be appreciated.
The assignment is to read an external text file and then parse that file to produce a new file.
The external file looks something like this:
2 //number of lines in the file
3,+,4,*,2,-.
5,*,2,T,1,+
I have to read this file and produce an output that takes the preceding int value and prints the following character (skipping the comma). So the output would look like this:
+++****--
*****TT+
I have tried to setup my code using two methods. The first to read the external file (passed as a parameter) which, as long as there is a next line, will call a second method, processLine, to parse the line. This is where I am lost. I can't figure out how this method should be structured so it reads the line and interprets the token values as either ints or chars, and then executes code based on those values.
I am only able to use what we have covered in class, so no external libraries, just the basics.
public static void numToImageRep(File input, File output) //rcv file
throws FileNotFoundException {
Scanner read = new Scanner(input);
while(read.hasNextLine()){ //read file line by line
String data = read.nextLine();
processLine(data); //pass line for processing
}
}
public static void processLine(String text){ //incomplete, all falls apart here.
Scanner process = new Scanner(text);
while(process.hasNext()){
if(process.hasNextInt()){
int multi = process.nextInt();
}
if(process.hasNext()==','){
}
}
this method can be a simple example that can do the job:
public static String processLine(String text){
String result = "";
String[] splitted = text.split(",");
int remaining = 0;
for(int i=0;i<splitted.length;i+=2)
{
remaining = (Integer.parseInt(splitted[i]));
while( remaining-- >0)
result += splitted[i+1];
}
return result;
}

Finding sub-strings in Java 6

I looked through the String API in Java 6 and I did not find any method for computing how many times a specific sub-string appears within a given String.
For example, I would like to know how many times "is" or "not" appears in the string "noisxxnotyynotxisi".
I can do the long way with a loop, but I would like to know whether there is a simpler way.
Thanks.
Edit: I'm using Java 6.
org.apache.commons.lang.StringUtils.countMatches method could be preferred.
Without using an external library, you can use String.indexOf(String str, int fromIndex); in a loop.
Update This example fully works.
/**
* #author The Elite Gentleman
* #since 31 March 2011
*
*/
public class Test {
private static final String STR = "noisxxnotyynotxisi";
public static int count(String str) {
int count = 0;
int index = -1;
//if (STR.lastIndexOf(str) == -1) {
// return count;
//}
while ((index = STR.indexOf(str, index + 1)) != -1) {
count++;
}
return count;
}
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(Test.count("is"));
System.out.println(Test.count("no"));
}
}
You can do this, but a loop would be faster.
String text = "noisxxnotyynotxisinono";
String search = "no";
int count = text.split(search,-1).length-1;
System.out.println(Arrays.toString(text.split(search,-1)));
System.out.println("count= " + count);
prints
[, isxx, tyy, txisi, , ]
count= 5
As you can see this is correct if the text starts or ends with the search value. The -1 argument stops it removing trailing seperators.
You can use a loop with indexOf() which is more efficient, but not as simple.
BTW: Java 5.0 has been EOL since Aug 2007. Perhaps its is time to look at Java 6. (though the docs are very similar)

Hadoop... Text.toString() conversion problems

I'm writing a simple program for enumerating triangles in directed graphs for my project. First, for each input arc (e.g. a b, b c, c a, note: a tab symbol serves as a delimiter) I want my map function output the following pairs ([a, to_b], [b, from_a], [a_b, -1]):
public void map(LongWritable key, Text value,
OutputCollector<Text, Text> output,
Reporter reporter) throws IOException {
String line = value.toString();
String [] tokens = line.split(" ");
output.collect(new Text(tokens[0]), new Text("to_"+tokens[1]));
output.collect(new Text(tokens[1]), new Text("from_"+tokens[0]));
output.collect(new Text(tokens[0]+"_"+tokens[1]), new Text("-1"));
}
Now my reduce function is supposed to cross join all pairs that have both to_'s and from_'s
and to simply emit any other pairs whose keys contain "_".
public void reduce(Text key, Iterator<Text> values,
OutputCollector<Text, Text> output,
Reporter reporter) throws IOException {
String key_s = key.toString();
if (key_s.indexOf("_")>0)
output.collect(key, new Text("completed"));
else {
HashMap <String, ArrayList<String>> lists = new HashMap <String, ArrayList<String>> ();
while (values.hasNext()) {
String line = values.next().toString();
String[] tokens = line.split("_");
if (!lists.containsKey(tokens[0])) {
lists.put(tokens[0], new ArrayList<String>());
}
lists.get(tokens[0]).add(tokens[1]);
}
for (String t : lists.get("to"))
for (String f : lists.get("from"))
output.collect(new Text(t+"_"+f), key);
}
}
And this is where the most exciting stuff happens. tokens[1] yields an ArrayOutOfBounds exception. If you scroll up, you can see that by this point the iterator should give values like "to_a", "from_b", "to_b", etc... when I just output these values, everything looks ok and I have "to_a", "from_b". But split() don't work at all, moreover line.length() is always 1 and indexOf("") returns -1! The very same indexOf WORKS PERFECTLY for keys... where we have pairs whose keys contain "" and look like "a_b", "b_c"
I'm really puzzled with all this. MapReduce is supposed to save lives making everything simple. Instead I spent several hours to just localize this.
NOt sure if that's the problem by try changing this:
String [] tokens = line.split(" ");
to this:
String [] tokens = line.split("\t");

Is there a .NET function to remove the first (and only the first) occurrence at the start of a string?

I was using the TrimStart function to do the following:
var example = "Savings:Save 20% on this stuff";
example = example.TrimStart("Savings:".ToCharArray());
I was expecting this to result in example having a value of "Save 20% on this stuff".
However, what I got was "e 20% on this stuff".
After reading the documentation on TrimStart I understand why, but now I'm left wondering if there is a function in .NET that does what I was trying to do in the first place?
Does anyone know of a function so I don't have to create my own and keep track of it?
I don't think such a method exists but you can easily do it using StartsWith and Substring:
s = s.StartsWith(toRemove) ? s.Substring(toRemove.Length) : s;
You can even add it as an extension method:
public static class StringExtension
{
public static string RemoveFromStart(this string s, string toRemove)
{
if (s == null)
{
throw new ArgumentNullException("s");
}
if (toRemove == null)
{
throw new ArgumentNullException("toRemove");
}
if (!s.StartsWith(toRemove))
{
return s;
}
return s.Substring(toRemove.Length);
}
}
No, I don't believe there's anything which does this built into the framework. It's a somewhat unusual requirement, IMO.
Note that you should think carefully about whether you're trying to remove "the first occurrence" or remove the occurrence at the start of the string, if there is one. For example, think what you'd want to do with: "Hello. Savings: Save 20% on this stuff".
You can do that quite easily using a regular expression.
Remove the occurrence on the beginning of the string:
example = Regex.Replace(example, #"^Savings:", "");
Remove the first occurrence in the string:
example = Regex.Replace(example, #"(?<!Savings:.*)Savings:", "");

Resources