Deathbomb
Deathbomb

Reputation: 13

How do I make a scanner for a char array?

Like how do I make it so that the user can input the values for a char array?

Here's my attempt at trying to make it, but got a syntax error:

    char [] bacteriaStrand = new char[9];
    String bacteria = bac.next();
    bacteriaStrand = bacteria;

Here's my entire code so far:

import java.util.Scanner;
public class SodiknBrukDNAAnalysispt3v1
{
    public static void main(String[] args)
    {
        Scanner bac = new Scanner(System.in);
        char [] treatmentStrand = {'T','C','G','A','G','A','G','T','A','T','C','C','C','A','G'};
        char [] bacteriaStrand = new char[9];
        char [] splicedStrand = new char[treatmentStrand.length + bacteriaStrand.length];
        System.out.println("NIH Treatment Splicer v. 1");
        System.out.println(" ");
        System.out.print("Treatment Strand: ");
        for (int t = 0; t < treatmentStrand.length; t++)
        {
            System.out.print(treatmentStrand[t]);
        }
        System.out.println(" ");
        System.out.print("Bacterium Strand: ");
        String bacteria = bac.next();
        bacteriaStrand = bacteria;
        for (int b = 0; b < bacteriaStrand.length; b++)
        {
            System.out.print(bacteriaStrand[b]);
            if (bacteriaStrand[b] != 'A' || bacteriaStrand[b] !='C' || bacteriaStrand[b] !='T' || bacteriaStrand[b] !='G')
            {
                System.out.println("Error! You can only input capital A, C, T, and G.");
            }
        }
    }
}

Upvotes: 1

Views: 8212

Answers (3)

sherl
sherl

Reputation: 21

You are assigning String to Char[] so it's type missmatch,either assign String to String Array or do bacteriaStrand=bacteria.toCharArray();to convert String bacteriaStrand to Char[] bacteria.or try

for(int i=0;i<bacteriaStrand.length;i++){
bacteriaStrand[i]=bac.next().charAt(0);
}

Upvotes: 1

Bojan Petkovic
Bojan Petkovic

Reputation: 2576

Here is an a solution that will work.

Using the suggestions from above (toCharArray). I have also added List of allowedCharacters for easier comparisons and ensuring the length is 9 before processing it.

You might also want to do to ignore cases.

bacteria.toUpperCase().toCharArray();

This will help

public class SodiknBrukDNAAnalysispt3v1 {
    public static void main(String[] args) {
        Scanner bac = new Scanner(System.in);
        char [] treatmentStrand = {'T','C','G','A','G','A','G','T','A','T','C','C','C','A','G'};
        char [] bacteriaStrand = new char[9];
        char [] splicedStrand = new char[treatmentStrand.length + bacteriaStrand.length];
        System.out.println("NIH Treatment Splicer v. 1");
        System.out.println(" ");
        System.out.print("Treatment Strand: ");
        for (int t = 0; t < treatmentStrand.length; t++)
        {
            System.out.print(treatmentStrand[t]);
        }
        System.out.println(" ");
        System.out.print("Bacterium Strand: ");
        String bacteria = bac.next();
        if (bacteria.length() != 9) {
            System.out.println("Error! You are expected to input 9 characters");
            return ;
        }
        bacteriaStrand = bacteria.toCharArray();
        List<Character> allowedCharacters = Arrays.asList('A', 'C', 'T', 'G');
        for (int b = 0; b < bacteriaStrand.length; b++) {
            System.out.print(bacteriaStrand[b]);
            if (!allowedCharacters.contains(bacteriaStrand[b])) {
                System.out.println("Error! You can only input capital A, C, T, and G.");
                return;
            }
        }
    }
}

Upvotes: 0

Nechemia Hoffmann
Nechemia Hoffmann

Reputation: 2507

If you want a char[] (primitives) you can use a method which returns a char[].

src:

public static char[] toCharArray(String input){
        StringTokenizer stringTokenizer = new StringTokenizer(input, " ");
        char[] chars = new char[stringTokenizer.countTokens()];

        for (int i = 0; i < chars.length; i++)
            chars[i] = stringTokenizer.nextToken().charAt(0);

        return chars;
    }

implementation:

char[] bacteriaStrand = toCharArray(bacteria);

- OR -

If your string is not delimited:

*edited*

That second example can be easily implemented by just calling String#toCharArray(), which returns a copy of the characters in the string. – Obicere

String string = "STRING";
char[] chars = string.toCharArray();

Thanks

Upvotes: 1

Related Questions