Counting the length of the textbox - netbeans-8

I want to try that if they input only two characters in the first name, it will also display, because only three characters and up is readable. thanks
String first = "";
String lnm = "";
String num = "";
String stud = "";
int num1;
int num2;
int id;
String fname = request.getParameter("name");
String lname = request.getParameter("last");
String Sid = request.getParameter("num");
first = fname.substring(fname.length()-3);
lnm = lname.substring(0, 3);
num1 = Integer.parseInt(Sid.substring(0, 2));
num2 = Integer.parseInt(Sid.substring(Sid.length()-6));
id = num2/num1;
stud = Integer.toString(id);
out.println(first + lnm + stud.substring(0, 4));

I found the following link which has a solution to a similar problem and I think it will solve your problem as well: How to get the last characters in a String in Java, regardless of String size
Jon Skeet responded to that post with 3 different possible solutions. His second solution should solve your problem as well:
String numbers = text.substring(Math.max(0, text.length() - 7));
In your case this would be:
first=fname.substring(Math.max(0, fname.length()-3));
I hope that helps.

Related

Any way to convert from Double to Hex in vb?

I want to add a text box where a user can input a number. No matter how large or small the number, it needs to be stored as a double. Then with a click button, the hex equivalent will be displayed in a second text box. Bonus points if you can show me how to take the 16 byte hex and change it to 4 variables with 4 bytes each.
For example, user enters 1234.56 in textbox1. Clicks button 1, and textbox2 displays the hex equivilent "40934A3D70A3D70A" Then take that string, and extract to 4 different 4-byte strings so str1=1093, str2=4A3D, str3=70a3, str4=d70A.
Are you looking for BitConverter? C# implementation:
double source = 1234.56;
// 40934A3D70A3D70A
string result = string.Concat(BitConverter
.GetBytes(source)
.Reverse()
.Select(b => b.ToString("X2")));
Having got result, extract its parts with Substring:
string str1 = result.Substring(0, 4);
string str2 = result.Substring(4, 4);
string str3 = result.Substring(8, 4);
string str4 = result.Substring(12, 4);
If you are looking for a C# implementation, try this:
static void Main(string[] args)
{
var number = 1234.56;
string hex = DoubleToHex(number);
string part1 = hex.Substring(0, 4);
string part2 = hex.Substring(4, 4);
string part3 = hex.Substring(8, 4);
string part4 = hex.Substring(12, 4);
}
internal static string DoubleToHex(double value)
{
var b = BitConverter.GetBytes(value).Reverse();
string result = string.Join(string.Empty, b.Select(i => i.ToString("X2")).ToArray());
return result;
}
This is the most efficient answer you're going to get:
unsafe static void Main()
{
var value = 1234.56;
var pLong = (long*)&value;
var fullString = (*pLong).ToString("X");
var pShort = (short*)pLong;
short value0 = *pShort, value1 = *(pShort + 1), value2 = *(pShort + 2),
value3 = *(pShort + 3);
string s0 = value0.ToString("X"), s1 = value1.ToString("X"), s2 = value2.ToString("X"),
s3 = value3.ToString("X");
Debug.Print(fullString);
Debug.Print(s0);
Debug.Print(s1);
Debug.Print(s2);
Debug.Print(s3);
}
Output:
40934A3D70A3D70A
D70A
70A3
4A3D
4093
Translation of Dmitry Bychenko answer to VB.NET:
Imports SplitValue = System.Tuple(Of String, String, String, String)
Module Module1
Function DoubleToByteArray(ByVal AValue As Double) As Byte()
Return BitConverter.GetBytes(AValue).Reverse().ToArray()
End Function
Function SplitByteArray(ByRef AValue As Byte()) As SplitValue
Dim StringValue As String = String.Join("", From AByte In AValue Select AByte.ToString("X2"))
Return New SplitValue(StringValue.Substring(0, 4), StringValue.Substring(4, 4), StringValue.Substring(8, 4), StringValue.Substring(12, 4))
End Function
Sub Main()
Dim Result As SplitValue
Result = SplitByteArray(DoubleToByteArray(1234.56))
Console.WriteLine(Result)
Console.ReadLine()
End Sub
End Module
Output:
(4093, 4A3D, 70A3, D70A)
try it: DoubleToHex
//dashseparator 0 /2/4
public string DoubleToHex(double d, bool reverse = false, int dashSeparator = 0)
{
byte[] bytes = BitConverter.GetBytes(d);
if (reverse) bytes = bytes.Reverse().ToArray();
var hex = BitConverter.ToString(bytes);
var hex4 = "";
if (dashSeparator == 2) return hex;
if (dashSeparator == 4)
{
hex = hex.Replace("-", "");
hex = Regex.Replace(hex, ".{4}", "$0-").TrimEnd('-');
return hex;
}
return hex.Replace("-", "");
}
sample Output:
Double: 1234.56
Hex: 0AD7A3703D4A9340
Hex in Reverse order: 40934A3D70A3D70A
Hex in Reverse order separate by 2 digits: 40-93-4A-3D-70-A3-D7-0A
Hex in Reverse order separate by 4 digits: 4093-4A3D-70A3-D70A
you can :
-control the generated Hex to be displayed in order/ reverse order.
-Add dash separator by 0 (no separator) / 2 /4 digits
Edit:
Vb.Net Version
Converting Code to Vb.Net
'dashseparator 0 /2/4
Public Function DoubleToHex(d As Double, Optional reverse As Boolean = False, Optional dashseparator As Integer = 0) As String
Dim bytes As Byte() = BitConverter.GetBytes(d)
If reverse Then
Array.Reverse(bytes)
End If
Dim hex = BitConverter.ToString(bytes)
Dim hex4 = ""
If dashseparator = 2 Then
Return hex
End If
If dashseparator = 4 Then
hex = hex.Replace("-", "")
hex = Regex.Replace(hex, ".{4}", "$0-").TrimEnd("-"C)
Return hex
End If
Return hex.Replace("-", "")
End Function

vb.net convert number to alphanumeric equivalent

I need to convert a four digit number to a three digit alphanumeric number where
001-999 = 001-999
1000 = 00A
1027 = 01A
etc.. up to whatever = ZZZ
I am very new to programming and cannot figure out how to proceed with this issue. Any help would be GREATLY appreciated.
How does this work for you?
Private Function ConvertMyNumber(toConvert As UShort) As String
Const MaxValueToConvert = 3885S
If (toConvert > MaxValueToConvert) Then Throw New ArgumentException(String.Format("Argument value of {0} exceeds maximum possible value of {1}.", toConvert, MaxValueToConvert))
Const NumeringSchemeThreshold = 1000
Const TotalResultLength = 3
Const PaddingChar = "0"c
If (toConvert < NumeringSchemeThreshold) Then Return toConvert.ToString().PadLeft(TotalResultLength, PaddingChar)
Dim adjustedValue = (toConvert - NumeringSchemeThreshold) 'since we're starting with this numbering scheme at NumeringSchemeThreshold
Const NumbersInAlphabet = 26
Dim moddedValue = (adjustedValue Mod NumbersInAlphabet) '0 to NumbersInAlphabet - 1 based on the cycle
Dim numCycles = Fix(adjustedValue / NumbersInAlphabet) 'What "cycle number" it is, i.e. the number of times around the alphabet loop.
Dim suffix As String = String.Empty
Dim prefix As String = CStr(numCycles)
Const firstStepThreshold = 100
Const secondStepThreshold = 110
Const LastAlphabetChar = "Z"c
If (numCycles >= firstStepThreshold And numCycles < secondStepThreshold) Then
numCycles = numCycles - firstStepThreshold
prefix = CStr(numCycles)
suffix = LastAlphabetChar
ElseIf (numCycles >= secondStepThreshold) Then
numCycles = numCycles - secondStepThreshold
suffix = LastAlphabetChar & LastAlphabetChar
prefix = String.Empty
End If
Const AsciiCharValueOffset = 65
'concat the cycle number to the converted modded letter, and return the zero-padded result.
Return (prefix & CChar(Char.ConvertFromUtf32(moddedValue + AsciiCharValueOffset)) & suffix).PadLeft(TotalResultLength, PaddingChar)
End Function

how to check what the first character of a string is in vb

I have the following code, which reads the date and time from some DateTimePickers in VB.
I need to be able to determine if the first value is a 0 or a 1, (eg 09:12... or 12:13...) and if it starts with a 0 to remove that character from the string.
this is what i have so far, but it takes the first character regardless.
DateFrom = Form1.DateTimePickerFrom.Value.ToString
DateTo = Form1.DateTimePickerTo.Value.ToString
VarTimeFrom = Form1.HourTimePickerFrom.Value.ToString
VarTimeTo = Form1.HourTimePickerTo.Value.ToString
Dim DateFromManipulated = Left(DateFrom, 10)
Dim DateToManipulated = Left(DateTo, 10)
Dim TimeFromManipulated = Right(VarTimeFrom, 9)
Dim TimeToManipulated = Right(VarTimeTo, 9)
If Left(DateFromManipulated, 1) = 0 Then
TimeFromMan = TimeFromManipulated.Remove(0, 1)
Else
TimeFromMan = TimeFromManipulated
End If
If Left(TimeFromManipulated, 1) = 0 Then
TimeToMan = TimeToManipulated.Remove(0, 1)
Else
TimeToMan = TimeToManipulated
End If
Console.WriteLine(DateFromManipulated)
Console.WriteLine(TimeToMan)
Console.WriteLine(TimeFromManipulated)
Console.WriteLine(TimeFromMan)
Console.WriteLine(DateToManipulated)
Console.WriteLine(TimeToManipulated)
I get the following:
09/11/2012
1:36:00
06:36:00
6:36:00
08/01/2013
11:36:00
Thanks in advance!
Mike
A string in VB.NET won't compare as equal to an integer. You could just reference character zero, though:
If DateFromManipulated(0) = "0"c Then DateFromManipulated = DateFromManipulated.Substring(1)
... however, you should be just formatting your date the way you want it to begin with:
Dim dateFrom As String = DateTimePickerFrom.Value.ToString("M/dd/yyyy H:mm:ss")
... for example. (M doesn't have a leading zero, as opposed to MM; same with H.) You can find all the format strings here: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Splitting a String into Pairs

How would I go on splitting a string into pairs of letter in VB?
for example: abcdefgh
split into: ab cd ef gh
I'll throw my hat in the ring:
Dim test As String = "abcdefgh"
Dim results As New List(Of String)
For i As Integer = 0 To test.Length - 1 Step 2
If i + 1 < test.Length Then
results.Add(test.Substring(i, 2))
Else
results.Add(test.Substring(i))
End If
Next
MessageBox.Show(String.Join(" ", results.ToArray))
The following allows for odd length strings. If the string is zero-length, I'm not sure what you'd want to do, you'll want to address that case.
Dim src As String = "abcdef"
Dim size As Integer
If src.Length > 0 Then
If src.Length Mod 2 = 0 Then
size = (src.Length / 2) - 1
Else
size = ((src.Length + 1) / 2) - 1
End If
Dim result(size) As String
For i = 0 To src.Length - 1 Step 2
If i = src.Length - 1 Then
result(i / 2) = src.Substring(i, 1)
Else
result(i / 2) = src.Substring(i, 2)
End If
Next
End If
In C# you would do like this:
Dictionary<String, String> Split(String input)
{
if (input.Count % 2 == 0)
{
Dictionary<string, string> Pairs = new Dictionary( );
for (int L = 0, R = 1; L < input.Count && R <= input.Count; ++L, ++R)
{
Char
Left = input[L],
Right = input[R];
Pairs.Add(
Left.ToString(),
Right.ToString());
}
}
else
{
throw new NotEvenException( );
}
return Pairs( );
}
void Main()
{
var Pairs = Split("ABCDEFGH");
foreach(string Key in Split("ABCDEFGH"))
{
Console.Write("{0}{1}\n", Key, Pairs[Key]);
}
}
/*
Output:
AB
CD
EF
GH
*/
Now, I know what you think: This isn't what I want! But I say: It is actually, at least partly.
Since I presume you're working in VB.net, the basic structure of what you want performed is outlined in the short snippet above.
For example: The method Count (of the object String) exists in both C# and in VB.
Hope it helps a bit at least!

SQL: Update does not affect any row

I want to update a dataset in a DB2/AS400 table.
The problem is if I there is string parameter in the parameters list the command does not find a row to update.
For example: If I run the command only with the company number the command will succeed. If I run the command with the company number and facility number the command fails.
Does anyone have any idea?
IDbConnection cn = Tools.GetCnApp();
try
{
StringBuilder sql = new StringBuilder();
sql.AppendLine("UPDATE " + Tools.GetSchemeApp() + "/ChangeReasonAssignments");
sql.AppendLine(" SET Confirmed = #CONF, Confirmed_By = #CONFBY, Confirmed_At = #CONFAT");
sql.AppendLine(" WHERE Company = #CONO AND Facility = #FACI AND Department = #DEPT");
sql.AppendLine(" AND Production_Group = #PRGR AND Manufacturing_Order = #ORDR AND Order_Operation = #OPER");
sql.AppendLine(" AND Confirmed = 0");
IDbCommand cmd = cn.CreateCommand();
cmd.SetParameter("#CONO", this.CompanyNumber);
cmd.SetParameter("#FACI", this.FacilityNumber);
cmd.SetParameter("#DEPT", this.ProductionGroup.Department.Name);
cmd.SetParameter("#PRGR", this.ProductionGroup.Name);
cmd.SetParameter("#ORDR", this.ManufacturingNumber);
cmd.SetParameter("#OPER", this.OperationNumber);
cmd.SetParameter("#CONFBY", Base.User);
cmd.SetParameter("#CONFAT", DateTime.Now.ToString());
cmd.SetParameter("#CONF", 1);
cmd.CommandText = sql.ToString();
if (cmd.ExecuteNonQuery() > 0)
{
}
EDIT
The datatypes in database are:
Company: INTEGER
Facility: VARCHAR
Dpartment: VARCHAR
Production_Group: VARCHAR
Manufacturing_Order:INTEGER
Order_Operation: INTEGER
The datatypes in .NET are:
CompanyNumber: int
FacilityNumber: String
Departmentname: String
ProductionGroup: String
Manufacturingorder: int
OrderOperation: int
sql.ToString() results:
UPDATE TSAEDBDEV/ChangeReasonAssignments SET Confirmed = #CONF, Confirmed_By = #CONFBY, Confirmed_At = #CONFAT WHERE Company = #CONO AND Facility = #FACI AND Confirmed = 0
Try to set the string values into ': cmd.SetParameter("#DEPT", "'" + this.ProductionGroup.Department.Name + "'");