even number scanner code [closed] - java.util.scanner

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
i am creating a scanner test. I have managed to print even number 1-50. i want to do a scanner code so that when a user inputs i.e 8 it will print all the even number from 8-50.
import java.util.Scanner;
public class ModulusCalculation
{
public static void main(String[] args)
{
int endLimit = 50;
System.out.println("WE ARE GOING TO PRINT EVEN NUMBER FROM 1 AND " + endLimit);
Scanner input = new Scanner(System.in);
for (int startingPoint = 1; startingPoint <= endLimit; startingPoint++)
{
if (startingPoint % 2 == 0)
{
input.nextLine();
System.out.println(+startingPoint);
}
}
}
}

Dont need to read input.nextLine(); within the for loop.
STEP 1: read a starting integer value
STEP 2: for (int startingPoint = startinteger; startingPoint <= endLimit; startingPoint++)
STEP3: print all the even numbers based on the condition within the for loop

Related

Determine if the integer is one digit and add a zero before it [duplicate]

This question already has answers here:
Leading zeros for Int in Swift
(12 answers)
Closed 7 years ago.
I need to check if the int is only one digit and if it is, I want to add a zero in front of it. I have this code but it doesnt work.
var minutes2 = Int(minutes)
var minutessize: Int = sizeofValue(minutes2)
if minutessize < 2 {
var needStringHere = "0\(minutes2)"
let a: Int? = needStringHere.toInt()
minutes2 = a!
}
You can just check if the minutes count is less than 10:
var str = "\(minutes2)"
if (minutes2 < 10) { str = "0\(minutes2)" }

Generate auto increment number in a textbox using C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to add sequence numbers in textbox but the format is 001 to onward, the main problem is when the number comes to 009, after it became 0010, four digit character. I want to reduce a zero from it, the number should look like 010. Please help me for this problem
This should meet your needs.
for (int i = 0; i <= 100; ++i)
{
Console.WriteLine(string.Format("{0:000}", i));
}
Quick and dirty. Probably not the best way to do this:
textBox1.Text = (Convert.ToInt16(textBox1.Text) + 1).ToString();
if (textBox1.Text.Length == 1)
{
textBox1.Text = "00" + textBox1.Text;
}
else if (textBox1.Text.Length == 2)
{
textBox1.Text = "0" + textBox1.Text;
}

(String Permutation) from interviewstreet.com [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
*Question (String Permutation) from interviewstreet.com*
Given two strings, write a method to decide if one is a permutation
of
the other. Your solution should consider case sensitivity and
whitespace as significant.
A permutation of a set of objects is an arrangement of those objects
into a particular order. For example, there are six permutations of
the string "abc", namely "abc", "acb", "bac", "bca", "cab", and
"cba".
Output:
Return 1 if the two strings are permutations of each other.
Return 0 if the two strings are not permutations of each other.
abc acb cab cba bac bca
import java.io.*;
import java.util.*;
public class Solution {
private Set permutations;
public static void main(String args[] ) throws Exception {
// Scanner sc = new Scanner(System.in);
//String string1 = sc.nextLine();
//String string2 = sc.nextLine();
String string1 = "str";
String string2 = "str";
Solution solution = new Solution();
int output = solution.permutation(string1, string2);
System.out.println(output);
}
public void stringPermuation(String s1, String s2) {
if (s2.length() > 0) {
for (int i = 0; i < s2.length(); i++) {
System.out.println(s1 + s2.charAt(i)+","+ s2.substring(0, i)+" +"+ s2.substring(i + 1));
stringPermuation(s1 + s2.charAt(i),
s2.substring(0, i) + s2.substring(i + 1));
}}
else{
permutations.add(s1);
System.out.println(s1);
}
}
public Set stringPermuation(String s) {
permutations = new HashSet<String>();
stringPermuation("", s);
return permutations;
}
private int permutation(String string1, String string2) {
int result = 0;
Set<String> setString1 = stringPermuation(string1);
Set<String> setString2 = stringPermuation(string2);
// create an iterator
System.out.println("There are total of " + setString1.size() + " permutations in String1:");
System.out.println("There are total of " + setString2.size() + " permutations in String2:");
if(setString1.size() == setString2.size())
result=IterateSet(setString1,setString2);
//Return 1 if string1 is a permutation of string2
//Return 0 if string1 is not a permutation of string2
return result;
}
public int IterateSet(Set setString1,Set setString2){
int i= 0;
Iterator<String> it = setString1.iterator();
while (it.hasNext()) {
if(setString2.contains(it.next()) && i == 0)
i=1;
}
return i;}}
...Sort the characters of the two strings (lexicographically) and if the two sorted strings are equal, the originals are permutations of each other.

how to efficiently check if the Levenshtein edit distance between two string is 1 [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
please note that it doesn't require to really calculate Levenshtein edit distance. just check it's 1 or not.
The signature of the method may look like this:
bool Is1EditDistance(string s1, string s2).
for example:
1. "abc" and "ab" return true
2. "abc" and "aebc" return true
3. "abc" and "a" return false.
I've tried recursive approve, but it it not efficient.
update: got answer from a friend:
for (int i = 0; i < s1.Length && i < s2.Length; i++)
{
if (s1[i] != s2[i])
{
return s1.Substring(i + 1) == s2.Substring(i + 1) //case of change
|| s1.Substring(i + 1) == s2.Substring(i) //case of s1 has extra
|| s1.Substring(i) == s2.Substring(i + 1); //case of s2 has extra
}
}
return Math.Abs(s1.Length - s2.Length) == 1;
If you only care if the distance is exactly 1 or not, you can do something like this:
If the difference of the strings' lengths is not 0 or 1, return false.
If both strings have length n, loop i = 0..n checking s1[i] == s2[i] for all i except one.
If the strings have length n and n+1, let i be the smallest index where s1[i] != s2[i], then loop j=i..n checking s1[j] == s2[j+1] for all j.

function trying to put dot after n characters [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am trying to write a function, it has 2 parameters one is of string and another is of number datatype, my function has to place a dot after every N characters, where N is provided at run time (some number provided through number datatype). Can anybody help me out please?
This smells like homework, so let me suggest how to start, and then you can come back with how far you've gotten.
First, you need to be able to iterate over the string, or at least to jump forward N characters along its length. Can you think of a construct that allows you to either iterate each character until you've iterated N character, or that allows you to split the string into substrings N characters long?
What language?
In C#:
public string PutDots(string input, int n)
{
char[] c = input.ToCharArray();
StringBuilder output = new StringBuilder();
for (int i = 0; i < c.Length; i++)
{
output.Append(c[i]);
if (i % n == 0 && i > 0)
{
output.Append(".");
}
}
return output.ToString();
}
something like this maybe:
public string foo(string input, int count)
{
string result = "";
for(int i=0; i < input.length; i++)
{
result += input[i];
if(i % count == 0)
result += '.';
}
return result;
}
(Depending on the language you might want to use something else then string concatenation to build the resultingstring)
In C#:
static string InsertDots(string s, int n)
{
if(string.IsNullOrEmpty(s)) return s;
if(n <= 0 || n > s.Length) return s;
Regex re = new Regex(string.Format("(.{{{0}}})", n));
return re.Replace(s, "$1.");
}

Resources