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 6 years ago.
Improve this question
how to get Name and UserName from this JSON
String str="{Name=jack,UserName=Jacki}"
in java android
Try this, code will use scanner class and its findInLine() method
String str="{Name=jack,UserName=Jacki}";
Scanner sc=new Scanner(str);
sc.findInLine("Name=");
if(sc.hasNext())
{
System.out.println(sc.next()); //This will print Name you can store it in variable as well
}
sc.findInLine("UserName=");
if(sc.hasNext())
{
System.out.println(sc.next()); //This will print UserName you can store it in variable as well
}
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Sample
{
public static void main (String[] args) throws java.lang.Exception
{
String str="{Name=jack,UserName=Jacki}";
String[] str1=str.split(",");
System.out.print(str1[0].substring(1));
System.out.print(str1[1].substring(0,str1[1].length()-1));
}
}
So in this method you basically take the string and split it on the ",". That gives you {Name=jack and UserName=Jacki}. The next two statements just parse these 2 new strings and remove the "{" and "}".
Related
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
import java.util.Scanner;
public class TextProcessing
{
public static void main(String[] args)
{
String sentence, wordToBeTargeted, wordToBeReplaced, output;
boolean wordToCheck;
Scanner myInput = new Scanner(System.in);
sentence = myInput.nextLine();
do
{
wordToCheck = true;
System.out.println("Please enter the word for replacement:");
wordToBeTargeted = myInput.nextLine;
if(sentence.toLowerCase().indexOf(wordToBeTargeted.toLowerCase()) == -1)
{
wordToCheck = false;
System.out.println("This word cannot be found in the sentence!")
}
else
{
System.out.println("Please enter the word you would like to replace with:");
wordToBeReplaced = myInput.nextLine();
}
}while(wordToCheck = false);
}
}
}
Write a file named TextProcessing.java that will replace all occurrences of a word in a string with another word
The expected outcome is like this:
I won't write the code out - you should still get something out of the exercise, but will give some direction. The first approach that comes to mind:
Split the first input on ' ' into a list
Iterate through the list, conditionally changing values based based on two input strings
As you're iterating you can either output into a new string / stringbuilder, or directly write to console depending on the requirements
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 4 years ago.
Improve this question
I want to get a user input number to print out "Color 1 Color 2..." etc. depending on what number the input is.
I want to do this but in java, and I'm not quite sure where to find it.
How to iterate a for loop for a user input in Java?
That will do the work, using only standard java classes (java.io.Console):
import java.io.Console;
public class Consoler {
public static void main(String[] args) {
final Console console = System.console();
console.printf("How may times?\n");
final String line = console.readLine();
try {
final int quantity = Integer.parseInt(line);
for (int i = 1; i <= quantity; i++) {
System.out.printf("Color %d ",i);
}
System.out.println();
} catch (final NumberFormatException e) {
System.err.println(line + " is not a number.");
}
}
}
java.util.Scanner is probably what you're looking for. As for the for loop, you should google Java for loop and read up on how they work. Then combine the two concepts.
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 4 years ago.
Improve this question
import java.util.*;
public class Ex13 {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
char firstL,secondL;
System.out.println("insert first l");
firstL=in.next().charAt(0);
System.out.println("insert second l");
secondL=in.next().charAt(0);
if (){
System.out.println("kk");
}
}
}
I need firstL and secondL to equal in the if section but I can't because the firstL and the secondL is a char type and I need to transform it to a Unicode so I can finish the project
Why don't you just check the value?
char firstL,secondL;
System.out.println("insert first l");
firstL=in.next().charAt(0);
System.out.println("insert second l");
secondL=in.next().charAt(0);
if (firstL == secondL){
System.out.println("kk");
}
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 4 years ago.
Improve this question
I get this value in String
kyc_CWaccountOperatorName|DANIYAL,kyc_cnic_ind|9110129505705,kyc_fatherName|Abujan,kyc_motherMaidenName|MOTHER,kyc_CWmobileNumber|03312551746,kyc_CWdateOfBirth|20/02/1993,kyc_cnicDateOfExpiry|2027-02-20,kyc_CWplaceOfBirth|KHI,kyc_mailAddHouseFlat No|Dha,kyc_city|Abbottabad
I want to get value of kyc_cnic_ind like
kyc_cnic_ind=9110129505705
Below is the solution for your problem:
import java.util.*;
import java.lang.*;
class Rextester
{
public static void main(String args[])
{
String str = new String("kyc_CWaccountOperatorName|DANIYAL,kyc_cnic_ind|9110129505705,kyc_fatherName|Abujan,kyc_motherMaidenName|MOTHER,kyc_CWmobileNumber|03312551746,kyc_CWdateOfBirth|20/02/1993,kyc_cnicDateOfExpiry|2027-02-20,kyc_CWplaceOfBirth|KHI,kyc_mailAddHouseFlat No|Dha,kyc_city|Abbottabad");
String[] ar = str.replace("|","=").split(",",0);
for(String s : ar)
System.out.println(s);
}
}
You can check output on the below link:
https://ideone.com/kzBYfl
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 7 years ago.
Improve this question
So I have the questions in the top part, but I want to have all the questions at the top, and when I need to ask the questions, I can just pull them down using a variable defined as the question. Right now however, the code is asking the questions from where the questions are, not using the variable "ask" and asking from System.out.print(ask). Any ideas on how to get it to do that?
import java.util.Scanner;
public class Greetings {
public static void main(String[] args) {
Scanner newscanner = new Scanner(System.in);
String ask = getString(newscanner, "Please enter your first name: ");
// String ask2 = getString(newscanner, "Please enter your last name: ");
// String ask3 = getString(newscanner, "Please enter your year of birth:
// ");
}
public static String getString(Scanner newscanner, String ask) {
System.out.print(ask);
String first = newscanner.next();
String firstletter = first.substring(0, 1).toUpperCase();
return firstletter;
}
}
Perhaps what you are looking to do is have the question be printed, and then the answer typed on the line below it? If so, what you need to do is change the first call in getString from System.out.print to System.out.println, which should add on a newline after the question, moving the input to the next line.
EDIT: This is what it might look like now:
Please enter your first name:John
And here's what it would change to:
Please enter your first name:
John