Reading values from user in C# - java

This code works fine, if the user enters the numbers in separate lines
i.e.
10
12
Result is : 22
However, when I try to input this there is an error.
10 12
Now, I know console.readline() reads the entire line, and obviously the space isn't an int and so I get the error. However, when I used to code in C, there was scanf function, which by simply specifying the datatype to expect from user, could run for both the cases. Also, I think JAVA's scanner.nextInt() would have not allowed such troubles. Is there any simple way to ask c# to read data separated by spaces, or lines in the same manner? This is the only reason why I never used c# in coding competitions. I simply don't understand how to solve this problem.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
class Program
{
static void Main(string[] args)
{
int result=0,a, b;
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
result = a + b;
Console.WriteLine("The result is {0}", result);
Console.ReadKey();
}
}
}

Here an example where extract the number(s) from the string with Regex
string input = "There are 4 numbers in this string: 40, 30, and 10.";
// Split on one or more non-digit characters.
string[] numbers = Regex.Split(input, #"\D+");
foreach (string value in numbers)
{
if (!string.IsNullOrEmpty(value))
{
int i = int.Parse(value);
Console.WriteLine("Number: {0}", i);
}
}
Console.ReadKey();

Related

byte[] returns random characters for only numbers and upper case letters

Currently using an RFID scanner to pull data and send it to a CSV file. Everything is working except the data being returned is not correct in some cases. If I scan a tag's USER bank that is lower case letters only, it returns the data normally (ie scan: abc, return: abc).
However, if I scan a tag with numbers or upper case letters, it returns random symbols.
Code relevant to getting the data is below:
#Override
public void onReadResultReceived(final ReadResult readResult) {
if (readResult.getStatus() == Status.STATUS_OK) {
mHandler.post(() -> {
synchronized (mInventoryResultsLock) {
// Read the memory bank.
if (readResult.getStatus() == Status.STATUS_OK) {
byte[] bankData = readResult.getBankData();
String str = new String(bankData);
appendText(str);
}
}
});
}
}
I have tried using StandardCharsets when converting the byte[] to a string and all the different options but the same thing happens.
Is there something I am missing? I don't understand why the behavior would be different for lower case letters vs numbers or upper case. Is there something I am misunderstanding about how a byte[] works?

My Question Is input : String s=" learning java programming by self" and output is : "gninrael java gnimmargopr by fles ? plese help"

The String contains Group of words.Write a program to print even words in reverse and odd words as same in the given sentence.
i tried with split(" ") but i am not getting to reverse the even words
input is : "am learning java with stackoverflow"
output : "ma learning avaj with wolfrevokcats"
Code:
class StringJumbling {
public static void main(String args[]) {
String s="my name is baghyavathi";
String arr[]=s.split(" ");
String reverse="";
int sr=arr.length;
for(int srk=0;srk>=sr-1;srk++) {
if(sr % 2 == 0) {
reverse = reverse + " " + arr[sr];
}
}
System.out.println(reverse);
}
}
Just a guidance.
Create a function that can reverse any string. This could be approached by converting string to char array and then you've to loop in reverse order.
For selecting even and odd words, again you tokenize using split and then you can select even and odd based on index.
Now, you can target one point at a time to solve this problem.
I hope this helps. That's how you learn the hard way.
Find a way to convert string to char array and that's the only difficult part I believe for you based on your question. Good luck.

java parsing array input control

Thanks for checking out my question.
Starting off, the program has the following goal; the user inputs currency formatted as "xD xC xP xH"; the program checks the input is correct and then prints back the 'long' version: "x Dollars, x Cents, x Penny's, x half penny's"
Here I have some code that takes input from user as String currencyIn, splits the string into array tokens, then replaces the D's with Dollars etc and prints the output.
public class parseArray
{
public parseArray()
{
System.out.print('\u000c');
String CurrencyFormat = "xD xS xP xH";
System.out.println("Please enter currency in the following format: \""+CurrencyFormat+"\" where x is any integer");
System.out.println("\nPlease take care to use the correct spacing enter the exact integer plus type of coin\n\n");
Scanner input = new Scanner(System.in);
String currencyIn = input.nextLine();
currencyIn.toUpperCase();
System.out.println("This is the currency you entered: "+currencyIn);
String[] tokens = currencyIn.split(" ");
for (String t : tokens)
{
System.out.println(t);
}
String dollars = tokens[0].replaceAll("D", " Dollars ");
String cents = tokens[1].replaceAll("C", " cents");
String penny = tokens[2].replaceAll("P", " Penny's");
String hPenny = tokens[3].replaceAll("H", " Half penny's");
System.out.println(" "+dollars+ " " +cents+ " " +penny+ " " +hPenny);
input.close();
}
}
Question 1: At the moment the program prints out pretty anything you put in. how do I establish some input control? I've seen this done in textbooks with switch statement and a series of if statements, but were too complicated for me. Would it parse characters using charAt() for each element of the array?
Question 2: Is there a 'better' way to print the output? My friend said converting my 4 strings (dollars, cents, penny's, hpenny's) into elements 0, 1, 2, 3 of a new array (called newArray) and print like this:
System.out.println(Arrays.toString(newArray));
Many thanks in advance.
There is a neat solution, involving Regular Expressions, Streams and some lambdas. Core concept is that we define the input format through a regular expression. We need some sequence of digits, followed by a 'D' or a 'd', followed by a " ", followed by a sequence of digits, followed by a C or c,... I will skip derivation of this pattern, it is explained in the regular expression tutorial I linked above. We will find that
final String regex = "([0-9]+)[D|d]\\ ([0-9]+)[C|c]\\ ([0-9]+)[P|p]\\ ([0-9]+)[H|h]";
satisfies our needs. With this regular expression we can now determine whether our input String has the right format (input.matches(regex)), as well as extract the bits of information we are actually interested in (input.replaceAll(regex, "$1 $2 $3 $4"). Sadly, replaceAll yields another String, but it will contain the four digit sequences we are interested in, divided by a " ". We will use some stream-magic to transform this String into a long[] (where the first cell holds the D-value, the second holds the C-value,...). The final program looks like this:
import java.util.Arrays;
public class Test {
public static void main(String... args) {
final String input = args[0];
final String regex =
"([0-9]+)[D|d]\\ ([0-9]+)[C|c]\\ ([0-9]+)[P|p]\\ ([0-9]+)[H|h]";
if (input.matches(regex) == false) {
throw new IllegalArgumentException("Input is malformed.");
}
long[] values = Arrays.stream(input.replaceAll(regex, "$1 $2 $3 $4").split(" "))
.mapToLong(Long::parseLong)
.toArray();
System.out.println(Arrays.toString(values));
}
}
If you want to have a List<Long> instead a long[] (or a List<Integer> instead of an int[]), you would use
List<Long> values = Arrays.stream(input.replaceAll(regex, "$1 $2 $3 $4").split(" "))
.map(Long::parseLong)
.collect(Collectors.toList());
It is necessary to change mapToLong to map to receive a Stream<Long> instead of a LongStream. I am sure that one could somehow write a custom Collector for LongStream to transform it into a List<Long>, but I found this solution more readable and reliable (after all, the Collector used comes from Oracle, I trust they test their code extensively).
Here is some example call:
$> java Test "10D 9c 8p 7H"
[10, 9, 8, 7]
$> java Test "10E 9C 8P 7H"
Exception in thread "main" java.lang.IllegalArgumentException: Input is malformed.
at Test.main(Test.java:10)
$> java Test "10D 9C 8P 7H 10D 9C 8P 7H"
Exception in thread "main" java.lang.IllegalArgumentException: Input is malformed.
at Test.main(Test.java:10)
Question1
You can actually check if the input is what it's supposed to be with simple checks. For example, you can check the first element like this:
if(tokens[0].charAt(1).equals("D"))
return true;
else
return false;
Another way to check if the input is correct is by using Regular Expressions, but I assume you are a beginner and this is too much trouble for you, although it is the better way. So I leave it to you to look through it later.
Question2
You can actually listen to your friend and do as they said. You can write it as follows:
for(int i = 0; i < 4; i++)
System.out.print(" " + tokens[i])
System.out.println();
Or you may use
System.out.println(Arrays.toString(newArray));
And you have saved newArray like this:
newArray[0] = " " + tokens[0];
you could use the .equals() method to see if what a user has typed in matches what you have
if (currencyIn.equals("CurrencyFormat"))
{
...
}
this is probably the simplest way i can think of!

Seemingly Random Characters at End of Output When Using Scanner

The code below is intended to encrypt and decrypt messages input. When I encrypt and decrypt data, it sometimes works and at other times doesn't. The following is an example of the problems I am experiencing.
Encryption
Decryption
As you can see, when I try to decrypt, my program terminated and some gibberish is output into the console. What is the issue with my code?
I'm unsure if mentioning this helps, but I have Eclipse's file encoding set to UTF-8.
Please excuse any poor code. I'm still very much a beginner with Java and I'm puzzled as to why this is happening.
import java.util.Scanner;
public class Transcrypt {
static String mode = "",
msg,
key;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (!mode.equals("e") && !mode.equals("d")) { // Ask for mode until equal to "e" or "d"
System.out.print("Encrypt or decrypt? (e/d) ");
mode = input.nextLine().toLowerCase();
}
System.out.print("Message: "); // Ask for message
msg = input.nextLine();
System.out.print("Passkey: "); // Ask for key
key = input.nextLine();
input.close();
System.out.println(transcrypt(msg, key, mode.equals("d"))); // Transcrypt and output
}
public static String transcrypt(String msg, String key, boolean decode) {
String result = "";
for (int i=0; i<msg.length(); i++) {
// Add or subtract Unicode index of key.charAt(i % key.length()) and/from msg.msg.charAt(i) and convert back to character
result += (char) ((int) msg.charAt(i) + ((int) key.charAt(i % key.length())) * (decode ? -1 : 1));
}
return result;
}
}
Your encoded message looks like "¼ÊßàãÊãæÑ×", but actually, it is "¼Êßàã\u0085ÊãæÑ×\u0095".
Most notably, it contains the control character 0x85 in-between which has “new line” semantics. So when copying that string, you’re copying the control characters with it and when pasting into the console upon your application’s query for the message, you are basically entering ¼Êßàã as message, the input committed via the “new line” control character, causing the subsequent query for the password to consume the trailing ÊãæÑ× characters.
The garbage you see right after the Passkey: output, is the result of your attempt to decode ¼Êßàã using the key ÊãæÑ×, as there was no newline entered at this point as the characters being already in the console’s buffer have been used.
Generally, as already said by Nándor Előd Fekete in this comment, you should not write characters to the console, that are actually binary data, like the encoded string.
By the way, you shouldn’t declare variables as static fields that are actually local to a method, i.e. your main method. Further, you don’t need to cast char to int when doing computations. char values are already a subset of the int values.

String.split() Not Acting on Semicolon or Space Delimiters

This may be a simple question, but I have been Googling for over an hour and haven't found an answer yet.
I'm trying to simply use the String.split() method with a small Android application to split an input string. The input string will be something along the lines of: "Launch ip:192.168.1.101;port:5900". I'm doing this in two iterations to ensure that all of the required parameters are there. I'm first trying to do a split on spaces and semicolons to get the individual tokens sorted out. Next, I'm trying to split on colons in order to strip off the identification tags of each piece of information.
So, for example, I would expect the first round of split to give me the following data from the above example string:
(1) Launch
(2) ip:192.168.1.101
(3) port:5900
Then the second round would give me the following:
(1) 192.168.1.101
(2) 5900
However, the following code that I wrote doesn't give me what's expected:
private String[] splitString(String inputString)
{
String[] parsedString;
String[] orderedString = new String[SOSLauncherConstants.SOCKET_INPUT_STRING_PARSE_VALUE];
parsedString = inputString.trim().split("; ");
Log.i("info", "The parsed data is as follows for the initially parsed string of size " + parsedString.length + ": ");
for (int i = 0; i < parsedString.length; ++i)
{
Log.i("info", parsedString[i]);
}
for (int i = 0; i < parsedString.length; ++i )
{
if (parsedString[i].toLowerCase().contains(SOSLauncherConstants.PARSED_LAUNCH_COMMAND_VALUE))
{
orderedString[SOSLauncherConstants.PARSED_COMMAND_WORD] = parsedString[i];
}
if (parsedString[i].toLowerCase().contains("ip"))
{
orderedString[SOSLauncherConstants.PARSED_IP_VALUE] = parsedString[i].split(":")[1];
}
else if (parsedString[i].toLowerCase().contains("port"))
{
orderedString[SOSLauncherConstants.PARSED_PORT_VALUE] = parsedString[i].split(":")[1];
}
else if (parsedString[i].toLowerCase().contains("username"))
{
orderedString[SOSLauncherConstants.PARSED_USERNAME_VALUE] = parsedString[i].split(":")[1];
}
else if (parsedString[i].toLowerCase().contains("password"))
{
orderedString[SOSLauncherConstants.PARSED_PASSWORD_VALUE] = parsedString[i].split(":")[1];
}
else if (parsedString[i].toLowerCase().contains("color"))
{
orderedString[SOSLauncherConstants.PARSED_COLOR_VALUE] = parsedString[i].split(":")[1];
}
}
Log.i("info", "The parsed data is as follows for the second parsed string of size " + orderedString.length + ": ");
for (int i = 0; i < orderedString.length; ++i)
{
Log.i("info", orderedString[i]);
}
return orderedString;
}
For a result, I'm getting the following:
The parsed data is as follows for the parsed string of size 1:
launch ip:192.168.1.106;port:5900
The parsed data is as follows for the second parsed string of size 6:
launch ip:192.168.1.106;port:5900
192.168.1.106;port
And then, of course, it crashes because the for loop runs into a null string.
Side Note:
The following snippet is from the constants class that defines all of the string indexes --
public static final int SOCKET_INPUT_STRING_PARSE_VALUE = 6;
public static final int PARSED_COMMAND_WORD = 0;
public static final String PARSED_LAUNCH_COMMAND_VALUE = "launch";
public static final int PARSED_IP_VALUE = 1;
public static final int PARSED_PORT_VALUE = 2;
public static final int PARSED_USERNAME_VALUE = 3;
public static final int PARSED_PASSWORD_VALUE = 4;
public static final int PARSED_COLOR_VALUE = 5;
I looked into needing a possible escape (by inserting a \\ before the semicolon) on the semicolon delimiter, and even tried using it, but that didn't work. The odd part is that neither the space nor the semicolon function as a delimiter, yet the colon works on the second time around. Does anybody have any ideas what would cause this?
Thanks for your time!
EDIT: I should also add that I'm receiving the string over a WiFi socket connection. I don't think this should make a difference, but I'd like you to have all of the information that you need.
String.split(String) takes a regex. Use "[; ]". eg:
"foo;bar baz".split("[; ]")
will return an array containing "foo", "bar" and "baz".
If you need groups of spaces to work as a single delimiter, you can use something like:
"foo;bar baz".split("(;| +)")
I believe String.split() tries to split on each of the characters you specify together (or on a regex), not each character individually. That is, split(";.") would not split "a;b.c" at all, but would split "a;.b".
You may have better luck with Guava's Splitter, which is meant to be slightly less unpredictable than java.lang.String.split.
I would write something like
Iterable<String> splits = Splitter.on(CharMatcher.anyOf("; ")).split(string);
but Splitter also provides fluent-style customization like "trim results" or "skip over empty strings."
Is there a reason why you are using String.split(), but not using Regular Expressions? This is a perfect candidate for regex'es, esp if the string format is consistent.
I'm not sure if your format is fixed, and if it is, then the following regex should break it down for you (am sure that someone can come up with an even more elegant regex). If you have several command strings that follow, then you can use a more flexible regex and loop over all the groups:
Pattern p = Pattern.compile("([\w]*)[ ;](([\w]*):([^ ;]*))*");
Matcher m = p.match( <input string>);
if( m.find() )
command = m.group(1);
do{
id = m.group(3);
value = m.group(4);
} while( m.find() );
A great place to test out regex'es online is http://www.regexplanet.com/simple/index.html. It allows you to play with the regex without having to compile and launch you app every time if you just want to get the regex correct.

Categories