Splitting input and result as array [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
If i have an input smt. like that:
1,10;3,3;4,1. Lets say String input.
How can I split it in ";", so the result could be like this:
[1,10]
[3,3]
[4,1]
Thanks!

I think you want to do something like this.
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input;
//your input is 1,10;3,3;4,1
input = scanner.next();
String[] splitted = input.split(";");
//save the new list or array
ArrayList list = new ArrayList();
for (int i = 0; i < splitted.length; i++) {
System.out.println(splitted[i]);
//for later usage
list.add(splitted[i]);
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}

Related

reverse element of the array in java? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to print the array in reverse order but I don't know why it's not working. I want to reverse them using the way I have mentioned below, in a single line, but my logic is not working.
void dynamicinputArraytwo() throws IOException {
int buffer;
int i,jo,io;
System.out.println("Enter size");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
buffer = Integer.parseInt(br.readLine());
float arrtwo[]=new float[buffer];
for( i=0;i< arrtwo.length;i++){
System.out.println ("Enter elements of array=");
arrtwo[i] = Float.parseFloat(br.readLine());
}
for (int jx=0; jx < arrtwo.length; jx++){
System.out.println("array before reverse "+arrtwo[jx] + " ");
}
float arrthree[];
arrthree = new float[arrtwo.length];
for(io=0,jo=arrtwo.length; jo>=0;io++,jo--){
arrthree[io] = arrtwo[jo] ;
}
for(io=0; io < arrtwo.length; io++){
arrtwo[io] = arrthree[io] ;
}
for (int jx=0; jx < arrtwo.length; jx++){
System.out.println("array after reverse "+arrtwo[jx] + " ");
}
}
Your first problem is here:
for(io=0,jo=arrtwo.length; jo>=0;io++,jo--){
arrthree[io] = arrtwo[jo] ;
}
The last index of an array is length - 1 because arrays are indexed starting at 0, so you'll have an exception (i.e. arrtwo[arrtwo.length] is not a valid index in the array). You should set jo to arrtwo.length - 1 initially instead.

How to extract email adresses from this string [closed]

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 5 years ago.
Improve this question
For example I have this string, where email addresses are differently formatted:
"a.b#c.d" <a.b#c.d>|"Michal pichal (michal.pichal#g.d)" <michal.pichal#g.d>|Melisko Pan <melisko.pan#domena.sk>
I need to extract the email addresses in a form of:
a.b#c.d|michal.pichal#g.d|melisko.pan#domena.sk
My idea was to get any char near # from group [0-9][A-Z][a-z]#.- but I do not know how. Please help with some hint.
This regex extracts emails out of your string:
public static void main(String[] args) {
Pattern pattern = Pattern.compile("[\\w.]+#[\\w.]+");
Matcher matcher = pattern.matcher("\"a.b#c.d\" <a.b#c.d>|\"Michal pichal (michal.pichal#g.d)\" <michal.pichal#g.d>|Melisko Pan <melisko.pan#domena.sk>\r\n");
while(matcher.find()){
String group = matcher.group();
System.out.println("group="+group);
}
It prints:
group=a.b#c.d
group=a.b#c.d
group=michal.pichal#g.d
group=michal.pichal#g.d
group=melisko.pan#domena.sk
import java.util.Scanner;
class test{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String res = ""; //This holds the final result
for(int i=0; i<str.length(); ++i){ //Loop through the entire string
if(str.charAt(i)=='<'){
String build = "";
for(int j=i+1; j<str.length(); ++j){
if(str.charAt(j)=='>'){
break; //Break when we hit the '>'
}
build += str.charAt(j); //Add those between < and >
}
res += build + "|"; //Add the pipe at the end
}
continue;
}
System.out.println(res);
}
}
This ought to do it.
Just run simple nested loops. No need for regex.

How does one go about doing so [closed]

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
Write a method called multiConcat that takes a String and an integer as parameters. Return a String made up of the string parameter concatenated with itself count time, where count is the integer. for example, if the parameters values are “ hi” and 4, the return value is “hihihihi” Return the original string if the integer parameter is less than 2.
What i have so Far
import java.util.Scanner;
public class Methods_4_16 {
public static String multiConcat(int Print, String Text){
String Msg;
for(int i = 0; i < Print; i ++ ){
}
return(Msg);
}
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
int Prints;
String Texts;
System.out.print("Enter Text:");
Texts = Input.nextLine();
System.out.print("Enter amount you wanted printed:");
Prints = Input.nextInt();
System.out.print(multiConcat(Prints,Texts));
}
}
Just a few hints:
concating strings can be done this way: appendTo += stuffToConcat
repeating an operation n times can be done with a for-loop of this kind:
for(int i = 0 ; i < n ; i++){
//do the stuff you want to repeat here
}
Should be pretty simple to build the solution from these two parts. And just in case you get a NullPointerException: remember to initialize Msg.
Try this:
public static String multiConcat(int print, String text){
StringBuilder msg = new StringBuilder();
for(int i = 0; i < print; i ++ ) {
msg.append(text);
}
return msg.toString();
}
I have used StringBuilder instead of a String. To know the difference, give this a read: String and StringBuilder.
Also, I would guess you are new to Java programming. Give this link a read. It is about Java naming conventions.
Hope this helps!

Arrays in Java? [closed]

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
so i need to find a way to get my program to hold a string of names that the user is going to input. the array will hold a total of five names and is going to output all the information back to the user with their entered names. I am using a single main class.
so far this is what i have:
import java.util.Scanner;
public class Names {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner (System.in);
String [] name = new String [5];
for (int i = 0; i < 5; i++){
System.out.println("Enter name: ");
String UserNames = input.nextLine();
name[i] = UserNames;
}
}
}
I need to know if this is storing the names correctly in the array? I am very new to java and need some insight from the pros. Also if i want to get the names to repeat back to them will it look like this
{
System.out.println(" The names you entered are:" + UserNames );
}
Thanks for any help I can get.
i need to know if this is storing the names correctly in the array?
So, print the array once you've filled it with Arrays.toString()
It seems you're doing it right.
To print them, you need to loop through the array.
for (int i = 0; i < name.length; i++){
System.out.println(name[i]);
}
as suggested by Kepani you can try Arrays.toString(arrayVarName)
public static void main(String[] args)
{
String [] arx = {"alpha", "beta", "gamma", "penta", "quad"};
System.out.println(arx); // returns object hashcode and not the strings stored
System.out.println(Arrays.toString(arx));
}
in case you do not know whether you will be getting 5 inputs or more you can try arraylist
public static void main(String[] args)
{
ArrayList<String> strList = new ArrayList<String>();
strList.add("alpha");//Construct would be strList.add(input)
strList.add("beta");
strList.add("gamma");
strList.add("penta");
strList.add("quad");
System.out.println(strList);
System.out.println(strList.toString());
}
as you would realize due to the use of generics ArrayList.toString() return the complete list of strings store

Purchase Java with Arrays [closed]

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'm Raymond, computer programming student. I have problem about arrays. Are instructor ask us to do a program that goes like this.
in this codes below. i want to display the same item code i entered. but the problem is that once i answered yes and input again number, the only thing that display is the last number or code i enter.
import java.util.Scanner;
public class _TindahanArray {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
String ans, i = "";
int x;
do {
System.out.print("Item code:");
i += a.next();
System.out.print("\nAnother item? [y/n]:");
ans = a.next();
} while (ans.equals("y"));
String[] code = new String[2];
for (x = 0; x < 1; x++) {
code[x] = i;
System.out.print(code[x]);
code[x] = "\n";
System.out.print(code[x]);
}
}
}
As you shown some efforts, I just want to update your code.
Your code is fine for only printing two item codes.
Use collection ArrayList to store the item codes. I am using String array list.
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayTest {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
String ans;
ArrayList<String> itemCodeList = new ArrayList<String>(); //create array list
do{
System.out.print("Item code:");
itemCodeList.add(a.next()); //add item code into array list
System.out.print("\nAnother item? [y/n]:");
ans = a.next();
}while(ans.equals("y"));
for (String code : itemCodeList)
{
System.out.println(code);
}
}
}
ArrayList example

Categories