简体   繁体   中英

trying to separate strings from an array with strings and ints (current program included)

This is my current program for taking input and giving the user a total pay over two days. I am currently trying to separate strings from a string array (Made after .split(", ")) and put those strings into its own array to process. I am also doing this same process with integers but so far I can't get the string separation to work properly. any help will be appreciated.*note, I am a somewhat beginner with this and only took one class so far so please keep it simple.

import java.util.Scanner;

public class AmusementPark
{
    public static void main(String[] args)
    {
        Scanner Reader=new Scanner(System.in);
        int [] WorkScheduleInts;
        String [] WorkScheduleStrings=new String[8];
        System.out.println("Please enter the work schedule as follows:");
        System.out.println("125, 2, 1, 7, 125, 3, 5, H");
        System.out.println("Enter Your work schedule:");
        String WorkScheduleinput=Reader.nextLine();
        String [] WorkScheduleSplit=new String[8];
        WorkScheduleSplit=WorkScheduleinput.split(", ");

        for(int x=0; x<WorkScheduleSplit.length;x++)
        {
            if(WorkScheduleSplit[x]=="A" || WorkScheduleSplit[x]=="B" || WorkScheduleSplit[x]=="C" || WorkScheduleSplit[x]=="D" || WorkScheduleSplit[x]=="E" || WorkScheduleSplit[x]=="F" || WorkScheduleSplit[x]=="G" || WorkScheduleSplit[x]=="H")
            {
                WorkScheduleStrings[x]=WorkScheduleSplit[x];
            }

            System.out.println(WorkScheduleStrings[x]);
        }
    }
}

Use following way to compare strings inside the if statement,

WorkScheduleSplit[x].equals("H")

Instead of == use String.equals

Read this link to understand the difference between String.equals and == operations.

Edit: Saw the comment on variables starting with a lower case and realized i missed that. Woops.

The problem that I see is that you put the H into the last place of workScheduleStrings. The first 7 places of workScheduleStrings won't get initialized but the last place will end up holding an H. What you need is another integer variable to count how many items have been placed into workScheduleStrings. Start it at 0 and change

workScheduleStrings[x] = workScheduleSplit[x];

to

workScheduleStrings[count] = workScheduleSplit[x];

After that you should increment count by one. Also you should check for equivalence with

workScheduleSplit[x].equals["H"];

You also have some body (brace) placement problems it looks like. I think both statements under the if statement should execute if the if statement evaluates true. Right now if will do the assignment if true but will always try to output. This could be a problem if you try to output uninitialized memory. If you do this it will only output an H but I assume you intend to take care of the integer parts of workSheduleSplit later. Really I think that should happen first. Anyway, The loop now looks like:

int count = 0;
for(int x=0; x<workScheduleSplit.length;x++){
//I leave out the comparisons of A-G because I'm lazy. You have that part if you use the equals method though.
if( workScheduleSplit[x].equals("H"){
    workScheduleStrings[count]=workScheduleSplit[x];
    System.out.println(workScheduleStrings[count]);
    count++;
}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM