Results 1 to 4 of 4

Thread: strings/arrays

  1. #1
    timmyglynnv3 is offline Member
    Join Date
    Apr 2017
    Posts
    1
    Rep Power
    0

    Default strings/arrays

    I want to create an input file with people first/last names and an amount of income in certain families. there are multiple families (murphy, johnson, hansen, etc.) and i need to compare the last names to group them under the same family and then calculate what their taxes would be for each family. how do i compare these?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: strings/arrays

    Comparing Strings is done with the equals() method of String. so "a".equals("a") would be true or myName.equals(yourName) where myName and yourName are String instances. But how you organize the information and process it is up to you, the designer.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Nihar is offline Member
    Join Date
    May 2017
    Posts
    3
    Rep Power
    0

    Default Re: strings/arrays

    String Array Declaration

    Square brackets is used to declare a String array. There are two ways of using it. The first one is to put square brackets after the String reserved word. For example:
    String[] thisIsAStringArray; Another way of declaring a String Array is to put the square brackers after the name of the variable. For example:
    String thisIsAStringArray[]; Both statements will declare the variable "thisIsAStringArray" to be a String Array. Note that this is just a declaration, the variable "thisIsAStringArray" will have the value null. And since there is only one square brackets, this means that the variable is only a one-dimensional String Array. Examples will be shown later on how to declare multi-dimensional String Arrays.
    String Array Declaration With Initial Size
    Learn Java here: https://hackr.io/tutorials/learn-java
    Arrays are usually used when we know how many objects are needed. Hence, arrays are usually declared with an initial size. Here is an example:
    String[] thisIsAStringArray = new String[5]; This will declare a String Array with 5 elements. Each element can be accessed using index that starts with 0. The fist element is "thisIsAStringArray[0]", the second element is "thisIsAStringArray[1]", and so on. Here is an example of declaring a String Array with size 5 and assigning values to each element:
    String[] thisIsAStringArray = new String[5];
    thisIsAStringArray[0] = "AAA";
    thisIsAStringArray[1] = "BBB";
    thisIsAStringArray[2] = "CCC";
    thisIsAStringArray[3] = "DDD";
    thisIsAStringArray[4] = "EEE"; Note, since there are only 5 elements and index started from 0, the last index will be 4. Or we could use the formula ( array length - 1). This means if we access the index greater than 4, an Exception will be raised. Example:
    String[] thisIsAStringArray = new String[5];
    thisIsAStringArray[5] = "FFF";
    The code will throw a java.lang.ArrayIndexOutOfBoundsException.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: strings/arrays

    The OP didn't really ask anything about arrays (as far as I could tell). Anyway, if you are going to create an array of known strings, then do it
    like this.

    Java Code:
    String[] thisIsAStringArray = {"AAA","BBB","CCC","DDD","EEE"};
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Problem with strings and arrays
    By Intrebute in forum New To Java
    Replies: 2
    Last Post: 11-29-2011, 01:17 AM
  2. strings into arrays
    By minusten in forum New To Java
    Replies: 2
    Last Post: 04-27-2011, 05:56 AM
  3. byte arrays and strings
    By 7wb in forum New To Java
    Replies: 1
    Last Post: 11-14-2010, 09:51 PM
  4. using random strings from arrays insult generator
    By carden2 in forum New To Java
    Replies: 5
    Last Post: 04-05-2010, 05:51 AM
  5. arrays strings and doubles
    By rgvbabe in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 11:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •