how can I change the lowercase array values to uppercase? - java

I need "args[i]" to be converted to Uppercase so the output will be:
"$ARG1" line break
"$ARG2" line break
"$ARG3" line break
and so on. I need to use the "toUpperCase" method but don't know how.
public class Main {
public static void main(String[] args) {
System.out.println("Number of args:" +
args.length);
for(int i=0; i<args.length; i++){
char dollar = '\u0024';
System.out.println(dollar + args[i]);
}
}
}

Java has this functionality built into the String object like so:
System.out.println(dollar + args[i].toUpperCase());
See the Oracle documentation here

Just use .toUpperCase() on any String, and it will return an all-upper-case String.
System.out.println(dollar + args[i].toUpperCase());

java has String method :public String toUpperCase()
public class Main {
public static void main(String[] args) {
System.out.println("Number of args:" + args.length);
for(int i=0; i<args.length; i++){
char dollar = '\u0024';
System.out.println(dollar + args[i].toUpperCase());
}
}
}
See the Oracle documentation here

public class Main {
public static void main(String[] args) {
System.out.println("Number of args:" +
args.length);
char dollar = '\u0024';
for(int i=0; i<args.length; i++){
System.out.println(dollar + args[i].toUpperCase());
}
}
}

Related

How to split a string and compare for equality in Java?

suppose we have
String str = "Hello-Hello1";
How do we split it and compare it to see if it is equal or not?
This is what I wrote, but it does not give me the result.
public static void main(String[] args) {
String str = "Hello-Hello1";
String [] a = str.split("-");
for(int i=0; i<a.length; i++) {
System.out.print(a[i]+ " ");
}
for(int first =0; first<a.length; first++) {
for(int second =first+1; second<a.length; second ++) {
if(a[first].equals(a[second])){
System.out.println(a[first]);
}
}
}
}
First, you split your string like shown here:
How to split a string in Java, then compare them using the equals method: a[0].equals(a[1]);
Thank you all for helping
Here is the answer
public class SplitStringAndCompare {
public static void main(String[] args) {
String str = "Hello-Hello1";
String [] a = str.split("-");
if(a[0].equals(a[1])) {
System.out.println(a[0]+ " is equal to " + a[1]);
} else {
System.out.println(a[0]+ " is not equal to "+ a[1]);
}
}

Weird String won't split (Whitespaces or Tabs)

I am having troubles splitting this string into the different values.
When splitting it using R, there is no problem splitting it using sep="\t".
But in Java I cannot make it work.
I copied the string from the file that I am reading from and it seems to be reproducible in the online java fiddlers.
I've already tried "\s+", "\t+", "\t", "\t", "\t+".
Maybe the String is not tab delimited? But why does R work then?
public class JavaFiddle {
static String s = " 1 0 3 150.00";
public static void main(String[] args) {
System.out.println(s.split("\\t+")[0]);
}
}
I think you can use the \\s :
public class JavaFiddle {
static String s = " 1 0 3 150.00";
public static void main(String[] args) {
String[] split = s.trim().split("\\s+");
for(int i=0; i < split.length; i++){
System.out.println(i + "-->" + split[i]);
}
}
}
And the output is:
0-->1
1-->0
2-->3
3-->150.00

Java program to reverse a string not working?

I have written a piece of code to reverse a string in Java. However, its showing multiple errors and I wish to understand where it is that I am going wrong. I know that there are alternative methods of reversing a string. However, I want to know where I am going wrong with my code.
public class RevString {
public static void main(String[] args)
{
public Reverse (String str)
{
int len = str.length();
String rev;
for (int i = 0; i <= len; i++)
{
rev = str[i] + rev;
}
System.out.println(rev);
}
Reverse("Canyon");
}
}
Errors:
Multiple markers at this line
- Syntax error on token ")", ; expected
- Syntax error on token "(", . expected
- Reverse cannot be resolved to a type
- Illegal modifier for parameter str; only final is
The method Reverse(String) is undefined for the type
RevString
Could someone provide me with a resolution?
There are many errors in your code :
For loop condition should be i < len
String rev should be initialized to "" (empty string), else it will throw error when you try to append another string to it.
You can't access characters in a string using str[i], use str.charAt(i) instead.
You are trying to initialize a function (Reverse) inside another function (main), you must initialize it outside the main function.
Also, here is a simple one liner for string reversal:
new StringBuilder(str).reverse().toString()
A good tip might be to use the StringBuilder class whenever you want to do any kind of string manipulation in Java.
Your code has many issues:
You are declaring the method Reverse() inside the main method.
You also need to initialize rev to an empty string.
You can use str.charAt(i) to access each character of the string.
Your for loop goes beyond the string if you use i <= len; so it
should be i < len;.
Your Reverse() method should be static since you are calling it in main
method (which is static)
Here is working code.
public class RevString {
public static void main(String[] args) {
Reverse("Canyon");
}
public static void Reverse (String str) {
int len = str.length();
String rev="";
for (int i = 0; i < len; i++) {
rev = str.charAt(i) + rev;
}
System.out.println(rev);
}
}
Please see the below code:
public class Hello {
public static String reverse (String str){
int len = str.length();
String rev="";
char[] strArray = str.toCharArray();
for (int i = 0; i < len; i++)
{
rev = strArray[i] + rev;
}
return rev;
}
public static void main(String[] args) {
String result = reverse("Canyon");
System.out.println("Reversed String: " + result);
}
}
public class reverseString
{
public static void main(String[] args)
{
System.out.println("Welcome to the the string reverser.");
System.out.println("Here is where a person may put is a sentence and the orintation" +
" of the words is reversed.");
Scanner keyboard = new Scanner(System.in);
String word = keyboard.nextLine();
int lengthOf = word.length();
int j = 0;
char loopStr;
String LoopStr;
String Nstr = "";
for(int n = lengthOf; n >0 ;n--)
{
j = n;
LoopStr = word.substring(j-1,j);
Nstr = Nstr + LoopStr;
}
System.out.println(Nstr);
}
}

Error while trying command line output of arguments

What is the error in following code while trying command line arguments ? I am getting an error at line System.out.println(args[i]);
public class CommandLA{
public static void main(String []args)
{
int s = 0;
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
s = s + Integer.parseInt(args[i]);
System.out.println("Sum is : "+s);
}
}
maybe
public static void main(String []args)
{
int s = 0;
for (String str : args) {
s = s + Integer.parseInt(str);
}
System.out.println("Sum is : "+s);
}
or using an indexed for
public static void main(String []args)
{
int s = 0;
for (int i = 0; i < args.length; i++) {
s = s + Integer.parseInt(args[i]);
}
System.out.println("Sum is : "+s);
}
Simple:
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
followed by
s = s + Integer.parseInt(args[i]);
But - you are missing the { after the loop! Therefore the scope in which i exists (is visible) is only the line directly after the "for-loop" line!
In other words you need for (..) { all stuff that uses i }!
To elaborate on the answer of GhostCat:
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
s = s + Integer.parseInt(args[i]);
is the same as
for(int i=0;i<args.length;i++){
System.out.println(args[i]);
}
s = s + Integer.parseInt(args[i]);
Which means that in the last line, i is not known, resulting in an error.
I wonder why the error was detected at the line before, because until that, the code is technically correct.
That said, I recommend to use brackets in any case. Some people omit them to get shorter code, but that means that if one adds a line later on, he could easily make a mistake. This is personal preference, of course.

replacing elements of an array change by change

I'm trying to do some basics concerning an array of elements, specifically characters. My question is, how do I get the program to print my changes one by one? for example (I do not want my output going from "moon" to "mOOn" in one instance, but from "moon" to "mOon" to "mOOn", like that. Here is my code.
import java.util.*;
public class Practice
{
public static void main(String[] args)
{
String array[] = {"uuuuuuuupppppppssssssssss"};
System.out.println(Arrays.toString(array));
for (int i = 0; i < array.length; i++)
{
System.out.println(array[i] = array[i].replace('p', 'P'));
//trying to print each change here
}
}
}
Thanks again!
EDIT/Update: I got the output to get the loop right, but the output is still not what I want (basically output: uPPPPPPS, uPPPPPPs, uPPPPPs, etc until the length of p ends). Any hints on what I could do? Thanks!
public static void main(String[] args){
String array = "uuuuuuuupppppppssssssssss";
System.out.println(array);
char[] chars = array.toCharArray(): //converted
for (int i = 0; i < chars.length; i++) {
if (chars[i] == 'p') {
System.out.println(array.replace('p', 'P'));
}
}
}
public static void main(String[] args)
{
String array = "uuuuuuuupppppppssssssssss";
System.out.println(array);
while ( array.contains("p") )
{
array = array.replaceFirst("p", "P");
System.out.println(array);
}
}

Categories