0

I'm trying to read a CSV file and then shuffle the characters of the strings in the array. The CSV file contains a dictionary from english to german. This is how it looks like:

eat,essen

play,spiel

sleep,schlafen

the desired output should look something like this:

eat sesen

play lpsie

sleep fenlahsc

This is my code so far (I keep getting the error "non-static method shuffle(String) cannot be referenced from a static context"):

public class Shuffle { 
    public static void main(String[] args) {
        String path = "/Users/SaberKlai/Documents/vokabeln.csv";
        String line = "";

        try {
            BufferedReader br = new BufferedReader(new FileReader(path));

            Shuffle s = new Shuffle();

            while ((line = br.readLine()) != null) {
               

                String[] values = line.split(",");               
                System.out.println(values[0] + " " + shuffle(values[1]));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void shuffle(String input){
        List<Character> characters = new ArrayList<Character>();
        for(char c:input.toCharArray()){
            characters.add(c);
        }
        StringBuilder output = new StringBuilder(input.length());
        while(characters.size()!=0){
            int randPicker = (int)(Math.random()*characters.size());
            output.append(characters.remove(randPicker));
        }
        System.out.println(output.toString());
    }
}

               
Kay
  • 19
  • 5

3 Answers3

0

You can directly reference the current object using 'this' keyword. Instead of

System.out.println(values[0] + " " + shuffle(values[1]));

you can use:

System.out.println(values[0] + " " + this.shuffle(values[1]));

Also, just get rid of :

Shuffle s = new Shuffle();

On a side-note, you might want to read more on OOPS concept!

Ankit Shubham
  • 2,989
  • 2
  • 36
  • 61
0

The issue is that you are trying to call an instance method (shuffle()) from a static method (main()). You need to make shuffle() a static method.

In addition to this, you can't do System.out.println(values[0] + " " + shuffle(values[1])), because shuffle() method returns no String, in fact it returns nothing. Hence you also need to change this piece of code as follows:

public class Shuffle { 
    public static void main(String[] args) {
        String path = "/Users/SaberKlai/Documents/vokabeln.csv";
        String line = "";

        try {
            BufferedReader br = new BufferedReader(new FileReader(path));

            while ((line = br.readLine()) != null) {
                String[] values = line.split(",");
                System.out.print(values[0] + " ");
                shuffle(values[1]);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void shuffle(String input){
        List<Character> characters = new ArrayList<Character>();
        for(char c:input.toCharArray()){
            characters.add(c);
        }
        StringBuilder output = new StringBuilder(input.length());
        while(characters.size()!=0){
            int randPicker = (int)(Math.random()*characters.size());
            output.append(characters.remove(randPicker));
        }
        System.out.println(output.toString());
    }
}
João Dias
  • 16,277
  • 6
  • 33
  • 45
0

Your method shuffle is non-static. Just make it static and make it return String instead of void

public static String shuffle(String input) {
...
return output.toString();
}

or use reference to your object Shuffle s

System.out.println(values[0] + " " + s.shuffle(values[1]));
Igor Kiulian
  • 164
  • 8