method intValue() is undefined for the type String (?)

Status
Nicht offen für weitere Antworten.

blue439

Mitglied
Hallo,

weshalb wird die Fehlermeldung: "method intValue() is undefined for the type String" ausgegeben?

Das Beispiel wird so ( fast identisch bzw. ähnlich) in der Literatur erwähnt...

Code:
import java.util.*;

public class HTDemo {
	public static void main(String[] args) {
		String text = "Have a good day. Have a good night. Have fun";
		
		Map<String,Integer> hashMap = new HashMap<String, Integer>();
		String[] words = text.split("[ .]");
		
		for( int i=0; i<words.length; i++) {
			if( words[i].length() > 1) {
				int value = hashMap.get(words[i].intValue());
				value++;
				hashMap.put(words[i], 1);
			}
		}
		
		Map<String, Integer> treeMap = new TreeMap<String, Integer>(hashMap);
		System.out.println("Wortanzahl in absteigender Reihenfolge: ");
		System.out.print(treeMap);
	}
}

Grüße
 
S

SlaterB

Gast
> Das Beispiel wird so ( fast identisch bzw. ähnlich) in der Literatur erwähnt...

komische Literatur, intValue() gibts für Integer & Long, nicht für String
 

blue439

Mitglied
:oops:
mein Fehler - das fehlte wohl etwas (sollte wohl mal 'ne Pause machen):

Code:
import java.util.*;

public class HTDemo {
	public static void main(String[] args) {
		String text = "Have a good day. Have a good night. Have fun";
		
		Map<String,Integer> hashMap = new HashMap<String, Integer>();
		String[] words = text.split(" ");
		
		for( int i=0; i<words.length; i++) {
			if( words[i].length() > 1) {
				if(hashMap.get(words[i]) != null) {
				int value = hashMap.get(words[i]).intValue();
				value++;
				hashMap.put(words[i], value);
			}
			else
				hashMap.put(words[i], 1);
		}
	}
		
		Map<String, Integer> treeMap = new TreeMap<String, Integer>(hashMap);
		System.out.println("Wortanzahl in absteigender Reihenfo ");
		System.out.print(treeMap);
	}
}
Das Bsp. stammt aus: Liang: Introduction to Java Programming, ein ähnliches Bsp. findet sich in Louis/Müller: Java 6 Kompendium

"Wortanzahl in absteigender Reihenfolge " stammt von mir :wink:
Das Bsp. sollte mir als Grundlage dienen, um es dann "umzustricken".
 
G

Gelöschtes Mitglied 5909

Gast
Hatte des auch mal vor einiger Zeit gemacht, is eigentlich easy :)

Code:
import java.io.FileReader;
import java.io.LineNumberReader;
import java.util.Iterator;
import java.util.TreeMap;

public class Wordcount {

	public static void main(String args[]) {
		if (args.length != 1) {
			System.out.println("Usage: java Wordcount <filename>");
			System.exit(-1);
		}
		TreeMap<String, Integer> words = new TreeMap<String, Integer>();
		int count = 0;
		try {
			FileReader reader = new FileReader(args[0]);
			LineNumberReader lineReader = new LineNumberReader(reader);
			String line;
			while ((line = lineReader.readLine()) != null) {
				count++;
				if (words.get(line) == null) {
					words.put(line, 1);
				} else {
					words.put(line, words.get(line) + 1);
				}
			}
			lineReader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		Iterator<String> iter = words.keySet().iterator();
		while (iter.hasNext()) {
			String key = iter.next();
			System.out.printf("%-25s %d\n", key, words.get(key));
		}
		System.out.println("===========================");
		System.out.printf("%-25s %d", "Wordcount:", count);
	}

}
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
R finaler Wert in outerMethod von method in InnerMethodClass Java Basics - Anfänger-Themen 2
S Methoden 2 non-static Methoden, trotzdem Fehler "non static method can not be referenced from a static context" Java Basics - Anfänger-Themen 9
P myClass?.method() in Java Java Basics - Anfänger-Themen 4
T setFill method Java Basics - Anfänger-Themen 3
H Methode mit Array als Rückgabe This method must return a result of Type int[] Java Basics - Anfänger-Themen 2
N The method setSaldo(double) in the type Konto is not applicable for the arguments (int, int) Java Basics - Anfänger-Themen 2
A startsWith method und substring Java Basics - Anfänger-Themen 2
W Remote Method Invocation RMI - Problem Java Basics - Anfänger-Themen 0
I Java Generics factory method Java Basics - Anfänger-Themen 2
V the static method should be accessed is a static way Java Basics - Anfänger-Themen 6
N Erste Schritte "non-static method" oder "XYZ can not be resolved" Java Basics - Anfänger-Themen 21
K Eigene Annotations, Pre-/Post-/Call-Method Java Basics - Anfänger-Themen 6
B Threads Thread sleep() Method einfache Frage Java Basics - Anfänger-Themen 8
G Was als main Method Java Basics - Anfänger-Themen 6
M Cannot make a static reference to the non-static method Java Basics - Anfänger-Themen 10
E Array to String Method Reverse Java Basics - Anfänger-Themen 5
D Methoden undefined method Java Basics - Anfänger-Themen 13
S Method mit Eingabe abrufen Java Basics - Anfänger-Themen 1
D Interface Amazon Skill Kit, Interface but method in other class? Java Basics - Anfänger-Themen 3
U Method References Java Basics - Anfänger-Themen 1
UnityFriday method getPrevious in class List<ContentType> cannot be applied to given types Java Basics - Anfänger-Themen 29
P Variablen einer Methode in andere Method übergeben Java Basics - Anfänger-Themen 6
B Methoden The method mirror(double[]) in the type Convolution is not applicable for the arguments (double) Java Basics - Anfänger-Themen 8
J easy remove method Java Basics - Anfänger-Themen 1
M abstract method does not override or implement.... Java Basics - Anfänger-Themen 7
R The method printf(String, Object[]) in the type printStrem in not applicable for the arguments ... Java Basics - Anfänger-Themen 3
M Deklaration und Initialisierung bei Method Erstellung Java Basics - Anfänger-Themen 12
K Variablen RETURN in Case-Switch / This method must return a result of type Item Java Basics - Anfänger-Themen 4
W Compiler-Fehler "non-static method cannot be referenced"-Problem Java Basics - Anfänger-Themen 6
K String equalIgnoreCase() Method Java Basics - Anfänger-Themen 19
E non-static method und static context Java Basics - Anfänger-Themen 15
L Command Prompt / Main Method / String Java Basics - Anfänger-Themen 9
M Methoden "Non-static method xy cannot be referenced from a static context" Java Basics - Anfänger-Themen 20
T selection method does not contain a main type Java Basics - Anfänger-Themen 7
U UML Factory Method Java Basics - Anfänger-Themen 4
R Compiler-Fehler Cannot find symbol (Method printIn) Java Basics - Anfänger-Themen 3
H non-static method cannot be referenced from a static context Java Basics - Anfänger-Themen 2
L Cannot make a static reference to the non-static method Java Basics - Anfänger-Themen 6
B Object "Method" in TreeSet, Fehler beim Vergleichen/Comparable Java Basics - Anfänger-Themen 9
M Problem mit Static Method Java Basics - Anfänger-Themen 8
W Main-method in Object-classes Java Basics - Anfänger-Themen 5
J Compiler findet method nicht Java Basics - Anfänger-Themen 12
M This method must return a result of type int Java Basics - Anfänger-Themen 13
M Fehlermeldung: the method.... ist undefined for the type object Java Basics - Anfänger-Themen 6
S this method must return a result of type double Java Basics - Anfänger-Themen 2
G Collections.binarySearch(LinkedList): cannot find method Java Basics - Anfänger-Themen 6
Y Theorie: Abstract Method Java Basics - Anfänger-Themen 6
C Methoden numberField.getDouble no such method error Java Basics - Anfänger-Themen 4
F Mal wieder: Cannot make a static reference to the non-static method Java Basics - Anfänger-Themen 9
F Cannot make a static reference to the non-static method Java Basics - Anfänger-Themen 3
Q non-static method blub cannot be referenced from a static context Java Basics - Anfänger-Themen 6
E Netbeans, "class does not have a main method" Java Basics - Anfänger-Themen 8
L no such method error: main() Java Basics - Anfänger-Themen 2
O Abstract Method & Generics Java Basics - Anfänger-Themen 10
N public class -> public method -> variable private? Java Basics - Anfänger-Themen 10
R Could not find main method Java Basics - Anfänger-Themen 3
U Boolean istGleich() Method Java Basics - Anfänger-Themen 15
Luk10 method () not found Java Basics - Anfänger-Themen 9
A OOP Fehler non-static method can not be[...] Java Basics - Anfänger-Themen 2
N Reference to non-static method Java Basics - Anfänger-Themen 8
G non static method scale(int,int) cannot be referenced from a static context Java Basics - Anfänger-Themen 16
A The method getYear() from the type Date is deprecated Java Basics - Anfänger-Themen 6
A cannot find symbol - symbol : method Java Basics - Anfänger-Themen 5
S The method readInt() Problem Java Basics - Anfänger-Themen 5
E method undefined - wo ist der Fehler? Java Basics - Anfänger-Themen 5
J non static method cannot be referenced from static context Java Basics - Anfänger-Themen 7
L cannot find symbol-method Java Basics - Anfänger-Themen 3
D kleine Passwortabfrage erstellen incl. Method zum verändern Java Basics - Anfänger-Themen 7
S Mein Quicksort Problem: he method quickSort(int[], int, int) Java Basics - Anfänger-Themen 2
M Invalid Method Java Basics - Anfänger-Themen 7
F does not overwride abstract method Java Basics - Anfänger-Themen 2
C Fehler: non-static method Java Basics - Anfänger-Themen 5
R Cannot make a static reference to the non-static method Java Basics - Anfänger-Themen 5
G String zu Method Java Basics - Anfänger-Themen 11
B cannot find symbol method equalsIgnoreCase? Java Basics - Anfänger-Themen 23
T Does not have a main method ? GGT. Java Basics - Anfänger-Themen 4
N invalid method declaration; return type required Java Basics - Anfänger-Themen 4
T this method.is not visible Java Basics - Anfänger-Themen 3
C overridden method does not throw java.io.IOException Java Basics - Anfänger-Themen 4
R non-static method execute Java Basics - Anfänger-Themen 3
J Morgen Java-Klausur. Stack, Heap, Method-Area Java Basics - Anfänger-Themen 2
V Non Static Method. Java Basics - Anfänger-Themen 2
S Method undefinded for "Klasse" Java Basics - Anfänger-Themen 4
R Eclipse: This method must return a result of type double. Java Basics - Anfänger-Themen 2
M Fehlermeldung (The method contains(String) is. Java Basics - Anfänger-Themen 3
M Fehlermeldung (the method add(OE) in the type Abteilung.) Java Basics - Anfänger-Themen 2
N ActionListener versch. Buttons zuweisen => versch. Method Java Basics - Anfänger-Themen 13
T method <init> not found in class . Java Basics - Anfänger-Themen 4
G Problem (oder Verständnisporblem) bei einer static method Java Basics - Anfänger-Themen 9
frau-u Altes Problem: non-static method cannot be reference Java Basics - Anfänger-Themen 7
M Solaris - Method Split Fehler Java Basics - Anfänger-Themen 2
M method xxx() from the type xxx is deprecated Java Basics - Anfänger-Themen 3
A Method.invoke() mit geerbten Methoden Java Basics - Anfänger-Themen 2
A The method getClass() from the type saveLog is not static Java Basics - Anfänger-Themen 2
G Abstract Class - Abstract Method Java Basics - Anfänger-Themen 4
R Could not find Main-Method Java Basics - Anfänger-Themen 4
S intValue Java Basics - Anfänger-Themen 2
R Compiler-Fehler JTable mit XML befüllen | The constructor JTable(Object[], String[]) is undefined Java Basics - Anfänger-Themen 10
D Klassen undefined for the type 'object' Java Basics - Anfänger-Themen 2
F The constructor Vehikel(String, int) is undefined warum?? Java Basics - Anfänger-Themen 4

Ähnliche Java Themen

Neue Themen


Oben