Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to print out only
Int from String and first 2 char from string as well so, how i can do that, please help me thank!!
suppose,
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class match1 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String address = "1234 main street";
}
}
Output should be like:
1234 ma
How can i do that please help me!! Thanks!!
String address = "1234 main street";
String[] tokens = address.split(" ");
System.out.println(tokens[0]+" "+tokens[1].substring(0,2));
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 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 "}".
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
is there an equivalent code in groovy for the following code in java:
import java.util.Arrays;
import java.util.List;
import java.util.Random;
class Text {
public static void main(String [] args) {
String [] array = {"hello", "world","Pasci","Jenny"};
List <String> list = Arrays.asList(array);
{
Random rand = new Random();
System.out.println("String from list: " + list.get(rand.nextInt(list.size())));
}
}
}
Thank you
Groovy's syntax is lighter and not as verbose as Java:
def list = ["hello", "world","Pasci","Jenny"]
Random rand = new Random()
println "String from list: " + list.get(rand.nextInt(list.size()))
Side note: Groovy is almost a super-set of Java, meaning, you can write almost any Java code in a groovy file and it'll compile and run.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My input String is as below,
{"string name"=hi;"id"=44401234;"string name"=hey;"id"=87695432.....}
I want only the ids as output like, {44401234 87695432}
for(int i=0;i<input.length();i++)
{
int index=input.indexOf("id");
hh=input.substring(index+4,index+12);
System.out.println(hh);
}
The below code uses Regex to get the value associated with id
try
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String input = "{\"string name\"=hi;\"id\"=44401234;\"string name\"=hey;\"id\"=87695432.....}";
Pattern pattern = Pattern.compile("\"id\"=[0-9]{8}");
Matcher m = pattern.matcher(input);
while (m.find()) {
String str = m.group();
str = str.replace("\"id\"=", "");
System.out.println(str);
}
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a simple program in progress that needs the declaration lines
read = new BufferedReader(new FileReader("marks.txt"));
and
line = read.readLine();
to be class variables. How would I do this?
Here is the code I wrote so far.
import java.io.*;
import java.math.*;
public class WriteKong
{
public static String line;
public static BufferedReader read;
public static PrintWriter write;
public static void main(String[] args) throws IOException
{
read = new BufferedReader(new FileReader("marks.txt"));
line = read.readLine();
while(line != null)
{
System.out.println(line);
read.readLine();
}
}
public static void sort()
{
// THIS IS WHAT THE FUNCTION DOES:
// > check the fourth digit in the line
// > if there is no fourth digit then store the mark
// > if mark is less than 50 then write to "fail.txt"
// > if mark is 50 or greater then write to "pass.txt"
}
}
EDIT: I want these variables to be declared as a class variable. I don't want to go through the pain of redefining the same variables in all of the methods I use.
They are class variables in your code. The code satisfies the requirements given.
If you're confused why your loop does not read all the lines from the file it's because you never assign the newly read line to to line.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am following this tutorial, for testing and this was a little test/lesson we were doing. I keep getting errors on the 11th line. Can someone tell me whats going on? This is the code, except its not complete.
import java.util.Scanner;
public class Test2Coding {
public static void main(String[] args) {}
{}
Scanner in = new Scanner(System.in);
int age;
System.out.println("How old are you?");
}
You have some extra brackets in the code. Your 3 lines of code should be directly inside the brackets after the main method declaration.
public static void main(String[] args) {
// code here
}
Just remove the brackets you are not using
import java.util.Scanner;
public class Test2Coding {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int age;
System.out.println("How old are you?");
}