Sono nuovo qui e anche in programmazione. Sto cercando di studiare altri argomenti da solo, dal momento che il mio istruttore non è abbastanza d'aiuto quando ho una domanda quindi eccola qui. Voglio invertire una parola con una pila generica.Inversione di una parola con una pila
Il mio pop, push, isEmpty e metodi peek funzionano (li ho testati con un programma più semplice che ho fatto prima di provarlo su questo.) E l'output sembra darmi la parola invertita char per char ma sempre dandomi un null prima di ogni char!
Le mie domande sono: Perché sta succedendo? E anche se ho un metodo expandCapacity per funzionare quando la capacità è a 9 ma non si applica quando l'input supera il limite.
Ecco il mio codice
package Stack;
import java.util.Scanner;
public class ReverseDriver<T> {
private static String out;
private static String in;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your sentence: ");
in = input.nextLine();
int size = in.length();
ArrayStack<Character> revStack = new ArrayStack<>(size);
for (int i = 0; i < in.length(); i++) {
char u = in.charAt(i);
revStack.Push(u);
if (in.length() > 9) {
revStack.expandCapacity();
}
}
while (!revStack.IsEmpty()) {
char u = revStack.Pop();
out = out + u;
System.out.flush();
System.out.print(out);
}
}
}
ecco l'output
run:
Enter a word:
word
nullr
nullro
nullrow
Exception in thread "main" java.lang.NullPointerException
at Stack.ReverseDriver.main(ReverseDriver.java:37)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
EDIT: ecco i metodi che ho detto che stavano lavorando.
@Override
public void Push (T element)
{
if (count == stack.length){
expandCapacity();
}
stack[++count] = element;
//System.out.println(count);
}
@Override
public String toString()
{
String result = "<top of stack>\n";
for (int index=count-1; index >= 0; index--){
result += stack[index] + "\n";
}
return result + "<bottom of stack>";
}
@Override
public boolean IsEmpty()
{ //Checks if array is empty
if(count == 0){
System.out.println("Nothing");
}
return count == 0;
}
public T Pop()
{
T output;
output = (stack[count - 1]);
count--;
return(output);
}
@Override
public T Peek()
{
//looks at the object at the top of this stack without removing it
//from the stack.
if(stack.length == 0){
// {
System.out.println("Cant peek a ghost");
}
return(stack[--count]);
}
// else
// {
// System.out.println(stack[count-1]);
// }
// }
@Override
public int Size()
{
//Sets the size of this vector
if(stack.length == 0){
System.out.println("Nothing inside");
}
System.out.println("The array's size is : " + count);
return count;
}
}
Mentre mi rendo conto che lei ha detto che sta lavorando, ti invitiamo a darci il codice nel metodo expandCapacity e EVuota, giusto per darci un po 'di informazioni sul perché questo è happening :) – christopher
Sei sicuro che questo è lo stesso codice che genera l'output - "Inserisci una parola" contro "Inserisci una frase" –
note: 'expandCapacity' viene chiamato 10 volte se' in.length() 'è 10, è questo intenzionale? Inoltre, 'in' e' out' dovrebbero essere più propriamente variabili locali in main, non in statics (almeno se questo era un programma "reale"). – hyde