Java StringBuffer cannot print out apostrophe - java

I would write a snippet that concats two string(with specific elements) to one. May someone could help me?
My code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String first = sc.nextLine();
String second = sc.nextLine();
StringBuilder builder = new StringBuilder(" ");
for(int i = 0; i < first.length(); i++){
builder.append(first.charAt(i));
builder.append(second.charAt(i));
}
String base = builder.toString();
System.out.println(base);
}
}

Tom,
The problem is that one string is longer than the other one.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String elso = sc.nextLine();
String masodik = sc.nextLine();
String longestString = elso;
String shortestString = masodik;
if (shortestString.length() > longestString.length()){
shortestString = elso;
longestString = masodik;
}
StringBuilder builderem = new StringBuilder(" ");
for(int i = 0; i < shortestString.length(); i++){
builderem.append(shortestString.charAt(i));
builderem.append(longestString.charAt(i));
}
for(int i = shortestString.length(); i < longestString.length(); i++){
builderem.append(longestString.charAt(i));
}
String alapba = builderem.toString();
System.out.println(alapba);
}
}
Please let me know if it is working!

Related

Print First N characters in reverse

import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int N = sc.nextInt();
StringBuilder sb = new StringBuilder(str);
System.out.print(sb.reverse().toString());
}
}
the output should be:-
apple
3
ppa
but I'm getting full string in reverse
elppa
like this
You aren't splitting the string at all in your code. What you need is a .substring(). Since you can't reverse a word after performing the .substring() on a StringBuilder, we'll just create our own reverse method. Try this:
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System. in );
String str = sc.nextLine();
int n = Integer.parseInt(sc.nextLine());
System.out.print(reverse(str.substring(0, n)));
sc.close();
}
public static String reverse(String text) {
return new StringBuilder(text).reverse().toString();
}
}
Using strictly StringBuilder methods (as you requested), you can do:
import java.util.Scanner;
public class SplitReverse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int n = Integer.parseInt(sc.nextLine());
StringBuilder sb = new StringBuilder(str);
sb=new StringBuilder(sb.substring(0,n));
System.out.println(sb.reverse());
sc.close();
}
}
However, I would recommend this one:
import java.util.Scanner;
public class SplitReverse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int n = Integer.parseInt(sc.nextLine());
StringBuilder sb = new StringBuilder(str.substring(0,n));
System.out.println(sb.reverse());
sc.close();
}
}
Try this code. I solved the same problem, I got it right.
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int N = sc.nextInt();
StringBuilder sb = new StringBuilder(str.substring(0,N));
System.out.print(sb.reverse().toString());
}
}

I am developing a hexadecimal to decimal converter

The output of my program should be in the text variable but it does not work.
Please help me, it is for a school competition. When I give it for ex.:AE2362 it simply prints nothing(value that I declared before to it).
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[] g = new String[6];
String abc = "ABCDEF";
for(int i =0; i<= g.length-1;i++) {
char c = abc.charAt(i);
g[i] = String.valueOf(c);
}
Scanner sc = new Scanner(System.in);
System.out.println("Enter a hexadecimal code:");
String inp = sc.nextLine();
sc.close();
String text ="";
for(int i =0;i<=inp.length()-1;i++) {
char c2 = inp.charAt(i);
for(int h =0;h<=g.length-1;h++) {
if(String.valueOf(c2)==g[h]) {
text += String.valueOf(h+10);
}
}
if(Integer.valueOf(c2)>=0&&Integer.valueOf(c2)<=9) {
text += String.valueOf(c2);
}
}
System.out.println(text);
}
}
check this solution if you are going to develop it yourself. if fact it is already developed, you don't have to invent one.
String hexValue = "put your hex in quotes";
int decimalValue = Integer.parseInt(hexValue, 16);
Hope it will help

How can I make a java program that takes an input of words, finds certain words, replaces them and then prints out everything again?

I am not sure what to do, I have found all this online and I am trying to change it to do what I explained above but I am stuck. Basically what I want to do is copy and paste an essay into the code somewhere, the have it look through the essay for any words i tell it to look for, and if it finds them then to replace it with the word or words I want it to.
/**
*
import java.util.Arrays;
public class Main {
public static String[] wordList(String line){
return line.split(" ");
}
public static void main(String args[]) {
String words = "test words tesing";
String[] arr = wordList(words);
for(words i=0; i<words.length; i++)
for (String s: arr)
System.out.println(s);
}
}
*/
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main ( String args[] ){
String[] sEnterWord = getSortedWordArr();
showWordlist(sEnterWord);
String sWordToChange = getInputFromKeyboard("Which word would you like to change? ");
System.out.println("You have chosen to change the word : " + sWordToChange);
changeWordInArray(sWordToChange, sEnterWord);
Arrays.sort(sEnterWord);
showWordlist(sEnterWord);
}
private static String[] getSortedWordArr(){
String line = getInputFromKeyboard("How many words are you going to enter? ");
int length = Integer.valueOf(line);
String[] sEnterWord = new String[length];
for(int nCtr = 0; nCtr < length; nCtr++){
sEnterWord[nCtr] = getInputFromKeyboard("Enter word " + (nCtr+1) + ":");
}
Arrays.sort(sEnterWord);
return sEnterWord;
}
private static String getInputFromKeyboard(String prompt){
System.out.print(prompt);
Scanner s = new Scanner(System.in);
String input = s.nextLine();
return input;
}
private static void showWordlist(String[] words){
System.out.println("Your words are: ");
for (String w : words){
System.out.println(w);
}
}
private static void changeWordInArray(String word, String[] array){
String newWord = getInputFromKeyboard("Enter the new word: ");
for (int i = 0; i < array.length; i++){
if (array[i].equals(word)){
array[i] = newWord;
break;
}
}
Arrays.sort(array);
}
}
To read from the keyboard use
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader (isr);
String cadena = br.readLine();
And all the words you introduced form the keyboard will be in "cadena".
To separte all the words you introduced you could use the method split from String.class.
String[] words = cadena.split(" ");
To find a specific word you could use a method and the code would be in your method would be:
String yourWord = "";
for(int i = 0; i < words.length; i++)
{
if(words[i].equals("your word"))
{
yourWord = words[i];
break;
}
}
To replece a word use the method replce(theWord, "the replacement")
You can prints all the words using a loop for with System.out.println(yourWord);

Java: find common characters in two strings

I'm asked to write a program that finds the common characters in two strings using the indexOf(char) method and a for loop. Here's what I have so far - the output comes out blank still.
import java.util.Scanner;
public class ClassName {
public static void main (String args []) {
Scanner input = new Scanner (System.in);
String a = "";
String b = "";
String c = "";
System.out.print("Enter two words: ")
a = input.nextLine();
b = input.nextLine();
for (int i = 0; i < a; i++){
char ch = a.charAt(i);
if (b.indexOf(ch) != -1){
c = c+String.valueOf(ch);
}
}
System.out.print("Common letters are: "+c);
}
}
output here
I'm not sure where to go from here.
thanks
Your code will duplicate common characters for example if you compare "developper" to "programmer" your result string will contain three time the e character
If you don't want that behaviour I suggest that you also use a Set like this:
public class CommonCharsFinder {
static String findCommonChars(String a, String b) {
StringBuilder resultBuilder = new StringBuilder();
Set<Character> charsMap = new HashSet<Character>();
for (int i = 0; i < a.length(); i++) {
char ch = a.charAt(i); //a and b are the two words given by the user
if (b.indexOf(ch) != -1){
charsMap.add(Character.valueOf(ch));
}
}
Iterator<Character> charsIterator = charsMap.iterator();
while(charsIterator.hasNext()) {
resultBuilder.append(charsIterator.next().charValue());
}
return resultBuilder.toString();
}
// An illustration here
public static void main(String[] args) {
String s1 = "developper";
String s2 = "programmer";
String commons = findCommonChars(s1, s2);
System.out.println(commons);
}
}
Result from the example:
public Set<Character> commonChars(String s1, String s2) {
Set<Character> set = new HashSet<>();
for(Character c : s1.toCharArray()) {
if(s2.indexOf(c) >= 0) {
set.add(c);
}
}
return set;
}
public class CommonCharFromTwoString {
public static void main(String args[])
{
System.out.println("Enter Your String 1: ");
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
System.out.println("Enter Your String 2: ");
String str2 = sc.nextLine();
Set<String> str = new HashSet<String>();
for(int i=0;i<str1.length();i++){
for (int j = 0; j < str2.length(); j++) {
if(str1.charAt(i) == str2.charAt(j)){
str.add(str1.charAt(i)+"");
}
}
}
System.out.println(str);
}
}

Statement if doesn't work

so I have being doing this program to verify if the word "bob" is in a string that the user enters. Example: "asddbobasd" -> "here is bob". Also, if there is one character between the bs it should print out "here is bob". Otherwise, it should print "here isn't bob".
tl dr: bxb = here is bob .bob = here is bob. bok = here isn't bob. When I'm executing the program, netbeans throws me an (!) at the if statement of (yed=='b' && zed=='b')
package lab;
import java.util.Scanner;
public class Lab {
public static void main(String[] args) {
char yed,zed;
Scanner sc = new Scanner (System.in);
String x;
System.out.println("Word");
x = sc.nextLine().toLowerCase();
int m=0;
for (int i = 0; i<=x.length(); i++) {
yed = x.charAt(i);
int j=i+2;
zed = x.charAt(j);
if (yed=='b' && zed=='b')
m++;
}
if (m>0){
System.out.println("here is bob");
}
if (m==0) {
System.out.println("here isn't bob");
}
}
}
Try to replace this:
for (int i = 0; i<=x.length(); i++) {
With using i<x.length()-2 instead of i<=x.length():
for (int i = 0; i<x.length()-2; i++) {

Categories

Resources