Jump to content

Java Program Help Needed


Emp75

Recommended Posts

I'm working on a program assignment for my Introduction to Java class and I need help a few things.

 

Is there anyway to split a String by characters and numbers? For example, the assignment has to do with molecular formulas and for Prozac the formula is C17.H19.F3.N10

 

I have split the formula by each of their atoms and how many of them using Stringtokenizer but I want to split each token (C17 H19 F3 N10) by their atoms and how many of them, so right now I have the string "C17" and I want to split that so I have atom = C and number = 17

 

For this example each atom is only one letter but others might have an atom with more than one letter so I can't just use substring.

 

I also have to create a PeriodicTable data type and I'm getting the NullPointerException and I am unsure as to why.

Here's my code:

 

import java.util.*;
import java.io.*;

public class PeriodicTable
{
private static String[] elements;

public PeriodicTable() throws IOException
{
	elements = new String[103];
	Scanner filein = new Scanner(new File("elements.csv"));

	int k=0;

	while(filein.hasNextLine())
	{
		elements[k] = filein.nextLine();
		k++;
	}

}

public static void display(String[] ar)
{
	for(String item: ar)
	{
		System.out.println(item);
	}

}

public static void main(String[] args)
{
	display(elements);
}
}

 

Any advice would be appreciated.

Link to comment
Share on other sites

Well, for your first issue you could make use of the charAt(index) method and substring method of the String class, and the isDigit(char) method of the Character class to work together a set of logic to do the seperation of the stings (I wont actually write the code for you as it is a homework assignment)

 

Will take me a bit longer to see if I can find where your other issue is

 

Edit: as far as I can tell, the only way I can think of to get a null pointer exception is if you are running the main method before the constructor or feeding in a null array to the display command (Comment your code, as I cannot actually tell what on earth the display method is trying to do, as I would think that you would be using the class's periodic table output rather than having one passed to it). Cant provide more help really without seeing the code that interacts with the method, and having the code be commented.

Luna_pirate_signature.png

Thanks to DrCue at DeviantArt for the signature source

Link to comment
Share on other sites

We haven't learned what the isDigit method is, could you perhaps go into more detail as to how to use it?

 

Well all I'm trying to do is create the Periodic Table data type which is just an array of all of the elements in the periodic table. The elements are stored on excel with the name, atomic number, atomic weight, symbol. In the constructor I'm just trying to read in the file and enter the data into the array and the display method to just print out the information in the array.

 

The first 6 in the array would look like this.

 

Hydrogen,1,H,1.01

 

Helium,2,He,4

 

Lithium,3,Li,6.94

 

Beryllium,4,Be,9.01

 

Boron,5,B,10.81

 

Carbon,6,C,12.01

Link to comment
Share on other sites

We haven't learned what the isDigit method is, could you perhaps go into more detail as to how to use it?

 

Well all I'm trying to do is create the Periodic Table data type which is just an array of all of the elements in the periodic table. The elements are stored on excel with the name, atomic number, atomic weight, symbol. In the constructor I'm just trying to read in the file and enter the data into the array and the display method to just print out the information in the array.

 

The first 6 in the array would look like this.

 

Hydrogen,1,H,1.01

 

Helium,2,He,4

 

Lithium,3,Li,6.94

 

Beryllium,4,Be,9.01

 

Boron,5,B,10.81

 

Carbon,6,C,12.01

 

To put it simply: isDigit(char ch) returns true if the character is a digit (0-9 or an assortment of other things you shouldn't see in testing) and false if it is anything else.

 

Okay, so the Display method is supposed to just output from the array elements since there is no real reason to not have it just access the elements array itself rather than having the main method do it.

 

would you by any chance be happening to get the null pointer exception trying to run just that class, rather than having another class using it?

Luna_pirate_signature.png

Thanks to DrCue at DeviantArt for the signature source

Link to comment
Share on other sites

Just using that class, I'll see if I can get the isDigit method working.

 

Well, that would be your problem. Try replacing the line:

private static String[] elements;

With this :

private static String[] elements = {"Honesty","Kindness","Laughter","Generosity","Loyalty","Magic"};                //Remember to initialize this array to something before trying to access it. Princess Luna is the best Pony. Honestly do not use this code in the final version of this program, from wyvren2000

Luna_pirate_signature.png

Thanks to DrCue at DeviantArt for the signature source

Link to comment
Share on other sites

Just using that class, I'll see if I can get the isDigit method working.

 

Well, that would be your problem. Try replacing the line:

private static String[] elements;

With this :

private static String[] elements = {"Honesty","Kindness","Laughter","Generosity","Loyalty","Magic"};                //Remember to initialize this array to something before trying to access it. Princess Luna is the best Pony. Honestly do not use this code in the final version of this program, from wyvren2000

 

The array is 103 inputs and they are supposed to be read from a file that I have. The file is an excel chart with every element in the periodic table that looks like this:

 

| Name | Atomic Number | Atomic Weight | Symbol |

1| | | | |

2| | | | |

3| | | | |

4| | | | |

 

etc. all the way down to 103.

 

I have another question (yeah, I'm pretty bad at this)

 

I am trying to set up a method that is used like this:

 

mol.displayAtomicMakeUp();

 

I have initialized mol in the main and I'm trying to get the displayAtomicMakeUp() method to read in what mol actually is. It might sound a bit strange, but I'm not sure how to word it.

 

If it was displayAtomicMakeUp(mol) it would make more sense to me, as I know how to actually take what mol (a string) is from that but what do I do for mol.displayAtomicMakeUp()?

Link to comment
Share on other sites

Just using that class, I'll see if I can get the isDigit method working.

 

Well, that would be your problem. Try replacing the line:

private static String[] elements;

With this :

private static String[] elements = {"Honesty","Kindness","Laughter","Generosity","Loyalty","Magic"};                //Remember to initialize this array to something before trying to access it. Princess Luna is the best Pony. Honestly do not use this code in the final version of this program, from wyvren2000

 

The array is 103 inputs and they are supposed to be read from a file that I have. The file is an excel chart with every element in the periodic table that looks like this:

 

| Name | Atomic Number | Atomic Weight | Symbol |

1| | | | |

2| | | | |

3| | | | |

4| | | | |

 

etc. all the way down to 103.

 

I have another question (yeah, I'm pretty bad at this)

 

I am trying to set up a method that is used like this:

 

mol.displayAtomicMakeUp();

 

I have initialized mol in the main and I'm trying to get the displayAtomicMakeUp() method to read in what mol actually is. It might sound a bit strange, but I'm not sure how to word it.

 

If it was displayAtomicMakeUp(mol) it would make more sense to me, as I know how to actually take what mol (a string) is from that but what do I do for mol.displayAtomicMakeUp()?

mol.displayAtomicMakeUp() should be defined in mol, or whatever type of class mol is. Sounds like its similar to a toString() method. You can write the method the same way as if you had it in a different class, except you can access all its elements without having to use get() methods.

 

Hope that makes a bit more sense.

99 dungeoneering achieved, thanks to everyone that celebrated with me!

 

♪♪ Don't interrupt me as I struggle to complete this thought
Have some respect for someone more forgetful than yourself ♪♪

♪♪ And I'm not done
And I won't be till my head falls off ♪♪

Link to comment
Share on other sites

The array is 103 inputs and they are supposed to be read from a file that I have. The file is an excel chart with every element in the periodic table that looks like this:

 

| Name | Atomic Number | Atomic Weight | Symbol |

1| | | | |

2| | | | |

3| | | | |

4| | | | |

 

etc. all the way down to 103.

 

I have another question (yeah, I'm pretty bad at this)

 

I am trying to set up a method that is used like this:

 

mol.displayAtomicMakeUp();

 

I have initialized mol in the main and I'm trying to get the displayAtomicMakeUp() method to read in what mol actually is. It might sound a bit strange, but I'm not sure how to word it.

 

If it was displayAtomicMakeUp(mol) it would make more sense to me, as I know how to actually take what mol (a string) is from that but what do I do for mol.displayAtomicMakeUp()?

I know about the entire thing of the array being intended to store the Periodic table, I only provided that sample code to point out that you were forgetting to initialize the array (I even mentioned it in the comment) since the constructor never runs if you run that class.

 

The specific code would be very helpful to tell you what is going on, but I will tell you what I can:

If mol is just a string variable, and the displayAtomicMakeUp() method is within the class calling the method, you only need to do displayAtomicMakeup(mol)

If mol is an object of the Class "X", and the displayAtomicMakeUP() method is within class "X" then you simply need to use mol.displayAtomicMakeup()

if mol is a string variable in this class, and displayAtomicMakeUp() is in class "X" which you have made an object out of named "object" you need to use object.displayAtomicMakeup(mol)

if displayAtomicMakeUp() is in this class, and mol is a string in the object "object" which is of type "X", you need to use displayAtomicMakeUp(object.getMol()) assuming you have an accessor method named getMol() in class "X"

Luna_pirate_signature.png

Thanks to DrCue at DeviantArt for the signature source

Link to comment
Share on other sites

public static void main(String[] args)
       {
           	try {
	    new PeriodicTable();
	    display(elements);
	} catch (IOException e) {

	    e.printStackTrace();
	}

       }

 

You never create an instance of the PeriodicTable class. This works fine for me.

 

You could also create an Element class:

 


public class Element {

   private String name;
   private int number;
   private double weight;
   private String symbol;

   public Element(String name, int number, String symbol, double weight) {
this.name = name;
this.number = number;
this.weight = weight;
this.symbol = symbol;
   }

   public String getName() {
       return this.name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public int getNumber() {
       return this.number;
   }

   public void setNumber(int number) {
       this.number = number;
   }

   public double getWeight() {
       return this.weight;
   }

   public void setWeight(double weight) {
       this.weight = weight;
   }

   public String getSymbol() {
       return this.symbol;
   }

   public void setSymbol(String symbol) {
       this.symbol = symbol;
   }

   @Override
   public String toString() {
return "Element [name=" + this.name + ", number=" + this.number
	+ ", weight=" + this.weight + ", symbol=" + this.symbol + "]";
   }






}

 

 

From there you can loop through the lines, split each section into parts then add a new element into a list.

 


//at top of code
import java.util.ArrayList;
static ArrayList<Element> myElementList = new ArrayList<Element>();


//when reading the file
while (filein.hasNextLine()) {
    item = filein.nextLine();
    String data[] = item.split(",");

    myElementList.add(new Element(data[0], Integer.parseInt(data[1]),
	    data[2], Double.parseDouble(data[3])));

}



//new display method
public static void display() {

for (int i = 0; i < myElementList.size(); i++) 
    System.out.println(myElementList.get(i).toString());


}


//results
Element [name=Hydrogen, number=1, weight=1.01, symbol=H]
Element [name=Helium, number=2, weight=4.0, symbol=He]
Element [name=Lithium, number=3, weight=6.94, symbol=Li]
Element [name=Beryllium, number=4, weight=9.01, symbol=Be]
Element [name=Boron, number=5, weight=10.81, symbol=B]
Element [name=Carbon, number=6, weight=12.01, symbol=C]

 

 

 

Once the elements are in a list, you can sort, print, add, remove, and do what you please. There might be a typo since I typed that from my ipad.

wii_wheaton.png

[software Engineer] -

[Ability Bar Suggestion] - [Gaming Enthusiast]

Link to comment
Share on other sites

Well, that would be your problem. Try replacing the line:

private static String[] elements;

With this :

private static String[] elements = {"Honesty","Kindness","Laughter","Generosity","Loyalty","Magic"};                //Remember to initialize this array to something before trying to access it. Princess Luna is the best Pony. Honestly do not use this code in the final version of this program, from wyvren2000

 

I lol'd :thumbsup:

 

but as was answered, you don't initialize your elements array, meaning it is a null pointer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.