0

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]);
        }
    }
}
Akshay Soam
  • 1,580
  • 3
  • 21
  • 39
  • 1
    String comparison in Java is done using `equals()`. Variables should begin with a lower case letter, so you would want to write `workScheduleSplit[x].equals("A")` and so forth. Also, there is no point in allocating an array of size 8 since `split()` is just going to immediately replace it with the result of splitting the string. – David Conrad Jan 13 '15 at 05:37

2 Answers2

0

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.

Community
  • 1
  • 1
Isuru Gunawardana
  • 2,847
  • 6
  • 28
  • 60
0

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++;
}}
Weregoat
  • 36
  • 4