I am very new to Java. My current program loops through a block that asks for users input in the console until the value they type equals done. I want to store each value the user types in an array that is a class property. When I try to append this array, I get an error that says Error:(59, 18) java: not a statement. My code is below. I will point out the line that the error occurs on inside the code. Thanks for your time!
package com.example.java;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the musical key calculator!");
System.out.println("Enter your notes one at a time.");
System.out.println("Use uppercase letters A-G and # for sharp and b for flat.(Ex. Eb)");
System.out.println("Remember that E# and B# do not exist in music!");
System.out.println("When you have entered all of your notes, type 'done'");
System.out.println("------------------------------------------------------------------");
boolean finished = false;
Scale YourScale = new Scale();
while(finished == false) {
System.out.print("Enter a note: ");
String note = scanner.nextLine();
if (note == "done'") {
finished = true;
} else {
YourScale.addNote(note);
}
}
if(finished == true){
StringBuilder output = new StringBuilder("Your notes are ");
String[] completedNotes = YourScale.notes;
for (int i = 0; i < completedNotes.length; i++) {
output.append(completedNotes[i] + " ");
}
}
}
public static class Scale {
public String[] notes = {};
public void addNote(String note){
notes[] = note; //Error occurs here.
}
}
}
Java arrays are fixed length, and that isn't how you create (or populate an array). I would prefer to use a Collection like,
public List<String> notes = new ArrayList<>();
public void addNote(String note){
notes.add(note);
}
But, you could use Arrays.copyOf(T[], int) and something like
public String[] notes = new String[0];
public void addNote(String note){
int len = notes.length;
notes = Arrays.copyOf(notes, len + 1);
notes[len] = note;
}
Finally, you do not test String equality with ==
if (note == "done'") {
should be something like
if (note.equals("done")) {
notes is a String array, which means it has many String objects inside. Also you initialize it to an empty array. Arrays should have a fixed size.
//Declare a variable notes, and initialize it as an empty array?
public String[] notes = {};
public void addNote(String note)
{
//This doesn't make sense in java - it's syntax is wrong
notes[] = note;
}
If you want to use arrays this is an example:
//Declare a variable notes, and initialize it as an array of
//specific size (I used 5 as example)
public String[] notes = new String[5];
public void addNote(String note)
{
//Here you should have some short of counter that counts in which
// position of the array you will save 'note' or just run a 'for'
//loop and the first element that is not initialized can get the note
for (int i = 0; i < notes.length; i++)
if (notes[i] == null)
{
notes[i] = note;
break;
}
}
Although this method allows you to save a fixed size, which is not desirable in your case, but nevertheless it uses Array and can help you understand how to use them.
If you want to implement it properly you should use an ArrayList. An ArrayList is an array where you can add new elements and remove them. You can find plenty of documentation online of how to use them.
You are trying to assign a String to a String array. You probably intended to add it to the array.
Related
I'm hoping someone can help me with these two values that have me stuck on a project. I have two classes and this first one generates a 2D array with random values.
import java.util.concurrent.ThreadLocalRandom;
public class Guitar {
private int strings;
private int chords;
public Guitar(int mstrings, int mchords) {
this.strings = mstrings;
this.chords = mchords;
}
private double[][] song = new double[strings][chords];
public void generateSong() {
for (int i = 0; i < song.length; i++) {
for (int j = 0; j < song[i].length; j++) {
song[i][j] = ThreadLocalRandom.current().nextDouble(27.5, 4186);
System.out.printf(" %.2f",song[i][j]);
}
System.out.println();
}
}
}
The number of rows and columns is determined by command line arguments. args[0] is the number of rows, args[1] is the number of columns. I converted them to int variables in the main method class
public class Songwriter {
public static void main(String[] args) {
System.out.println("Guitar(): Generated new guitar with " + args[0] + " strings. Song length is " + args[1] + " chords.");
String args0 = args[0];
int strings = Integer.parseInt(args0);
String args1 = args[1];
int chords = Integer.parseInt(args1);
Guitar guitarObj1 = new Guitar(strings, chords);
guitarObj1.generateSong();
}
}
My problem lies in passing the int variables of the command line arguments to make the 2D array the corresponding size. I know my code isn't completely wrong b/c when I set the strings and chords variables equal to 3 and 4 or whatever in the Guitar class itself, the table prints fine.
Sorry if I seem clueless. My class just covered the first chapter on object oriented programming and I've yet to get the fundamentals down.
This is the problematic line:
private double[][] song = new double[strings][chords];
When you create a new object of your Guitar class, the song array is initialized with whatever the values of strings and chords are at that time, which would (most probably) be 0.
Change it to this:
private double[][] song;
public Guitar(int mstrings, int mchords) {
this.strings = mstrings;
this.chords = mchords;
song = new double[mstrings][mchords];
}
EDIT : OP you just answered your own question :)
It doesn't crash but the only output is the system.out.print in the
first line of the main. I believe it's because the strings and chords
variables default to 0, making the array 0x0, and I'm failing to
change their values
I have the following code, and I have sort the array list alphabetically in the main method, as the user inputs his strings.
Here is my code:
import java.util.Scanner;
import java.util.ArrayList;
class Main{
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
Scanner scan = new Scanner(System.in);
String name;
do{
System.out.println("Enter the next name: ");
name = scan.nextLine();
String toUpperCase = titleCase(name);
if(!toUpperCase.equals("Stop")){
names.add(toUpperCase);
}
} while(!name.equalsIgnoreCase("STOP"));
System.out.println(names.toString());
}
public static String titleCase(String s){
String output = s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
return output;
}
}
Please don't give any generic answers, I've been struggling with this for a while now. If the answer seems simple to you, it probably isn't for me.
replace this line:
names.add(toUpperCase);
with this:
int index = names.size();
for (int i = 0; i < names.size(); i++) {
if (names.get(i).compareTo(toUpperCase) > 0) {
index = i;
break;
}
}
names.add(index, toUpperCase);
so, every time you have new string from user - you will insert it into proper position of your array list
this method is quite slow, but ok for home assignment
As suggested in the comments, the most simple way of maintaining a sorted data structure upon each insert is to use a TreeSet or any other data structure that maintains sorted order internally. Instead of declaring an ArrayList<String> you would simply need to modify your code to this:
Set<String> names = new TreeSet<>();
Scanner scan = new Scanner(System.in);
String name;
do {
System.out.println("Enter the next name: ");
name = scan.nextLine();
String toUpperCase = titleCase(name);
if(!toUpperCase.equals("Stop")){
names.add(toUpperCase);
}
} while(!name.equalsIgnoreCase("STOP"));
From Javadocs for TreeSet:
the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.
Please try below code. You can replace the sorting algorithm with more efficient algorithm like merge sort/selection sort etc..
import java.util.Scanner;
import java.util.ArrayList;
class alsort{
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
Scanner scan = new Scanner(System.in);
String name;
do{
System.out.println("Enter the next name: ");
name = scan.nextLine();
String toUpperCase = titleCase(name);
if(!toUpperCase.equals("Stop")){
names.add(toUpperCase);
}
} while(!name.equalsIgnoreCase("STOP"));
System.out.println(names.toString());
for(int i=0;i<name.length();i++){
for(int j=i;j<=name.length();j++){
if(names.get(i).compareTo(names.get(j))>0){
String tmp=names.get(i);
names.set(i, names.get(j));
names.set(j, tmp);
}
}
}
System.out.println(names.toString());
}
public static String titleCase(String s){
String output = s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
return output;
}
}
public class SortedArrayList<T> extends ArrayList<T> {
/**
*
*/
private static final long serialVersionUID = 1L;
#SuppressWarnings("unchecked")
public void insertSorted(T value) {
add(value);
Comparable<T> cmp = (Comparable<T>) value;
for (int i = size()-1; i > 0 && cmp.compareTo(get(i-1)) < 0; i--)
Collections.swap(this, i, i-1);
}
public static void main(String[] s){
SortedArrayList<String> myList = new SortedArrayList<String>();
myList.insertSorted("ddd");
myList.insertSorted("aaa");
myList.insertSorted("xyz");
System.out.println(myList);
}
}
Hello I have implemented this basic program which should sort out the strings that are inserted however it somehow is failing to insert the strings .
For example if I implement :
TestSort t = new TestSort();
t.i("abc");
t.i("aab");
Can anybody see the error and help me fix this error please ?
Thank you
Here is the code :
public class TestSort {
private int length;
String[] data;
public TestSort() {
length = 0;
}
public void i(String value) {
data[length] = value;
setSorted(data);
length++;
}
public void setSorted(String data[]) {
for(int i = data.length-1; i >= 0; i--) {
for(int j = 0; j < i; j++) {
if(data[j].compareTo(data[j + 1]) > -1) {
String temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
for(int i = 0; i < data.length; i++) {
System.out.print(data[i] +" ");
}
}
}
You don't initialize the array data. So it is set null, and accesses with data[i] will get you an NullPointerException. Even if you initialize this field, it will not work, as Arrays in Java have a fixed size, you have to reallocate the Array, if you insert a new value. You should try a List-implementation instead.
So the code should initialize in the constructor:
data = new ArrayList<String>();
and insertion would change to
data.add(value);
you can change your constructor code as (String array max length can be taken as input parameter):
public testsort()
{
data = new String[10];
length = 0;
}
But if you are not sure with the size of array you can use ArrayList.
You are getting exception because you are comparing with data[j+1] that is still null.
first time when you call
t.i("abc");
there is only one reference in data array that is pointing to String literal "abc" and that is at index 0. index 1 is still referring to null.
first String is already sorted so no need to sort that. if you are having more than one string then you should call setSorted() method.
to solve this you can put your condition in loop as:
if((data[j] != null && data[j+1] != null) &&(data[j].compareTo(data[j + 1]) > -1))
A working example but still: use a List and life is much easier :-)
public class Test {
private int length;
private String[] data;
public Test(int arrayLength) {
// INITIALIZE YOU ARRAY --> No NULLPOINTEREXCEPTION!
data = new String[arrayLength];
length = 0;
}
public void i(String value) {
data[length] = value;
length++;
}
public void setSorted() {
for (int j = 0; j < data.length - 1; j++) {
if (data[j].compareTo(data[j + 1]) > -1) {
String temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
for (String s : data) {
System.out.println(s);
}
}
public static void main(String[] args) {
Test t = new Test(5);
t.i("bbb");
t.i("aaa");
t.i("ccc");
t.i("zzz");
t.i("ddd");
// USE SETSORTED HERE --> else you fill your array with the same elements
t.setSorted();
}
}
The variable 'data' is null since it is nowhere initialized hence giving null pointer exception. Since 'data' is an array and as per the rule whenever an array is defined, it has to be of defined length. for e.g if we consider your case. 'data' can be initialized as :-
String[] data = new String[any numerical value]
the numerical value will be its length i.e. the maximum number of elements it can hold.
Secondly, as per your program statement :-
data[length] = value;
is trying to assign value at data's [length] index which is completely wrong since you haven't defined the length therefore how could you guess the index's value. Therefore your this approaoch is logically wrong.
For such situation i.e. whenever we're unaware about the length of the array, use of ArrayList is suggested. Therefore your program can be re-written by two ways:-
1) Either define the length of the array
String[] data = new String[n];
where n ranges from at least 1 to any positive integer.
2) By using ArrayList
public class Main {
List<String> data;
public Main(){
data = new ArrayList<String>();
}
public static void main(String... q){
Main m = new Main();
m.insertData("abc");
m.insertData("zxy");
m.insertData("aab");
m.insertData("aaa");
m.showData();
}
public void insertData(String str){
data.add(str);
Collections.sort(data);
}
public void showData(){
if(data!=null && !data.isEmpty()){
for(String s : data){
System.out.println(s);
}
}
}
}
output:-
aaa
aab
abc
zxy
Hope this helps.
as Mnementh suggested, the reason for NPE is that you have created the field data of type String[] but you never initialized it.
Other answers have provided every reason on why your code throwing ugly errors; I have just improved your code by replacing your String[] with List<String> so you don't have to worry about the size of your array anymore.
Sorting is also simplified now using Collections.sort().
have a look,
class test1 {
public static void main(String[] args) {
Test sorting = new Test();
sorting.input("abc");
sorting.input("cba");
sorting.input("aab");
sorting.setSorted();
}
}
class Test {
private List<String> data = new ArrayList<String>();
public void input(String value) {data.add(value);}
public void setSorted() {
Collections.sort(data);
for (String current : data) {
System.out.println(current);
}
}
}
if you are using Java 8, then you can use Arrays.parallerSort(), it performs sorting the same way as Collection.sort but with a parallel implementation.
Current sorting implementations provided by the Java Collections Framework > (Collections.sort and Arrays.sort) all perform the
sorting operation sequentially in the calling thread. This enhancement
will offer the same set of sorting operations currently provided by
the Arrays class, but with a parallel implementation that utilizes the
Fork/Join framework. These new API's are still synchronous with regard
to the calling thread as it will not proceed past the sorting
operation until the parallel sort is complete.
to implement it, replace Collections.sort with Arrays.parallelSort in the above code,
Replace,
Collections.sort(data);
with,
Arrays.parallelSort(data.toArray(new String[data.size()]));
I've written a java picnic game (very basic), the only thing I don't know how to do is get the program not to accept items of the same letter. I also want the program to list how many times the user entered a rejected item. Note that this allows any ordering of items, as long as no two items start with the same letter (dis-
regarding case). An acceptable sequence of inputs would be
mustard
,
ketchup
,
tofu
,
anchovies
.
However,
mustard
,
ketchup
,
tofu
, and
Kettle corn
would not work since \
Kettle corn
" begins
with the same letter as \
ketchup
" (ignoring case).
import java.util.*;
public class PlayPicnic
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Picnic picnic = new Picnic();
ArrayList<String> unaccepted = new ArrayList<>();`enter code here`
while (picnic.numberOfItems() < 5)
{
System.out.print("What do you want to bring on the picnic? ");
String item = scan.nextLine();
if (picnic.okayToBring(item))
{
picnic.add(item);
}
else
{
if(!unaccepted.contains(item)) unaccepted.add(item);
System.out.println("Sorry, you can't bring " + item);
}
}
System.out.println("\nHere's what we'll have at the picnic:");
picnic.show();
System.out.println(Arrays.toString(unaccepted.toArray()));
}
}
corresponding class
import java.util.*;
public class Picnic
{
// INSTANCE VARIABLES:
private ArrayList<String> stuffToBring; // items to bring on the picnic
// CONSTRUCTOR:
//-----------------------------------------------------
// Construct a new Picnic.
//-----------------------------------------------------
public Picnic()
{
stuffToBring = new ArrayList<String>(); // initialize list
}
//-----------------------------------------------------
// Given an item s, see if it's okay to add it to the list.
// Return true if it is, false otherwise:
//-----------------------------------------------------
public boolean okayToBring(String s)
{
// "Secret rule" -- s can't be an item already in the list:
if (stuffToBring.contains(s)) // "contains" is in the ArrayList class
{
return false;
}
else
{
return true;
}
}
//-----------------------------------------------------
// Given an item s, add it to the list (if it's okay to add it)
//-----------------------------------------------------
public void add(String s)
{
if (okayToBring(s)) // this test keeps people from cheating!
{
stuffToBring.add(s);
}
}
//-----------------------------------------------------
// Print the items in the list
//-----------------------------------------------------
public void show()
{
for (int i = 0; i < stuffToBring.size(); i++)
{
String s = stuffToBring.get(i);
System.out.println(s);
}
}
//-----------------------------------------------------
// Returns the number of items in the list:
//-----------------------------------------------------
public int numberOfItems()
{
return stuffToBring.size();
}
}
Make a custom Set to remove duplicate, if first char is same(ignoring the case).
Set<String> unaccepted =new TreeSet<>(new Comparator<String>() {
#Override
public int compare(String o1, String o2) {
Character first=Character.toLowerCase(o1.charAt(0));
Character second=Character.toLowerCase(o2.charAt(0));
return first.compareTo(second);
}
});
Now, add the value to this Set. It will ignore the same first Character data.
Change the while loop like below,
while (unaccepted.size()<5){
System.out.print("What do you want to bring on the picnic? ");
String item = scan.nextLine();
unaccepted.add(item);
}
Here is logic for checking for items in list with first char
public boolean checkItem(String input, List<String> items) {
String inputFirstChar = input.substring(0, 1);
boolean exist = false;
for (String item : items) {
String itemFirstChar = item.substring(0, 1);
if(itemFirstChar.equalsIgnoreCase(inputFirstChar)) {
exist = true;
break;
}
}
return exist;
}
private ArrayList<String> stuffToBring;
make your reference variable type with interface not the ArrayList implementation, do like this one
private List<String> stuffToBring;
To reject words with a used first letter, you put a simple String in class "Picnic":
private String usedLetters = "";
Then in method "okayToBring" you check if the letter is already used:
public boolean okayToBring(String s) {
return (usedLetters.indexOf(s.toLowerCase().charAt(0)) == -1); // letter not contained in String
}
and in method "add" you append the new word's first character to this String:
public void add(String s)
{
if (okayToBring(s))
{
stuffToBring.add(s);
usedLetters += s.toLowerCase().charAt(0);
}
}
Regarding your second question, your phrasing is a bit unclear. If you want to count how many times a specific item has been unsuccessfully entered, you could use a Hashmap to store rejected Strings and their count:
private HashMap<String, int> unaccepted = new HashMap<String, int>();
Then your program's "else" clause looks like follows:
int newcount = (unaccepted.containsKey(item) ? unaccepted.get(item)++ : 1);
unaccepted.put(item, newcount);
System.out.println("Sorry, you can't bring " + item + "(" + unaccepted.get(item) + " unsuccessful tries)");
If you just want to count the total number of unsuccessful entries, use a Set and println(unaccepted.size());
This question already has an answer here:
How to use java.util.Scanner to correctly read user input from System.in and act on it?
(1 answer)
Closed 7 years ago.
Alright, so the code is pretty straight forward. Generic class ourSet, that takes in some elements, puts it in a LinkedList, and does some functions on the two sets.
My problem is actually quite unrelated the general concept of the project, its more in the "user input interface" I've created. I want it to take in some Strings and add it to the set, then while receiving the string "EXIT" (all caps), to exit the loop, and do the same for the next set. What is happening is that the do while loop is only sending the 1st, 3rd, 5th,.. for all odd numbers.
package set.pkgclass;
import java.util.Scanner;
import java.util.LinkedList;
public class SetClass {
public static void main(String[] args) {
ourSet<String> set1 = new ourSet<String>();
ourSet<String> set2 = new ourSet<String>();
Scanner input = new Scanner(System.in);
System.out.println("Please enter a string to put in set 1, "
+ "type EXIT (in all caps) to end.");
do {
set1.add(input.nextLine());
}
while (!"EXIT".equals(input.nextLine()));
System.out.println("Please enter a string to put in set 2, "
+ "type EXIT (in all caps) to end");
do {
set2.add(input.nextLine());
}
while (!"EXIT".equals(input.nextLine()));
ourSet.intersection(set1,set2);
ourSet.difference(set1, set2);
ourSet.union(set1, set2);
}
}
class ourSet<T>{
private LinkedList<T> mySet = new LinkedList<>();
public void add(T element){
mySet.add(element);
}
public void remove(T element){
mySet.remove(element);
}
public boolean membership(T element){
if(mySet.contains(element) == true) {
return true;
}
else {
return false;
}
}
public static <T> void union(ourSet<T> s1, ourSet<T> s2){
System.out.print("The union is: ");
for (int i=0; i < s1.mySet.size(); i++) {
T t = s1.mySet.get(i);
if (!s2.mySet.contains(t)){
s2.add(t);
}
}
for (int i=0; i < s2.mySet.size(); i++){
T t = s2.mySet.get(i);
System.out.print(t+", ");
}
System.out.println();
}
public static <T> void intersection(ourSet<T> s1, ourSet<T> s2){
System.out.print("The intersection is: ");
for (int i=0; i < s1.mySet.size(); i++) {
T t = s1.mySet.get(i);
if (s2.mySet.contains(t)) {
System.out.print(t+", ");
}
}
System.out.println();
}
public static <T> void difference(ourSet<T> s1, ourSet<T> s2){
System.out.print("The difference is: ");
for (int i=0; i < s1.mySet.size(); i++) {
T t = s1.mySet.get(i);
if (!s2.mySet.contains(t)) {
System.out.print(t+", ");
}
}
System.out.println();
}
}
The reason is, you're calling input.nextLine() twice:
do {
set1.add(input.nextLine());
}
while (!"EXIT".equals(input.nextLine()));
A much easier way than do-while is while:
while (!(String in = input.nextLine()).equals("EXIT")) {
set1.add(in);
}
You ask for input twice
do {
set1.add(input.nextLine()); // you enter a number
}
while (!"EXIT".equals(input.nextLine())); // what if you enter another number?
// it'll just loop again, skipping that number
You need to ask for input once and use that either to stop or add it to your set.
You loop is doing exactly what you've told it to...
Read a line of text and add it to set1
Read a line of text and check to see if it equals "EXIT"
Repeat as required...
So, every second request is being used to check for the exit state. Instead, you should assign the text from the input to a variable and use it to do you checks, for example...
do {
String text = input.nextLine();
if (!"EXIT".equals(text)) {
set1.add();
}
} while (!"EXIT".equals(text));
ps- Yes, I know, you could use break ;)