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 2 years ago.
Improve this question
If there is a given
Inputs
String "abcdaa/efgh/hidjk/lmno/pqrs/tuvw" and if
int slashCounter=3,
The desired Output should be -
Output: abcdaa/efgh/hidjk/lmno
(Basically if slashCounter=3 only alplabets upto 4th '/' is allowed. From fourth '/'everything is ignored. Below is the few Input and Output. (There may be any number of alphabets between '/' to '/'). Below is few more inputs
Input:
String aaabcd/efgh/hidjk/lmno/pqrs/tuvw
if int slashCounter=2
Output: aaabcd/efgh/hidjk
Input:
String aaabcd/efgh/hidjk/lmno/pqrs/tuvw
if int slashCounter=4
Output: aaabcd/efgh/hidjk/lmno/pqrs
Could someone help me with the logic of this in JAVA. Thanks in advance.
This is how you do.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
// till how much we want the string
int display = sc.nextInt();
// splits the String when "/" is encounter and then stores it in 0 index and then increases the index
String aplhabets[] = input.split("/");
// to add the String we want
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i <= display; i++) {
sb.append(aplhabets[i]+"/");
}
// as we dont want the last "/" of the String we just print from 0 to string length - 1, this will remove teh last "/"
System.out.print(sb.substring(0, sb.length()-1));
}
Output:
aaabcd/efgh/hidjk/lmno/pqrs/tuvw
2
aaabcd/efgh/hidjk
String aplhabets[] = input.split("/") this splits the String and puts it in the array whenever / is encounter.
sb.substring(0, sb.length()-1) this cause when we were appending String builder from he loop it's adding / in the end.
So in order to remove last / we do sb.substring(0,sb.length-1) where first parameter is start index and second index is end index
in substring(int start, int end) start in inclusive and end is exclusive.
this is how you do the problem. Suggest you learn how to use arrays and String manipulation. this will surely benefit you.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I need to print an array's element with it's index next to it. If the array element is even, it must display the element and its correlating index number, if the element is odd, it must display the element and "1".
For example:
Input: int[] array = new int [25]
Output: 0:0, 1:1, 2:2, 3:1, 4:4, 5:1, 6:6, 7:1 and so on
How would I do this?
You could do something like, You can check if a number is even then print the number and index separated by a colon else print number and 1 separated by a colon, This can be achieved by using for loop construct.Would recommend you to follow proper guide/tutorial as you look like a beginner in java.
public static void main(String[] args) {
int[] array = {1,2,3,4,5};
int counter = 0;
for(int i : array) {
if(i % 2 == 0)
System.out.print(i+":"+counter);
else
System.out.print(i+":1");
if(counter<array.length-1)
System.out.print(", ");
counter++;
}
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 6 years ago.
Improve this question
I was required to make a code that accepted a binary number (1's and 0's) and then counted how many ones were in that binary number. My code fulfills this purpose.
The second part of the exercise is this: if the user enters a number that is NOT binary, I must output that there is an error and keep prompting the user until they give a binary number.
Can someone show me how to incorporate this? I have tried several times but cannot make it click. Thanks! Here is my code.
import java.util.Scanner;
public class NewClass
{
public static void main( String [] args )
{
Scanner scan = new Scanner( System.in);
int i = 0, count = 0;
String number;
System.out.println("Please enter a binary number.");
number = scan.next();
String number1 = "1";
while ((i = number.indexOf(number1, i++)) != -1) {
count++;
i += number1.length();
}
System.out.println("There are "+ count + " ones in the binary number.");
}
}
You already know how to find all of the 1's; you should be able to do something similar to find all of the 0's.
If the sum of those two counts is not the length of the string, there must be at least one illegal character in it.
If you use the parseInt method you can do
Integer.parseInt("1100110", 2) returns 102
or in your case
int intVal = Integer.parseInt(number, 2);
as per the javadocs
Throws:
NumberFormatException - if the String does not contain a parsable int.
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 7 years ago.
Improve this question
How to write a java program to get a string as a key board input. Then count the number of letters, digits and spaces available in that string and display the count of letters, digits and spaces.
This is honestly... really, really easy and something that really should be taught to you in an introductory course or something.
Scanner scan = new Scanner(System.in);
String userInput = scan.nextLine();
You will need to put, right after package, import java.util.Scanner. To do the other thing, i.e. counting the number of characters in the String, simply invoke length().
Pass a string as an argument in this method you get count of letter, space, and digit
public void count(String str) {
int space = 0;
int digit = 0;
int letter =0;
for (int f4 = 0; f4 < str.length(); f4++) {
if (Character.isSpace(str.charAt(f4)))
space++;
if(Character.isAlphabetic(str.charAt(f4)))
letter++;
if(Character.isDigit(str.charAt(f4)))
digit++;
}
System.out.println("space = "+space+", digit = "+digit+", letter = "+letter);
}
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
So I am trying to prompt the user to enter any words into a string. Then I want to prompt them to count the number of occurrences for whatever letter they want to count. So if they enter words in a string like "this is a test" and they search "t" for example, the return would be 3 t's in the string "this is a test". I am a little confused as to where to go from here...
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String inputValue;
String s = "";
System.out.print("Enter a string of words or type done to exit: ");
inputValue = input.readLine();
System.out.print("Which letter would you like to count: ");
s = input.readLine();
int counter = 0;
I am thinking about maybe doing a for loop and do something like counter++.
Answer provided by Jean above is correct but I would like use a different method to calculate number of occurrence of a character in a String.
String string = "this is a testing string";
int count = string.length() - string.replaceAll("t", "").length();
Or
int counter = string.split("t").length - 1;
You would need to escape meta characters if you are to check character like $.
Using Apache commons-lang you could simply do
int counter = StringUtils.countMatches(s, intputValue);
But if you really want to code it, then you could use
public int count(String fullString, char valueToCount)
{
int count = 0;
for (int i=0; i < fullString.length(); i++)
{
if (fullString.charAt(i) == valueToCount)
count++;
}
return count;
}
Another solution would consist of replacing everything in your string except the input char and return the length of the trimmed string.
return s.replaceAll("[^" + inputValue + "]", "").length();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm kind of new to this stuff, but what i want to do is just print out 00s from an int array that i created. I used a stringBuilder() to get rid of the commas and stuff. Now when I print out the numbers, they must have a space after every third 0 (a total of 11 0s). How do I do that? I only get a space after every 0 :-(.
here is what I got so far.
public class AccountNumber {
private int[] digits = new int [11];
// Methods Returns a string representation
public String toString() {
StringBuilder builder = new StringBuilder();
for (int value :digits) {
builder.append(value + " ");
}
String text = builder.toString();
return text;
//return Arrays.toString(digits);
}
public AccountNumber ( boolean random ){
}
}
The output I want is 000 000 000 00
I have another (main) class which creates the object for me. That's where the printing should happen.
public class Test1 {
public static void main (String [] args) {
//Random rand = new Random(false);
AccountNumber acc = new AccountNumber(false);
System.out.println(acc.toString());
//AccountNumber.AccountNumber();
}
}
Thank you
You are trying to pretty print 11-digit account number on your Account class. You want to insert space after each 3 digits. If my assumptions are correct, you just need a counter to see which digit you're on and test if that digit can be divided by three:
int index= 0;
for (int value :digits) {
builder.append(value);
if (index %3 == 0) {
builder.append(" ");
}
++index;
}
This could be written in more clear way by using classic for loop, but I don't know which type your digits field is.