The user is allowed to play with an array of strings. They can add strings to the array, remove strings from the array, search for strings in the array, and eventually they will be able to sort the array. The sorting is what is messing me up. I've tried a few different approaches. The first approach was to convert the array into an ArrayList and use Collections to sort the ArrayList, which would be converted back into the static class array. It doesn't work. The second approach I tried was to iterate through the array and try to sort only the strings added by the user instead of everything in the array (since there are some null values in the array). Perhaps I should iterate through the array and then store the non-null values into a new array that I can then sort? But what if I want to add more strings after sorting the new array? That's why I stopped with the second solution. The third attempt was to use Arrays.sort() on my array but for some reason it does not work.
Here is the exception:
Exception in thread "main" java.lang.NullPointerException
at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:157)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
at java.util.Arrays.sort(Arrays.java:472)
at java.util.Collections.sort(Collections.java:155)
at testingSearch.sortArray(testingSearch.java:93)
at testingSearch.main(testingSearch.java:42)
Here is my code:
import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class testingSearch {
static String[] strArray;
static {
strArray = new String[5];
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while(true){
System.out.println("1. Add string to the string array.");
System.out.println("2. Remove string from the string array.");
System.out.println("3. Display strings in string array.");
System.out.println("4. Search the string array for a string.");
System.out.println("5. Sort the strings in the string array.");
int userChoice = 0;
userChoice = input.nextInt();
switch(userChoice) {
case 1:
addString();
break;
case 2:
removeString();
break;
case 3:
displayStrings();
break;
case 4:
searchArray();
break;
case 5:
sortArray();
break;
}
}
}
public static void addString(){
Scanner input = new Scanner(System.in);
System.out.println("What string do you want to add?");
String userInput;
userInput = input.nextLine();
ArrayList<String> stringList = new ArrayList<String> (Arrays.asList(strArray));
stringList.add(userInput);
strArray = stringList.toArray(strArray);
}
public static void removeString(){
Scanner input = new Scanner(System.in);
System.out.println("What string do you want to remove?");
String userInput;
userInput = input.nextLine();
ArrayList<String> stringList = new ArrayList<String> (Arrays.asList(strArray));
stringList.remove(userInput);
strArray = stringList.toArray(strArray);
}
public static void displayStrings(){
for (String s: strArray){
if (!(s == null)){
System.out.println(s);
}
}
}
public static void searchArray(){
Scanner input = new Scanner(System.in);
System.out.println("What string do you want to search the array for?");
String userInput;
userInput = input.nextLine();
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(strArray));
if (stringList.contains(userInput)){
System.out.println("The string array contains that string!");
}
else {
System.out.println("The string array does not contain that string...");
}
}
public static void sortArray(){
/*ArrayList<String> stringList = new ArrayList<String> (Arrays.asList(strArray));
Collections.sort(stringList);
strArray = stringList.toArray(strArray);*/
/*for (String s: strArray) {
if (!(s == null)){
Arrays.sort(strArray);
}
}*/
List<String> stringList = new ArrayList<String>(Arrays.asList(strArray));
Collections.sort(stringList);
strArray = stringList.toArray(strArray);
//Arrays.sort(strArray);
}
}
The reason you're getting NullPointerExceptions can be explained by the javadoc for Arrays#sort() (emphasis mine):
Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface.
Because Arrays.sort() expects Comparable elements and not null values, you end up with a NullPointerException when the method tries to call compareTo().
The fix-this-now way of solving this would be to simply make sure all null elements in your array are replaced with something non-null, such as "". So loop through your array at creation and after removing a String and set null elements to "". However, this solution probably wouldn't perform too well for your code, as it requires another loop after every String is removed, which could grow onerous. At least it won't require you to create a bunch of objects, due to the magic of the String pool, so it's a bit better than what you might do with a different object.
A better solution would be to simply use ArrayList<String> instead of a raw array; after all, you're already using one to manage addString() and removeString(), so you would have less converting from array to ArrayList and back to do. In addition, you wouldn't need to worry about NPEs when sorting (at least for your use case; adding null to a Collection would still result in NPEs when sorting).
You can also just use a raw array, but managing that would get kind of annoying, so I wouldn't recommend that. If you do it right you won't have to worry about NPEs though.
No problem! Here you go:
1. Create a new array
2. Insert items to that array, in the right order
public class sorter {
public static void main(String[] args){
String[] array = new String[]{"HI", "BYE", null, "SUP", ":)"};
//Sort:
String[] newArray = new String[array.length];
int index = 0;
for(int m = 0 ; m < newArray.length; m++){
String leastString = null;
int i = 0;
for(i = 0; i < array.length; i++){
if(leastString==null&&array[i]!=null){
leastString = array[i];
break;
}
}
for(int j = i+1; j < newArray.length; j++){
if(array[j]!=null){
if(array[j].compareTo(array[i])<0){
leastString = array[j];
i = j;
}
}
}
if(i==newArray.length)break;
newArray[m] = leastString;
array[i] = null;
}
for(String s : newArray){
System.out.println(s);
}
}
}
This prints:
:)
BYE
HI
SUP
null
EDIT: Another very simple way to solve this in a very effiecient manner, is to use ArrayList:
public class AClass {
public static void main(String[] args){
String[] array = new String[]{"HI", "BYE", null, "SUP", ":)"};
//Sort:
ArrayList<String> newArray = new ArrayList<String>();
for(String s : array){
if(s!=null){
newArray.add(s);
}
}
Collections.sort(newArray);
String[] retval = new String[newArray.size()];
retval = newArray.toArray(retval);
for(String s : retval){
System.out.println(s);
}
}
}
I guess the simple way of doing things really would be:
static String[] strArray;
static {
strArray = new String[5];
for(int i = 0, i < strArray.length; i++)
{
strArray[i] = "";
}
}
And then just call
Arrays.sort(strArray);
When you want to sort it. If that doesn't work, although I think it should; your initial approach would have been the following:
List<String> stringList = new ArrayList<String>();
for(int i = 0; i < strArray.length; i++)
{
stringList.add(strArray[i]);
}
Collections.sort(stringList);
strArray = stringList.toArray(new String[stringList.size()]);
Although it clearly doesn't seem very memory-friendly.
Related
import java.util.Scanner;
import java.util.ArrayList;
public class kek2 {
public static void main(String[] args) {
Scanner imeskanera = new Scanner(System.in);
ArrayList<String> wordlist = new ArrayList <String>();
while(true) {
System.out.println("Type a word: ");
String word = imeskanera.nextLine();
int lenght =wordlist.size();
if(word.equals("")) {
for(int i = 0;i<lenght; i++) {
System.out.println(wordlist.get(lenght-i));}
break;
}
wordlist.add(word);
}
}
}
Im trying to print out the array in reversed order but i get error in (lenght-i) part, everything looks fine to me, am'I doing something wrong that Java doesnt allow?
It may be better to limit while loop to reading the inputs and when user is done, print the list contents in any desired order.
Scanner imeskanera = new Scanner(System.in);
List<String> wordlist = new ArrayList<>();
String word;
System.out.println("Type a word: ");
while(!(word = imeskanera.nextLine()).isEmpty()) {
wordlist.add(word);
System.out.println("Type a word: ");
}
for (int i = wordlist.size(); i-- > 0;) { // index decremented in condition
System.out.println(wordlist.get(i));
}
Or ListIterator may be retrieved using List::listIterator and its methods hasPrevious() / previous() can be used to iterate in reverse direction -- however, the size of list is needed anyway:
for (ListIterator i = wordlist.listIterator(wordlist.size()); i.hasPrevious();) {
System.out.println(i.previous());
}
I want to store as many elements as desired by the user in an array. But how do I do it.
If I were to create an array, I must do so with a fixed size. Every time a new element is added to the array and the array becomes full, I want to update its size by '1'.
I tired various types of code, but it did not work out.
It would be of great help if someone could give me a solution regarding it - in code if possible.
Instead of using an array, use an implementation of java.util.List such as ArrayList. An ArrayList has an array backend which holds values in a list, but the array size is automatically handles by the list.
ArrayList<String> list = new ArrayList<String>();
list.add("some string");
You can also convert the list into an array using list.toArray(new String[list.size()]) and so forth for other element types.
On a low level you can do it this way:
long[] source = new long[1];
long[] copy = new long[source.length + 1];
System.arraycopy(source, 0, copy, 0, source.length);
source = copy;
Arrays.copyOf() is doing same thing.
You can create a temporary array with a size that is one element larger than the original, and then copy the elements of the original into the temp, and assign the temporary array to the new one.
public void increaseSize() {
String[] temp = new String[original.length + 1];
for (int i = 0; i < original.length; i++){
temp[i] = original[i];
}
original = temp;
}
You can change the size in various ways, but the same idea applies.
You can't. You can either create a new array and move the items to that array - Or you can use an ArrayList.
By using copyOf method in java.util.Arrays class String[] size will increment automatically / dynamically. In below code array size 0 after manipulation of using Arrays.copyOf the size of String array is increased to 4.
package com.google.service;
import java.util.Arrays;
public class StringArrayAutoIncrement {
public static void main(String[] args) {
String[] data = new String[] { "a", "b", "c", "d", "e" };
String[] array = new String[0];// Initializing array with zero
int incrementLength = 1;
for (int i = 0; i < data.length; i++) {
array = Arrays.copyOf(data, i + incrementLength);// incrementing array by +1
}
/**
* values of array after increment
*/
for (String value : array) {
System.out.println(value);
}
}
}
Output:
a
b
c
d
e
https://www.java2novice.com/java-arrays/array-copy/
int[] myArr = {2,4,2,4,5,6,3};
System.out.println("Array size before copy: "+myArr.length);
int[] newArr = Arrays.copyOf(myArr, 10);
System.out.println("New array size after copying: "+newArr.length);
You can also do myArr = Arrays.copyOf(myArr, 10);
In this case do, myArr = Arrays.copyOf(myArr, myAry.length+1);
u can use array list for that
here is example for array list of string
'import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Ex01 {
public static void main(String[] args) throws IOException {
BufferedReader userInput = new BufferedReader
(new InputStreamReader(System.in));
ArrayList<String> myArr = new ArrayList<String>();
myArr.add("Italian Riviera");
myArr.add("Jersey Shore");
myArr.add("Puerto Rico");
myArr.add("Los Cabos Corridor");
myArr.add("Lubmin");
myArr.add("Coney Island");
myArr.add("Karlovy Vary");
myArr.add("Bourbon-l'Archambault");
myArr.add("Walt Disney World Resort");
myArr.add("Barbados");
System.out.println("Stupid Vacation Resort Adviser");
System.out.println("Enter your name:");
String name = userInput.readLine();
Integer nameLength = name.length();
if (nameLength == 0)
{
System.out.println("empty name entered");
return;
}
Integer vacationIndex = nameLength % myArr.size();
System.out.println("\nYour name is "+name+", its length is " +
nameLength + " characters,\n" +
"that's why we suggest you to go to "
+ myArr.get(vacationIndex));
}
}'
similarly u can make array for integer,blooean and all kinds of data types
for more detail u can see this
http://www.anyexample.com/programming/java/java_arraylist_example.xml
Create list object, add the elements and convert that list object to array using list.toArray(new String[list.size()])
u can do it by this
import java.util.Scanner;
class Add
{
public static void main(String[] args)
{
Scanner obj=new Scanner(System.in);
char ans='y';
int count=0;
while(ans!='N'||ans!='n')
{
int initial_size=5;
int arr[]=new int [initial_size];
arr[0]=1;
arr[1]=2;
arr[2]=3;
arr[3]=4;
arr[4]=5;
if(count>0)
{
System.out.print("enter the element u want to add in array: ");
arr[initial_size-1]=obj.nextInt();
}
System.out.print("Do u want to add element in array?");
ans=obj.next().charAt(0);
if(ans=='y'||ans=='Y')
{
initial_size++;
count++;
}
}
}
}
public class IncreaseArraySize {
public static void main(String[] args) {
int arr[] = new int[5];
for (int i = 0; i < arr.length; i++) {
arr[i] = 5;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println(" ");
System.out.println("Before increasing Size of an array :" + arr.length);
int arr2[] = new int[10];
arr = arr2;
arr2 = null;
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
System.out.println("After increasing Size of an array : " + arr.length);
}
}
Use an ArrayList. The size it automatically increased if you try to add to a full ArrayList.
I am trying to write a program that would take an ArrayList containing "How", "Are" and "You?" and pass this to a 'stutter' method that will get the output to repeat the words after asking the user for number/how many times they want the word repeated.
Example: if user enters 4, i would pass How, Are and You? and 4 to this stutter method and the output would be How, How, How, How, Are, Are, Are, Are, You?, You?,You?,You?. This seems simple enough but i cannot get the output correct. Any help is appreciated!
import java.util.*;
public class Question3 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("how");
list.add("are");
list.add("you?");
Scanner input = new Scanner(System.in);
System.out.println("How many repeats?");
int repeat = input.nextInt();
System.out.println("Before stutter: " + list);
stutter(list, repeat);
}
public static void stutter(ArrayList<String> list, int repeat){
ArrayList<String> modifiedList = new ArrayList<String>();
for(String str : list) {
for(int i = 0; i < repeat; i++)
modifiedList.add(str);
System.out.print(modifiedList);
}
}
}
The print statement is running in the outer loop, so it prints modifiedList once for each str. Move it to the end of the method so it prints the final result once:
public static void stutter(ArrayList<String> list, int repeat){
ArrayList<String> modifiedList = new ArrayList<String>();
for(String str : list) {
for(int i = 0; i < repeat; i++)
modifiedList.add(str);
}
System.out.print(modifiedList);
}
I have to compare two String arrays with integer values separated by "#" input by the user as follows:
Input 1: Size of the array:
3
Input 2: (Array 1)
1#4#5
3#6#7
5#8#9
Input 2: (Array 2)
1#3#5
4#6#7
5#7#9
They contain the same no. of integer strings per line as specified by the user input array size. For eg: the 1st line of Array 1 = 1#4#5 = 3 integer strings.
In case, the array inputs are blank in any line, the output should be "invalid".
The output should be "yes" if the integer values in both the arrays are same irrespective of their position in the array i.e. if they are equivalent, otherwise the output should be "no".
My code passes very few test cases and mostly gives the correct output only when the two arrays are transpose of each other (when seen from the input format). It does not give the correct output when all the integer strings in both the arrays are same irrespective of their positions in the array.
eg. 1: Test case passed:
The output for the arrays in the example above is yes
eg.2: Test case failed:
Input 1: Size of the array:
2
Input 2: (Array 1)
1#6
3#4
Input 2: (Array 2)
6#3
4#1
Output: no
Here is my code:
import java.util.Scanner;
import java.util.Arrays;
public class StringComparison
{
public static void main (String[]args)
{
Scanner input = new Scanner (System.in);
// Input the array size
System.out.println("Enter the array size:");
int size = input.nextInt();
input.nextLine();
String[] s1 = new String[size];
String[] s2 = new String[size];
// Input 1st array elements
System.out.println("Enter the 1st array elements:");
for (int i=0;i<size; i++)
{
s1[i]= input.nextLine();
}
// Input 2nd array elements
System.out.println("Enter the 2nd array elements:");
for (int i=0;i<size; i++)
{
s2[i]= input.nextLine();
}
// Check for equivalence
System.out.println(equivalent (size, s1, s2));
}
public static String equivalent (int input1, String[]input2, String[]input3)
{
String result =null;
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
char []b1 = new char[input1*input1];
char[]b2 = new char[input1*input1];
int[] num1 = new int[input1*input1];
int[] num2 = new int[input1*input1];
for (int i=0; i<input1;i++)
{
String[] a1 = input2[i].split("#");
// if the user inputs are less or more than required
try
{
for (int j=0;j<input1;j++)
sb1.append (a1[j]);
}
catch (java.lang.ArrayIndexOutOfBoundsException e)
{
result ="invalid";
return result;
}
}
for (int i=0; i<input1;i++)
{
String[] a2 = input3[i].split("#");
// if the user inputs are less or more than required
try
{
for (int k=0;k<input1;k++)
sb2.append (a2[k]);
}
catch (java.lang.ArrayIndexOutOfBoundsException e)
{
result ="invalid";
return result;
}
}
// Storing the contents of the StringBuilder objects in a char array
sb1.getChars (0,((input1*input1)-1),b1,0);
sb2.getChars (0,((input1*input1)-1),b2,0);
// Converting the elements of the char array into integers and storing it in an int array
for (int p=0; p<((input1*input1)-1);p++)
{
num1[p] = Character.digit( b1[p],(input1*input1)-1);
}
// Converting the elements of the char array into integers and storing it in an int array
for (int q=0; q<((input1*input1)-1);q++)
{
num2[q] = Character.digit( b2[q],(input1*input1)-1);
}
// Sorting the two integer arrays
Arrays.sort (num1);
Arrays.sort (num2);
if (Arrays.equals (num1,num2))
{
result = "yes";
}
else
{
result ="no";
}
return result;
}
}
i have rewritten your equivalent method. i hope its okay to use ArrayList.
private static String equivalent(String[] s1, String[] s2) {
ArrayList<Integer> num1 = new ArrayList<Integer>();
ArrayList<Integer> num2 = new ArrayList<Integer>();
for (String str : s1) {
String[] storage = str.split("#");
for (String st : storage) {
num1.add(Integer.parseInt(String.valueOf(st)));
}
}
for (String str : s2) {
String[] storage = str.split("#");
for (String st : storage) {
num2.add(Integer.parseInt(String.valueOf(st)));
}
}
Collections.sort(num1);
Collections.sort(num2);
if (num1.equals(num2)) {
return "yes";
} else {
return "no";
}
}
this does what you want to achieve with fewer code. if you need help understand or have any other questions feel free to ask
The logic you are using to make the integer array is wrong and it produced invalid entries.Try printing the array values.All the test cases will fail.
Try below to convert the the char into the integer
num2[q] = Integer.parseInt(String.valueOf(b2[q]));
num1[p] = Integer.parseInt(String.valueOf(b1[p]));
Note:Example 1 should have been "NO". There is no 8 in the second input.
I am trying to create an array that reads string tokens from standard input, and places them in an array, and then prints the words out, until it reaches a specific word. For example, let's say I wanted my array to read a series of words until it reached the word "okay" from std in, print out each word, and then terminate before printing out "okay". The length of this array will be unknown, so I am confused on how to do this.
String s = sc.next();
String[] copy = new String[???];
for( int i = 0; i < copy.length; i++ ){
copy[i] = sc.next();
}
Something like:
String s = sc.next();
ArrayList<String> copy = new ArrayList<String>();
while(!s.equals("okay")){
copy.add(s);
s = sc.next();
}
for (String n : copy){
System.out.println(n);
}
If you don't want to use any list at all, then this becomes impossible. This is simply because array size needs to be defined at the time the array object is created.
So with this constraint you can have a large integer and declare an array of that size.
Final int MY_LARGE_CONST = 3000;
...
String[] tokens = new String[MY_LARGE_CONST]...
This is wasteful since it takes more memory and will fail if you have more tokens than the constant.
Alternaely if you are ok with lists and not ok with iterating over that for actual processing, then u can put the tokens in an ArrayList and once they are all collected, call the toArray method on the ArrayList object.
It's my code Without using ArrayList.
import java.util.Scanner;
import java.util.StringTokenizer;
public class Sample {
public static void main(String[] args) {
Scanner sc = new Scanner(System. in );
String line = sc.nextLine();
StringTokenizer st = new StringTokenizer(line);
int len = st.countTokens();
String[] array = new String[len];
for (int idx = 0; idx < len; idx++) {
array[idx] = st.nextToken();
}
for (String str: array) {
System.out.println(str);
}
}
}