Array of Arrays JAVA [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm having a problem with making a 2d array that stores user input and shows an error when the name that you enter for example is already stored in that array.
this is my program so far...
import java.io.*;
import java.lang.*;
public class a extends b{
public static void main (String args[]) throws Exception{
String phonebook[][] = new String[2][];
BufferedReader input = new BufferredReader (new InputStreamReader (System.in));
System.out.println("[1] Add contacts");
System.out.println("[2] View all contacts");
int choice = input.nextInt();
selection(choice);
}
}
import java.io.*;
import java.lang.*;
public class b{
public static void selection(int choice){
case 1:
System.out.println("Enter name: ");
phonebook[0][0] = input.nextLine();
System.out.println("Enter landline or phone numbers: ");
phonebook[0][1] = input.next();
for(int x = 0; x < phonebook.length; x++){
for (int y = 0; y < phonebook[x].length ; y++){
}
}
break;
case 2:
show_phonebook(phonebook);
break;
default:
System.out.println("ERROR");
break;
}
public static void show_phonebook(String phonebook[][]){
System.out.println(phonebook[x][y]);
System.out.println();
}
}
I know this code looks shit but I'm still a noob. I don't know how to do the error thing so a little help will be very grateful. Thanks

Class names in Java Start with an Capital "A extends B"
I don't see a profit in inheriting another Class here. Define the methods in Class A
You will need a proper switch(argument) case: and so on....
showing all contancts should loop through the filled arrays.
For your error message solution: define a input string and try to check the indexes in the forloop equality: if(array [x][y].equals(input)){ code...}
Edit: you can make your check if "better" by first .toLowerCase the input and String at Array[x][y].toLowerCase -> the improvement would be that the user can check for MaRTin and if there was already an mArtIN stored, it will trigger.

Related

I want to print the output using method but then i cant print it. WHY? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
This is my code.
What I think my error is that method code.
import java.util.Scanner;
public class Question1 {
public static void main (String[]args){
String major;
Scanner read = new Scanner (System.in);
System.out.print("Enter character : " );
major=read.next();
}
}
public static void mn(){ //method
if (major==M){
System.out.println("Mathematic");
}
else if (major==C){
System.out.println("Computer Science");
}
else if (major==I){ technology
System.out.println("Information Technology");
}
else {
System.out.println("Invalid Input");
}
}
A couple of things are wrong with your code.
Your method mn lies outside of your class. This is invalid.
Strings should be compared with equals
Your method doesn't take any arguments but requires one in this case.
M, C and I need to be String literals, otherwise Java can interpret them as something different.
You actually need to call the method mn to see its output.
So all in all, your code should look like this
import java.util.Scanner;
public class Question1 {
public static void main (String[]args){
String major;
Scanner read = new Scanner(System.in);
System.out.print("Enter character : " );
major=read.nextLine(); // better nextLine, because otherwise the press of "Enter" will not be registered.
mn(major);
}
public static void mn(String major){ //method
if (major.equals("M")){
System.out.println("Mathematic");
}
else if (major.equals("C")){
System.out.println("Computer Science");
}
else if (major.equals("I")){
System.out.println("Information Technology");
}
else {
System.out.println("Invalid Input");
}
}
}
You don't have any calls to mn() in your main function, so the code inside the function won't run. Add the following line to the end of your main function to make the mn() function run, and add "String major" inside the parenthesis of your mn() function declaration:
mn(major);
I hope this helps!

How does one go about doing so [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Write a method called multiConcat that takes a String and an integer as parameters. Return a String made up of the string parameter concatenated with itself count time, where count is the integer. for example, if the parameters values are “ hi” and 4, the return value is “hihihihi” Return the original string if the integer parameter is less than 2.
What i have so Far
import java.util.Scanner;
public class Methods_4_16 {
public static String multiConcat(int Print, String Text){
String Msg;
for(int i = 0; i < Print; i ++ ){
}
return(Msg);
}
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
int Prints;
String Texts;
System.out.print("Enter Text:");
Texts = Input.nextLine();
System.out.print("Enter amount you wanted printed:");
Prints = Input.nextInt();
System.out.print(multiConcat(Prints,Texts));
}
}
Just a few hints:
concating strings can be done this way: appendTo += stuffToConcat
repeating an operation n times can be done with a for-loop of this kind:
for(int i = 0 ; i < n ; i++){
//do the stuff you want to repeat here
}
Should be pretty simple to build the solution from these two parts. And just in case you get a NullPointerException: remember to initialize Msg.
Try this:
public static String multiConcat(int print, String text){
StringBuilder msg = new StringBuilder();
for(int i = 0; i < print; i ++ ) {
msg.append(text);
}
return msg.toString();
}
I have used StringBuilder instead of a String. To know the difference, give this a read: String and StringBuilder.
Also, I would guess you are new to Java programming. Give this link a read. It is about Java naming conventions.
Hope this helps!

I have a casting error (String to int) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
A friend and I are making a simple game where it randomly picks one of those names and the user has to guess it until he/she gets it right. , but we are getting an error saying: Exception in thread "main" java.lang.NumberFormatException: For input string: "Adam Kovic". Can anyone please help?
package projectpackage;
import java.util.Random;
import java.util.Scanner;
public class ProjectClass {
public static void main(String eth[]) {
int adam = Integer.valueOf("Adam Kovic");
int bruce = Integer.valueOf("Bruce Greene");
int joel = Integer.valueOf("Joel Ruben");
int spoole = Integer.valueOf("Sean Poole");
int larr = Integer.valueOf("Lawrence Sonntag");
int james = Integer.valueOf("James Willems");
int matt = Integer.valueOf("Matt Peake");
Random r = new Random();
int num[] = { adam, bruce, joel, spoole, larr, james, matt };
}
}
You could get away with just making an array of strings, each element being a string with one of those names, and have a randomized pick between 0 and 6; whichever number it picked would be the specific array element chosen. Then in the code for checking if the player has picked the correct name, simply compare the user's input string to the string array element the randomizer picked.
Because Integer.valueOf() expects a number in string format. example "23".
So it should used in this case Integer.valuOf("23")
Is this for your homework?
public static void main(String eth[]) {
List<String> names = new ArrayList<String>();
names.add("Adam Kovic");
names.add("Adam Kovic 2");
names.add("Adam Kovic 3");
names.add("Adam Kovic 4");
names.add("Adam Kovic 5");
Random r = new Random();
String name = names.get(r.nextInt(names.size()));
boolean guessed = false;
while (!guessed) {
// guess, you can figure out how to get the guess
String guess = "";
//
if (guess.equals(name)) {
System.out.println("you guessed correctly!");
guessed = true;
} else {
System.out.println("Wrong! " + guess + " is incorrect");
}
}
}

I have to write a code that uses classes for DNA, RNA, and Protein. Im stuck on the first part [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have to take a users input int as length of the dna a sequence. Im trying to return the numberString but i get an issue with my array every time
Driver
import java.util.Scanner;
public class GenBank1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("Enter a desired DNA sequence length, between 1 and 10 please:");
int inputLength = in.nextInt();
System.out.println(inputLength);
System.out.println(DNA1.toNumString(inputLength));
int[] baseId = new int[inputLength];
for(int i=0;i<=baseId.length;i++){
baseId[i]=inputLength;
int rndmr = (int)(4.0*Math.random());
baseId[i]-=rndmr;
System.out.print(baseId[i]+1 + ",");
}
}
}
for(int i=0;i<=baseId.length;i++)
You should probably do
for(int i=0;i<baseId.length;i++)
since, in an array with (say) 10 entries, they will have indices 0 to 9. Trying to look up array[10] will throw an exception.
Without knowing what the DNA1 class is and what the function of the program is, I would not be able to comment futher. However yes, you will experience ArrayIndexOutOfBoundsException to correct this the following should work
import java.util.Scanner;
public class GenBank1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("Enter a desired DNA sequence length, between 1 and 10 please:");
int inputLength = in.nextInt();
System.out.println(inputLength);
System.out.println(DNA1.toNumString(inputLength));
int[] baseId = new int[inputLength];
for(int i=0;i<baseId.length;i++){
baseId[i]=inputLength;
int rndmr = (int)(4.0*Math.random());
baseId[i]-=rndmr;
System.out.print(baseId[i]+1 + ",");
}
}
}
Your ending clause for your FOR loop wanted to go from 0 -> length which would be wrong as you will go over the Array index.

Purchase Java with Arrays [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm Raymond, computer programming student. I have problem about arrays. Are instructor ask us to do a program that goes like this.
in this codes below. i want to display the same item code i entered. but the problem is that once i answered yes and input again number, the only thing that display is the last number or code i enter.
import java.util.Scanner;
public class _TindahanArray {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
String ans, i = "";
int x;
do {
System.out.print("Item code:");
i += a.next();
System.out.print("\nAnother item? [y/n]:");
ans = a.next();
} while (ans.equals("y"));
String[] code = new String[2];
for (x = 0; x < 1; x++) {
code[x] = i;
System.out.print(code[x]);
code[x] = "\n";
System.out.print(code[x]);
}
}
}
As you shown some efforts, I just want to update your code.
Your code is fine for only printing two item codes.
Use collection ArrayList to store the item codes. I am using String array list.
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayTest {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
String ans;
ArrayList<String> itemCodeList = new ArrayList<String>(); //create array list
do{
System.out.print("Item code:");
itemCodeList.add(a.next()); //add item code into array list
System.out.print("\nAnother item? [y/n]:");
ans = a.next();
}while(ans.equals("y"));
for (String code : itemCodeList)
{
System.out.println(code);
}
}
}
ArrayList example

Categories