How to parse a string for numbers and words (strings)? IN JAVA - string

I have a list of strings that contain names and a series of numbers, ex: "John James Hollywood 1 2 3".
I want to parse out the names using a loop so that I can scan each part of the text and store it in a new string variable. Then I tried to use a conditional to put any numbers in a different variable. I will calculate the average of the numbers and then print the name and the average of the numbers, so the output looks like this:
John James Hollywood: 2
I don't know whether to use the input string stream or what my first steps are.
public static String parseNameNumbers(String text)
{
Scanner scnr = new Scanner(text);
Scanner inSS = null;
int numSum = 0;
int countNum = 0;
String newText = new String();
String sep = "";
while(scnr.hasNext()) {
if(scnr.hasNextInt()) {
numSum = numSum + scnr.nextInt();
countNum++;
}
else {
newText = newText + sep + scnr.next();
sep = " ";
}
}
return newText + ": " + (numSum/countNum);
}

Related

How to rearrange the string in C#

\In my csv file have two row data :
\a;b;c;d;e;
\1;2;3;4;5;
\How do I make it as below in c#:
\a=1;enter code here
\b=2;enter code here
\c=3;enter code here
\d=4;enter code here
\e=5;enter code here
tldr; you split the text.
got string from csv //e.g. string originalTxt = #"\a;b;c;d;e; \1;2;3;4;5";
split row
rearrange/edit for each word in each row
combine
string resultTxt = "";
string mycodehere ="enter code here ";
string originalTxt = ...; //TODO :: your code for get text from csv
string[2] rows = originalTxt.Split('\'); // split the \ got 2 rows
string[] row1 = rows[0].Split(';'); //split each word
string[] row2 = rows[1].Split(';'); //split each word
for(int i= 0 ; i< row1.Count ; i++){
resultTxt += #"\" + row1[i] + "=" + row2[i] + ";" + mycodehere + " " ;
}
//TODO :: use resultTxt

How to reverse a string with letters and numbers so that only the letters extract

How can I reverse the string which includes numbers and letters, but I want to output the letters in reverse order. (Without using string functions)
string = 'Hellow 432'
length = len(string)
output = ""
while length>0:
output += string[length-1]
length = length-1
print (output)
String details = "Hellow 432";
string numeric = "";
string nonnumeric = "";
char[] mychar = details.ToCharArray();
foreach (char ch in mychar)
{
if (char.IsDigit(ch))
{
numeric = numeric + ch.ToString();
}
else
{
nonnumeric =ch.ToString()+ nonnumeric;
}
}
return nonnumeric + numeric;
}

Code for creating string in a loop to report numbers

I created a code that functions as it is supposed to. I determined the escape function to be -1 for the user to exit the program and used if/else to only add the sum of positive integers.
I know that I have to save the numbers that pass the if statement (only positive numbers) and the only way that I can think of doing this is through a String.
Unfortunately, whenever I attempt to add a string as part of the while loop, it will print the statement over and over again when I only want a single line.
I'm also struggling to set the user input to a single line. I know it has everything to do with the .nextLine() command, but if I pull it outside the brackets (which I've attempted to do) then it reads as an error.
Actually, a source about conversion of Strings to characters or inputs to Strings would be very helpful as well. It's apparent that this is where a good portion of my understanding is lacking.
public static void main(String args[])
{
int userNum = 0;
int sum = 0;
Scanner s = new Scanner(System.in);
String str3;
System.out.print("Enter positive integers (to exit enter -1):\n ");
//Loop for adding sum with exit -1
while(userNum != -1){
//condition to only calculate positive numbers user entered
if(userNum > 0){
//calculation of all positive numbers user entered
sum += userNum;
str3 = String.valueOf(userNum);}
userNum = s.nextInt();
}
System.out.println("The values of the sum are: " + str3);
System.out.println("The Sum: " + sum);
}
}
I'm hoping for the user input to be printed,
Enter positive integers (to exit enter -1): _ _ ___//with the user
input in the same row.
And...
values from string to read out on same line, not multiple lines.
The variable str needs to be initialized as:
String str3 = "";
and in the loop, each entered number must be concatenated to str.
int userNum = 0;
int sum = 0;
Scanner s = new Scanner(System.in);
String str3 = "";
System.out.print("Enter positive integers (to exit enter -1):\n ");
while (userNum != -1) {
userNum = s.nextInt();
if (userNum > 0) {
sum += userNum;
str3 += " " + userNum;
}
}
System.out.println("The values of the sum are: " + str3);
System.out.println("The Sum: " + sum);

Write function for appending space between currency and number

C# code for formatting a number with commas and decimal
public class Program
{
public static void Main(string[] args)
{
string Str = null;
Str = string.Format(CultureInfo.InvariantCulture,"{0:c}", 9989.87);
Console.WriteLine(Str);
}
}
Output is: $9,989.87 but i want as $ 9,989.87
To avoid some of the culture specific settings, you can try specifying the format:
string str = 9989.87.ToString("$ #,##0.00"); // or = $"{9989.87:$ #,##0.00}"; in VS 2015
or better
var str = "$ " + 9989.87.ToString("n", System.Globalization.CultureInfo.InvariantCulture);
Visual Studio 2015 interpolated string versions:
var str = System.FormattableString.Invariant($"$ {-9989.87:n}"); // "$ -9,989.87"
or with different formats for negative and zero values with the ; section separator:
var ss = $"{-9989.87:$ #,##0.00;-$ #,##0.00;-}"; // "-$ 9,989.87"

How to split a string into multiple strings if spaces are detected (GM:Studio)

I made a console program, but the problem is that it doesn't allow parameters to be inserted. So I'm wondering how would I split a single string into multiple strings to achieve what I need. E.g.: text="msg Hello" would be split into textA="msg" and textB="Hello"
This is the main console code so far (just to show the idea):
if (keyboard_check_pressed(vk_enter)) {
text_console_c = asset_get_index("scr_local_"+string(keyboard_string));
if (text_console_c > -1) {
text_console+= "> "+keyboard_string+"#";
script_execute(text_console_c);
text_console_c = -1;
}
else if (keyboard_string = "") {
text_console+= ">#";
}
else {
text_console+= "> Unknown command: "+keyboard_string+"#";
};
keyboard_string = "";
}
I cant recommend spliting string with iteration by char, because when u try split very very very long string, then time to split is very long and can freeze thread for a short/long time. Game maker is single threaded for now.
This code is much faster.
string_split
var str = argument[0] //string to split
var delimiter = argument[1] // delimiter
var letDelimiter = false // append delimiter to each part
if(argument_count == 3)
letDelimiter = argument[2]
var list = ds_list_create()
var d_at = string_pos(delimiter, str)
while(d_at > 0) {
var part = string_delete(str, d_at , string_length(str))
if(letDelimiter)
part = part + delimiter
str = string_delete(str, 1, d_at)
d_at = string_pos(delimiter, str)
ds_list_add(list, part)
if(d_at == 0 && str != "")//last string without delimiter, need to add too
ds_list_add(list, str)
}
return list;
Dont forget ds_list_destroy after you iterate all strings
for example:
var splited = string_split("first part|second part", '|')
for(splited) {
//do something with each string
}
ds_list_destroy(splited)
Something like this may help, haven't tested it out but if you can follow what is going on its a good place to start.
Text = "msg Hello"
counter = 0
stringIndex = 0
for (i = 0; i < string_length(text); i++)
{
if string_char_at(text,i) == " "
{
counter++
stringIndex = 0
} else {
string_insert(string_char_at(text,i),allStrings(counter),stringIndex)
stringIndex++
}
}
allStrings should be an array containing each of the separate strings. Whenever a " " is seen the next index of allStrings starts having it's characters filled in. stringIndex is used to add the progressive characters.

Resources