I am developing a hexadecimal to decimal converter - java

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

Related

How can I detect all the capital letters in a string given by the user?

So I'm trying to write a method where it detects all the capital letters given by the user. How is this possible? Is it possible to do it using ASCII values? Is it possible in another way? Please give me suggestions and about the best way to come up with this algorithm
NOTE: problem is as stated at bottom of the source CODE
SOURCE CODE
Scanner sc = new Scanner(System.in);
private String userPassword;
private char[] passChar;
private int passwordScore = 0;
public String getPassword(){
userPassword = sc.nextLine();
passChar = userPassword.toCharArray();
return userPassword;
}
// Ingore this comment below, this is just for me
//do for( char i : passChar.length) or actualy if statmen so that if the pass is not 8 charaters long it doesnt qulafiy
public void passRequirments(){
}
public int passLength(){
for(char upperCase: passChar){
passwordScore = passwordScore + 4;
}
return passwordScore;
}
//THIS IS WHERE IM HAVING TROUBLE
public void passUpper(){
for (char c : passChar){
if (Character.isUpperCase(passChar.charAt(c))){
}
}
}
THE PROBLEM
public void passUpper(){
for (char c : passChar){
if (Character.isUpperCase(passChar.charAt(c))){
passwordScore = passwordScore + ((userPassword.length()- passChar.charAt(c)) * 2);
}
}
}
I think something like this could be a feasible solution
import java.io.*;
import java.util.*;
public class Main{
public static int printUpperCaseLetters(String s)
{
char[] ch = new char[s.length()];
ch = s.toCharArray();
int cap_count = 0;
for(int i=0; i<ch.length; i++)
{
int val = (int)ch[i];
if(val>=65 && val<=90)
{
cap_count++;
System.out.println(ch[i]);
}
}
return cap_count;
}
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
String s = input.next();
// So this method returns you the capital letters count inside of the string
int cap_count = printUpperCaseLetters(s);
}
}
Hope this help!
You can fetch the function part and add it to your code.
Just focus on the method printUpperCaseLetters and pass your String to this method.
Just use regex to replace all non capital letters to empty space:
string = string.replaceAll("[^A-Z]", "");
You will end up with string that contains only capital letters from input.

Java StringBuffer cannot print out apostrophe

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!

JAVA Reversing a string on multiple lines output

Why in my program am i getting the output on one single line ? like abc123... I want my output to be printed on multiple lines, same as my inputs..
import java.util.Scanner;
class Reverse
{
public static void main(String args[])
{
String original;
String reverse = "";
Scanner kbd = new Scanner(System.in);
while (kbd.hasNextLine()) {
original = kbd.nextLine();
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- ) {
reverse = reverse + original.charAt(i);
}
}
System.out.println(reverse);
}
}
It should be for example like this :
input: abc
123
...
output:cba
321
...
Here's one way of doing it:
import java.util.Scanner;
class Reverse {
public static void main(String args[])
{
String original;
String reverse = "";
Scanner kbd = new Scanner(System.in);
StringBuilder output = new StringBuilder();
while (kbd.hasNextLine())
{
original = kbd.nextLine();
StringBuilder sb = new StringBuilder(original);
output.append(sb.reverse().toString()).append("\n");
}
System.out.println(output.toString());
}
}
EDIT I noticed that in your question it seems that you only want to print the output after all input has been provided. I've modified the code from my original answer to do this.
import java.io.*;
import java.util.*;
public class reverseString {
public static void main(String[] args) {
String input="";
System.out.println("Enter the input string");
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
input = br.readLine();
char[] try1= input.toCharArray();
for (int i=try1.length-1;i>=0;i--)
System.out.print(try1[i]);
}
catch (IOException e) {
e.printStackTrace();
}
}}
try this:
package stackoverflow;
import java.util.Scanner;
class Reverse
{
public static void main(String args[])
{
String original;
String reverse = "";
Scanner kbd = new Scanner(System.in);
while (kbd.hasNextLine()) {
original = kbd.nextLine();
//befor you add the reversed string add a jump line firs
if(reverse.length()>0)reverse=reverse+"\n";
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- ) {
reverse = reverse + original.charAt(i);
}
}
System.out.println(reverse);
}
}
You should change this part
for ( int i = length - 1 ; i >= 0 ; i-- ) {
reverse = reverse + original.charAt(i);
}
in
for ( int i = length - 1 ; i >= 0 ; i-- ) {
reverse = reverse + original.charAt(i);
}
reverse = reverse + '\n';
This will add new line character.
I have one advice for you - use StringBuilder for new reverse string
Like this:
public static void main(String args[]) {
String original;
StringBuilder sbReverse = new StringBuilder();
Scanner kbd = new Scanner(System.in);
while (kbd.hasNextLine()) {
original = kbd.nextLine();
int length = original.length();
for (int i = length - 1; i >= 0; i--) {
sbReverse.append(original.charAt(i));
}
sbReverse.append('\n');
}
System.out.println(sbReverse.toString());
}
The reason - in Java, strings are immutable.
That mean every time it execute reverse = reverse + original.charAt(i); it will be created a new string in memory.
You're getting the output in a single line because you are using System.out.printf(). Use System.out.println() instead.
PS: an easier way to reverse a string would be to use reverse() from StringBuilder.
reverse = new StringBuilder(original).reverse().toString();

Changing input so in can take any length of string, problems with output

Currently I have a method that asks user for an input string but only outputs the first 16 characters! The method is supposed to take in any length of string then output the characters in 4x4 blocks after it does the following: first row remains the same. Shift the second row one position to the left, then shifts the third row two positions to the left. Finally, shift the fourth row three positions to the left. As of now it will only output the first 4x4 block
Also I am not sure how I can change the method so it doesnt ask for user input
I would like it to use a given string like:
String text = shiftRows("WVOGJTXQHUHXICWYYMGHTRKQHQPWKYVGLPYSPWGOINTOFOPMO");
"WVOGJTXQHUHXICWYYMGHTRKQHQPWKYVGLPYSPWGOINTOFOPMO" is the given encrypted string I would like to use. but without asking for user input..I keep getting errors and incorrect outputs..please show how I might fix this
code I am using:
public class shiftRows {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String[] input= new String[4];
String[] output= new String[4];
System.out.println("Enter a String");
String inputStr = sc.next();
for (int i = 0, n = 0; i < 4; i++, n+=4) {
input[i] = inputStr.substring(0+n, 4+n);
}
// -
output[0] = input[0];
for(int i=1; i<4; i++)
{
output[i] = Shift(input[i],i);
}
for(int i=0; i<4; i++)
{
System.out.println(output[i]);
}
}
public static String Shift(String str, int shiftNum)
{
char[] out = new char[4];
if(shiftNum==1)
{
out[0]=str.charAt(1);
out[1]=str.charAt(2);
out[2]=str.charAt(3);
out[3]=str.charAt(0);
}
if(shiftNum==2)
{
out[0]=str.charAt(2);
out[1]=str.charAt(3);
out[2]=str.charAt(0);
out[3]=str.charAt(1);
}
if(shiftNum==3)
{
out[0]=str.charAt(3);
out[1]=str.charAt(0);
out[2]=str.charAt(1);
out[3]=str.charAt(2);
}
return new String(out);
}
}
Here's a good way to do it :
import java.util.Scanner;
public class shiftRows {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String inputStr = "WVOGJTXQHUHXICWYYMGHTRKQHQPWKYVGLPYSPWGOINTOFOPMO";
for (int i = 0 ; i < inputStr.length() ; i++){
System.out.print(inputStr.charAt(i));
if ((i + 1)%4 == 0) System.out.println();
}
}
}
If you want to stock it into a String, just concatenate at each loop and add a "\n" each time the if test is valid.

java beginner programming, putting strings in char array to filter input

in this code I've been trying to filter the characters in the array with a for-loop to a second array, but am unable to. Could anyone tell me what is exactly wrong with my code?
public class Deel1 {
public static void main(String[] args) {
String zinInvoer = getInput();
String zinUitvoer = filterZin(zinInvoer);
}
static String getInput() {
Scanner scan = new Scanner(System.in);
String zinInvoer = "";
System.out.println("Voer een zin in: ");
if (scan.hasNextLine()) {
zinInvoer = scan.nextLine().trim();
}
if (zinInvoer.equals("")) {
System.out.println("Geen invoer!");
System.exit(0);
}
return zinInvoer;
}
static String filterZin(String zinInvoer) {
String zinUitvoer = "";
char ongefilterd[] = zinInvoer.toCharArray();
String nogFilteren = new String(ongefilterd);
char a = nogFilteren.charAt(97);
for (a = 97; a <= 122; a++) {
a = a += 32;
char gefilterd[] = //second array to be printed
}
System.out.println("Gefilterd: " + zinUitvoer);
return zinInvoer;
}
}
Sorry if it annoys you but I had to translate your variables into english in order to figure out what their purposes were.
First of all, it will always throw an exception when the string is less than 98 letters long because it looks for the 97th letter.
Second, the for loop in "filterZin" will only filter letter # 98, which I am guessing was not your intention.
Also, geFilterd should probably be created outside of the for loop, and in the for loop you (i guess) would want to do
geFilterd[a]=a+32;
a+=32;
Because I couldn't figure out what your overall goal was for this program, I made a version of it that does what I think you were trying to do, but again, I do not know.
import java.util.Scanner;
public class Deel1 {
public static void main(String[] args) {
String phraseInput = getInput();
filterPhrase(phraseInput);
}
static String getInput() {
Scanner scan = new Scanner(System.in);
String phraseInput = "";
System.out.println("Voer een zin in: ");
if (scan.hasNextLine()) {
phraseInput = scan.nextLine().trim();
}
if (phraseInput.equals("")) {
System.out.println("Geen invoer!");
System.exit(0);
}
return phraseInput;
}
static String filterPhrase(String phraseInput) {
String phraseOutput = "";
char onFiltered[] = phraseInput.toCharArray();
String currentFilter = new String(onFiltered);
// for (a = 97; a <= 122; a++) {
// a = a += 32;
// //char filtered[] = //second array to be printed
// }
char[] filtered = new char[26];
for(int i=97;i<=122;i++){
char a = currentFilter.charAt(i);
filtered[i-97]= (char) (a+32);
}
System.out.println("filtered: " + filtered.toString());
return phraseInput;
}
}

Categories

Resources