Turning stdin input into an integer array Java [duplicate] - java

This question already has answers here:
how to convert an integer string separated by space into an array in JAVA
(5 answers)
Closed 2 years ago.
I am trying to read user input from a scanner into an integer array, I currently have this:
int [] arr1 = {Integer.parseInt(sc.nextLine().split(" "))};
But i am given the error
String[] cannot be converted to String
Any help would be much appreciated :)

sc.nextLine().split(" ") //This returns String[]
int a = Integer.parseInt("") //Integer.parseInt requires one String param.
Try below code:
String input = sc.nextLine();
String[] inputs = input.split(" ");
List<Integer> ints = Arrays.stream(inputs).
map(Integer::parseInt).collect(Collectors.toList());
Check the document:
API: Integer.parseInt
String.split()

Related

Java how to print arrays? [duplicate]

This question already has answers here:
Cannot invoke add on the array type
(2 answers)
Java increase array by X size
(6 answers)
Closed 2 years ago.
I'm kinda new to programming and I know that the problem is pretty simple.. but I couldn't figure it out..
Let's say that I'd like to create an arraylist like this ;
String words [ ] = { } ; instead of ArrayList words = new ArrayList(); or
ArrayList<String> words= new ArrayList<String>();
but the problem is... when I want to add the words that we scan on the string that we created to the list , I get an error..
("Cannot invoke add(String) on the array type String[]
") I've tried to use Arrays.toString() method.. but I guess there is
something that I miss.. Could you guys help me out?
import java.util.Arrays;
import java.util.Scanner;
public class testing_Stuff {
public static void main(String[] args) {
String sentence =("This is texting");
Scanner scan = new Scanner(sentence);
String words [] = {};
while(scan.hasNext()) {
words.add((scan.next());
}
System.out.println(words);
}
}
You are trying to use the add method on an array which is not possible. Also, you have some syntax mistakes.
You can either define a length to the array from the start and add to it as follows:
String sentence ="This is texting";
Scanner scan = new Scanner(sentence);
String words[] = new String[sentence.split(" ").length];
int i = 0;
while(scan.hasNext()) {
words[i++] = scan.next();
}
System.out.println(Arrays.toString(words));
Or you can use a list as follows:
String sentence = "This is texting";
Scanner scan = new Scanner(sentence);
List<String> words = new ArrayList<>();
while(scan.hasNext()) {
words.add(scan.next());
}
System.out.println(words);
In both cases the output would be:
[This, is, texting]

Removing all certain characters from an ArrayList [duplicate]

This question already has answers here:
Remove all non alphabetic characters from a String array in java
(9 answers)
Removing all characters but letters in a string
(4 answers)
Closed 4 years ago.
How do you remove all certain elements or characters from an ArrayList?
Let's say I have a scanner object like
Scanner keyboard = new Scanner(System.in);
String userInput = keyboard.nextLine();
And the user inputs something like
Bubblesort.=
If I wanted to save the characters of the String variable to an ArrayList, I'd use a char ArrayList and then load the characters in using a for loop like so
ArrayList<Character> stringArrayList = new ArrayList<Character>();
for (int i = 0; i < userInput.length(); i++) {
stringArrayList.add(userInput.charAt(i));
}
My question is, how do I remove all the characters which aren't letters. Like '.', and '=' so that the ArrayList contains only the word "Bubblesort"?

taking multiple inputs in a specific format as a single input in java [duplicate]

This question already has answers here:
Is there an equivalent method to C's scanf in Java?
(7 answers)
Closed 5 years ago.
I have been using the scanner so far for taking inputs in java but what i need for this particular task is setting values for 4 variables with a single line input i.e. 07:05:45PM
The C equivalent solution for this particular solution is as follows:
scanf("%d:%d:%d%s", &hh, &mm, &ss, t12) ;
I am looking for the java equivalent of this code.
You could use a Scanner instance by setting to it a delimiter such as "\\s*:\\s*".
It could give this code :
Scanner s = new Scanner("07:05:45PM").useDelimiter("\\s*:\\s*");
int hours = s.nextInt();
int minutes = s.nextInt();
String secondAndMeridiem = s.next();
int seconds = Integer.valueOf(secondAndMeridiem.substring(0, 2));
String meridiem = secondAndMeridiem.substring(2, 4);
You can try delimiter option in scanner, but it will not support multiple delimiters together. So it will not support your requirement of ""%d:%d:%d%s"" fully.
Please have a look at
How to read comma separated integer inputs in java
Just curious to know, why don't you look at the SimpleDateFormatter to parse the single string and get hour, minutes, seconds
The closest I could get is this but this would work only if there was some delimiter between 45 and PM in 07:05:45PM like 07:05:45 PM. Otherwise, the input needs to be taken as String and parsed.
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int hh, mm, ss;
String t12;
scanner.useDelimiter("[:\n ]");
hh = scanner.nextInt();
mm = scanner.nextInt();
ss = scanner.nextInt();
t12 = scanner.next();
System.out.println(hh);
System.out.println(mm);
System.out.println(ss);
System.out.println(t12);
}

How to take multiple string value as input? [duplicate]

This question already has answers here:
How can I read input from the console using the Scanner class in Java?
(17 answers)
Closed 5 years ago.
I have a string variable that I use to get input values. for ex.
Scanner in=new Scanner(System.in);
varName=in.next();
when I give value as (John jony) it only displays John. Any way to get whole string?
Use the following instead:
Scanner in=new Scanner(System.in);
String text = in.nextLine();
System.out.println( text );
"in.nextLine()" reads in a whole line

Return the user input backwards e.g. Laura would be returned as aruaL [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
In this program I am trying to return the user input backwards
e.g. Laura would be returned as aruaL, but I am getting [C#11d72ca as my output..
String input = JOptionPane.showInputDialog("Enter name");
Scanner scanner = new Scanner(input);
String name = scanner.next();
scanner.close();
char[] backwards = new char[name.length()];
for(int i = 0; i<name.length(); i++)
{
backwards[i] = name.charAt(name.length()-1-i);
}
JOptionPane.showMessageDialog(null, backwards.toString());
}
There is an easier way to reverse a string:
String reverse = new StringBuilder(name).reverse().toString()
If you don't want to use this method, in order to covert a char[] to a string you would need to do something like:
String reverse = new String(backwards);
Calling toString on an array will call the toString method of the Object. This will return you the hashCode which is what you are getting

Categories