3 Replies - 1117 Views - Last Post: 18 April 2013 - 05:45 PM Rate Topic: -----

#1 DeadTheory23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 16-September 12

The method is undefined for type string?

Posted 18 April 2013 - 03:47 PM

The program I'm working on is giving me the error "The method setByteToThisString() is undefined for the type String" I don't see how I am getting that error (line 217). It appears to me as though it should be correct. Any insight is greatly appreciated!
PS. I know the program isnt complete yet, this is just a temporary hang up for me.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics2D;

public class U28
{
	public static void main(String[] args)
	{
		MiscFocusFrame myframe = new MiscFocusFrame();
		myframe.setVisible(true);
	}
}

class MiscFocusFrame extends JFrame
{
	private JTextField myField;
	private MiscFocusPanel myPanel;
	private final int FRAMEW = 900;
	private final int FRAMEH = 200;

	public MiscFocusFrame()
	{
		setTitle("U28");
		setSize(FRAMEW, FRAMEH);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		myPanel = new MiscFocusPanel();
		Container contentPane = getContentPane();
		contentPane.add(myPanel, "Center");
	}
}

class MiscFocusPanel extends JPanel
{
	private MiscFocusRegister register1;
	private MiscFocusRegister register2;
	private MiscFocusRegister register3;
	private MiscFocusRegister register4;

	public MiscFocusPanel()
	{
		register1 = new MiscFocusRegister("4");
		register2 = new MiscFocusRegister("4");
		register3 = new MiscFocusRegister("4");
		register4 = new MiscFocusRegister("4");

		register1.setRegisterToPassFocusTo(register2);
		register2.setRegisterToPassFocusTo(register3);
		register3.setRegisterToPassFocusTo(register4);
		register4.setRegisterToPassFocusTo(register1);

		setLayout(new GridLayout(2, 6));

		JLabel labelA = new JLabel("Cup 1", JLabel.LEFT);
		JPanel labelPanelA = new JPanel();
		labelPanelA.add(labelA);
		add(labelPanelA);

		JPanel regPanelA = new JPanel();
		regPanelA.add(register1.getMyField());
		add(regPanelA);
		
		JButton myButton1 = new JButton("Add To Next Cup");
		MiscButtonListener myButtonListener1 = new MiscButtonListener();
		myButton1.addActionListener(myButtonListener1);
		JPanel buttonPanel1 = new JPanel();
		buttonPanel1.add(myButton1);
		add(buttonPanel1);

		JLabel labelB = new JLabel("Cup 2", JLabel.LEFT);
		JPanel labelPanelB = new JPanel();
		labelPanelB.add(labelB);
		add(labelPanelB);

		JPanel regPanelB = new JPanel();
		regPanelB.add(register2.getMyField());
		add(regPanelB);
		
		JButton myButton2 = new JButton("Add To Next Cup");
		MiscButtonListener myButtonListener2 = new MiscButtonListener();
		myButton2.addActionListener(myButtonListener2);
		JPanel buttonPanel2 = new JPanel();
		buttonPanel2.add(myButton2);
		add(buttonPanel2);
		
		JLabel labelC = new JLabel("Cup 3", JLabel.LEFT);
		JPanel labelPanelC = new JPanel();
		labelPanelC.add(labelC);
		add(labelPanelC);

		JPanel regPanelC = new JPanel();
		regPanelC.add(register3.getMyField());
		add(regPanelC);
		
		JButton myButton3 = new JButton("Add To Next Cup");
		MiscButtonListener myButtonListener3 = new MiscButtonListener();
		myButton3.addActionListener(myButtonListener3);
		JPanel buttonPanel3 = new JPanel();
		buttonPanel3.add(myButton3);
		add(buttonPanel3);
		
		JLabel labelD = new JLabel("Cup 4", JLabel.LEFT);
		JPanel labelPanelD = new JPanel();
		labelPanelD.add(labelD);
		add(labelPanelD);

		JPanel regPanelD = new JPanel();
		regPanelD.add(register4.getMyField());
		add(regPanelD);
		
		JButton myButton4 = new JButton("Add To Next Cup");
		MiscButtonListener myButtonListener4 = new MiscButtonListener();
		myButton4.addActionListener(myButtonListener4);
		JPanel buttonPanel4 = new JPanel();
		buttonPanel4.add(myButton4);
		add(buttonPanel4);

	}

	public void paintComponent(Graphics g)
	{
		Graphics2D g2 = (Graphics2D) g;
		super.paintComponent(g2);
	}

	private class MiscButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			register1.swapRegisterContents(register2);
		}
	}
}


/*
This class contains a new method, setRegisterToPassFocusTo(), and in
the listener focus is passed to that register after the new value for
the text field is taken in.
*/

class MiscFocusRegister
{
	private MiscFocusByte registerValue;
	private JTextField myField;
	private MiscFocusRegister registerToPassFocusTo;

	public MiscFocusRegister()
	{
	}

	public MiscFocusRegister(String stringIn)
	{
		registerValue = new MiscFocusByte(stringIn);
		myField = new JTextField(stringIn, 8);
		TextFieldListener myListener = new TextFieldListener();
		myField.addActionListener(myListener);
	}

	public void setRegisterValue(MiscFocusByte byteIn)
	{
		registerValue = byteIn;
	}

	public MiscFocusByte getRegisterValue()
	{
		return registerValue;
	}

	public void setMyField(JTextField fieldIn)
	{
		myField = fieldIn;
	}

	public JTextField getMyField()
	{
		return myField;
	}

	public void setRegisterToPassFocusTo(MiscFocusRegister registerIn)
	{
		registerToPassFocusTo = registerIn;
	}

	public void swapRegisterContents(MiscFocusRegister source)
	{
		MiscFocusByte tempValue;

		tempValue = source.getRegisterValue();
		source.setRegisterValue(this.getRegisterValue());
		source.myField.setText(source.getRegisterValue().getStringFromByte());
		this.setRegisterValue(tempValue);
		this.myField.setText(this.getRegisterValue().getStringFromByte());
	}

	private class TextFieldListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			String inputString = myField.getText();
			registerValue.setByteToThisString(inputString);
			registerToPassFocusTo.getMyField().requestFocusInWindow();
		}
	}
}

class MiscFocusByte
{
	public static final int bitsinbyte = 8;
	private static final String junk = "00000000";
	private char bytearray[] = new char[bitsinbyte];

	public MiscFocusByte()
	{
		//junk.getChars(0, bitsinbyte, bytearray, 0);
		junk.setByteToThisString();
	}

	public MiscFocusByte(String stringIn)
	{
		junk.getChars(0, bitsinbyte, bytearray, 0);
		//int stringlength = stringIn.length();
		//if(stringlength > bitsinbyte)
		  //stringlength = bitsinbyte;
		//stringIn.getChars(0, stringlength, bytearray, 0);
		//int i = java.lang.Integer.parseInt(stringIn) + 4;
		//stringIn = Integer.toString(i);
	}

	public String setByteToThisString(String astring)
	{
		junk.getChars(0, bitsinbyte, bytearray, 0);
		//int stringlength = astring.length();
		//if(stringlength > bitsinbyte)
		  //stringlength = bitsinbyte;
		//astring.getChars(0, stringlength, bytearray, 0);
		int i = java.lang.Integer.parseInt(astring) + 4;
		return astring = Integer.toString(i);
	}

	public String getStringFromByte()
	{
		return String.copyValueOf(bytearray);
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: The method is undefined for type string?

#2 Maynia  Icon User is online

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 47
  • Joined: 17-March 09

Re: The method is undefined for type string?

Posted 18 April 2013 - 03:54 PM

You're trying to call the method setByteToThisString() on a String variable, that you've named 'junk'. The compiler is telling you exactly what you need to know here - there is no method defined on Strings by that name. Perhaps you meant to call it on a different variable? I will admit I'm having difficulty seeing what this code is actually doing overall.

This post has been edited by Maynia: 18 April 2013 - 03:56 PM

Was This Post Helpful? 0
  • +
  • -

#3 DeadTheory23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 16-September 12

Re: The method is undefined for type string?

Posted 18 April 2013 - 04:02 PM

View PostMaynia, on 18 April 2013 - 03:54 PM, said:

You're trying to call the method setByteToThisString() on a String variable, that you've named 'junk'. The compiler is telling you exactly what you need to know here - there is no method defined on Strings by that name. Perhaps you meant to call it on a different variable? I will admit I'm having difficulty seeing what this code is actually doing overall.


Thank you for the reply, and My apologies, I know the code is incomplete, but in short its an assignment for my java class. Our prof gave us his code for one program and told us to edit it to make it do the following: Use a grid layout to display the labels, jtextfields, and buttons, and make it so that when the button is pressed by a jtextfield it takes the number in it, adds it to the number in the next box, and display it there. Its an exercise with focus, grid layouts, and a little extra. I've got the first 2 parts down but am drawing a blank for the final part :(
Was This Post Helpful? 0
  • +
  • -

#4 darek9576  Icon User is online

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: The method is undefined for type string?

Posted 18 April 2013 - 05:45 PM

junk.setByteToThisString();

junk variable is a String and if you look into a file that contains String class - String.java - you will see that there is no method setByteToThisString().

You want to do:
setByteToThisString(junk);

This post has been edited by darek9576: 18 April 2013 - 05:45 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1