Simple Java question

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 470 Views - Last Post: 01 April 2012 - 02:15 PM Rate Topic: -----

#1 eagles94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-March 12

Simple Java question

Posted 01 April 2012 - 07:51 AM

I am making a spanish verb conjugator gui. Basically, you enter a spanish verb like "hablar" (to talk) and press the conjugate button. It will conjugate the verb in spanish present tense. The problem is that to conjugate into the tense
I need to drop the ar in habl(ar) and replace it with the letter o to get hablo. How do I chop off the last two letters?

This is what I tried to do. I placed hablar in an array called verbs and then made another array that had two less places then the verbs array. I then used a for loop to fill each position of the new array except for the last two characters since its length will exceed it. It still keeps out printing Hablar though!

Is This A Good Question/Topic? 0
  • +

Replies To: Simple Java question

#2 teQuiero  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 31-March 12

Re: Simple Java question

Posted 01 April 2012 - 08:09 AM

i think its more better if you create an a list of pastTense and presentTense then compare it to the text. rather than droping or removing txt.
Was This Post Helpful? 0
  • +
  • -

#3 eagles94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-March 12

Re: Simple Java question

Posted 01 April 2012 - 08:15 AM

so what your saying is to basically create another array of already filled verbs with tense changes and just print that out. If so that was my original plan but wouldnt chopping off the last two letters and just addin a letter be faster and less lines?? Or is it actually complex to do that?
Was This Post Helpful? 0
  • +
  • -

#4 Mylo  Icon User is offline

  • D.I.C Addict

Reputation: 165
  • View blog
  • Posts: 587
  • Joined: 11-October 11

Re: Simple Java question

Posted 01 April 2012 - 08:24 AM

You could do something like String myWord = "Hablar"; System.out.println(myWord.substring(0, myWord.length() - 2) + 'o');

I'm not familiar with Spanish, but I assume the same replace 'ar' with 'o' will not work every time. If that is the case, I would probably have a map of them as teQuiero suggested.

Also, show us your code for your attempt, we will be able to point out what you did incorrectly.
Was This Post Helpful? 0
  • +
  • -

#5 eagles94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-March 12

Re: Simple Java question

Posted 01 April 2012 - 08:31 AM

alright let me try that and if it doesnt work at all, I'll post the code. thanks.
Was This Post Helpful? 0
  • +
  • -

#6 eagles94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-March 12

Re: Simple Java question

Posted 01 April 2012 - 08:52 AM

Okay now im lost. I keep getting nullpointerexception error. Here is the code.

import javax.swing. *;
import java.awt.*;
import java.awt.event.*;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;


public class Conjugator extends JPanel
{
String[] regularAR = new String[5];
String[] regularER = new String[5];
String s, myWord;
private JLabel title, output;
private JTextField input;
private int i;
public Conjugator()
{
	
	
	regularAR[0] = "comprar";
	regularAR[1] = "bailar";
	regularAR[2] = "cantar";
	regularAR[3] = "hablar";
	regularAR[4] = "trabajar";
	
	regularER[0] = "beber";
	regularER[1] = "correr";
	regularER[2] = "romper";
	regularER[3] = "aprender";
	regularER[4] = "deber";
	
	setLayout(new BorderLayout());


   JPanel titlepanel = new JPanel();
	titlepanel.setLayout(new FlowLayout());
	add(titlepanel, BorderLayout.NORTH);
		
	JPanel ippanel = new JPanel();
	ippanel.setLayout(new FlowLayout());
	add(ippanel, BorderLayout.CENTER);
	
	JPanel dppanel = new JPanel();
	dppanel.setLayout(new FlowLayout());
	add(dppanel, BorderLayout.SOUTH);

	
	title = new JLabel("Spanish Verb Conjugator");
	title.setFont(new Font("Serif", Font.BOLD, 20));
	titlepanel.add(title, BorderLayout.CENTER);
	
	input = new JTextField("Enter common Spanish Verb", 20);
	input.setHorizontalAlignment(SwingConstants.LEFT);
	ippanel.add(input);
	
	output = new JLabel("");
	dppanel.add(output);
	
}

public void update()
{
	 s = input.getText();
	
	for(int i = 0; i < regularAR.length; i++);{
	if(s.equals(regularAR[i])) {
    myWord = s;
	} }
	String j = m.substring(0,myWord.length()-2);
        String one = "o";
	output.setText("Yo: " +j+one);
	 
		
	
    }

  }
 


Was This Post Helpful? 0
  • +
  • -

#7 Ryano121  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1055
  • View blog
  • Posts: 2,235
  • Joined: 30-January 11

Re: Simple Java question

Posted 01 April 2012 - 09:37 AM

A NullPointerException on which line?
Was This Post Helpful? 0
  • +
  • -

#8 eagles94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-March 12

Re: Simple Java question

Posted 01 April 2012 - 09:55 AM

on line 70 with the string j = substring blah blah
Was This Post Helpful? 0
  • +
  • -

#9 Ryano121  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1055
  • View blog
  • Posts: 2,235
  • Joined: 30-January 11

Re: Simple Java question

Posted 01 April 2012 - 10:03 AM

m.substring(0,myWord.length()-2);

where is m defined?
Was This Post Helpful? 0
  • +
  • -

#10 eagles94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-March 12

Re: Simple Java question

Posted 01 April 2012 - 10:27 AM

oops. Its supposed to be myWord.substring

but even then it still shows up as a null pointer exception
Was This Post Helpful? 0
  • +
  • -

#11 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Simple Java question

Posted 01 April 2012 - 10:35 AM

post the exception
Was This Post Helpful? 0
  • +
  • -

#12 Ryano121  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1055
  • View blog
  • Posts: 2,235
  • Joined: 30-January 11

Re: Simple Java question

Posted 01 April 2012 - 10:36 AM

Best thing you can do is set a breakpoint on line 70. Then watch the value of 's' and 'myWord'. You will probably see that s is set to null. Therefore pointing to a problem with the input.
Was This Post Helpful? 0
  • +
  • -

#13 eagles94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-March 12

Re: Simple Java question

Posted 01 April 2012 - 10:39 AM

here is the exception

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Conjugator.update(Conjugator.java:70)
at StudyTool$presentListener.actionPerformed(StudyTool.java:46)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6288)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6053)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4651)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.window.dispatchEventImpl(window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Was This Post Helpful? 0
  • +
  • -

#14 teQuiero  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 31-March 12

Re: Simple Java question

Posted 01 April 2012 - 11:50 AM

seems fine to me when i tried compiling after fixing
String j = myWord.substring(0,myWord.length()-2);

Was This Post Helpful? 0
  • +
  • -

#15 GetSet  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 08-February 11

Re: Simple Java question

Posted 01 April 2012 - 01:26 PM

View Posteagles94, on 01 April 2012 - 10:27 AM, said:

oops. Its supposed to be myWord.substring

but even then it still shows up as a null pointer exception



If you haven't, you need to instantiate your strings.

That is,

String j = new String();



Then,

j = substring yada yada;

later in the code.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2