This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's Calculator Programming subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.

This forum is locked: you cannot post, reply to, or edit topics. General Coding and Design => Calculator Programming
Author Message
WikiGuru
ADOS (Attention deficit... Oh! Shiny!)


Elite


Joined: 15 Sep 2005
Posts: 923

Posted: 22 Jan 2007 05:32:32 pm    Post subject:

I've been trying to create an array of strings in Java. Here's my code (only the parts giving me problems):

In Class A:

Code:
public class A{
 private String a[10];

 public void changeElement(String e, int num){
  a[num]=e;
 }

}


Here's my application code:

Code:
import Java.util.Scanner;
public class App{
 Scanner reader=new Scanner(System.in);
 System.out.print("Elements(less than 10):");
 int j=reader.nextInt();
 A test=new A();
 for(int i=0; i<j; i++){
  While(reader.nextLine()=="\n");
  System.out.print("\nInput element "+i+":");
  A.changeElement(reader.nextLine(),i);
 }
}


What's wrong with this? It compiles fine, but throws a runtime error pointing to changeElement when I when I try to run this code and it reaches that point.
Back to top
JoeImp
Enlightened


Active Member


Joined: 24 May 2003
Posts: 747

Posted: 22 Jan 2007 07:09:00 pm    Post subject:

Why would you want to create a class to do that? Just access the array directly.

Last edited by Guest on 22 Jan 2007 07:09:21 pm; edited 1 time in total
Back to top
WikiGuru
ADOS (Attention deficit... Oh! Shiny!)


Elite


Joined: 15 Sep 2005
Posts: 923

Posted: 22 Jan 2007 07:20:06 pm    Post subject:

I'm creating a phone book class for my programming class.
Back to top
JoeImp
Enlightened


Active Member


Joined: 24 May 2003
Posts: 747

Posted: 22 Jan 2007 07:30:27 pm    Post subject:


Code:
public class String_Class
{
   private String array[];
   
   public String_Class()
   {
  array = new String[10];
   }
   
   public void set(String value, int index)
   {
  array[index] = value;
   }
   
   public void output()
   {
  for (int x = 0; x < 10; x++)
  {
     System.out.println(array[x]);
  }
   }
   
   public static void main(String [] args)
   {
  String_Class tester = new String_Class();
  
  for (int x = 0; x < 10; x++)
  {
     tester.set(new String(Integer.toString(x)), x);
  }
  
  tester.output();
   }
}



Code:
0
1
2
3
4
5
6
7
8
9
Back to top
Arcane Wizard
`semi-hippie`


Super Elite (Last Title)


Joined: 02 Jun 2003
Posts: 8993

Posted: 23 Jan 2007 02:59:36 am    Post subject:

For testing classes I suggest JUnit.

Quote:
I'm creating a phone book class for my programming class.
I'm betting the phone book won't always hold exactly 10 or less entries.

Try Java.util.HashMap or Java.util.ArrayList. They're like arrays but actually usefull and come with all the functions for element manipulation you'd need.
Back to top
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are UTC - 5 Hours

 

Advertisement